Home | History | Annotate | Line # | Download | only in dev
sequencer.c revision 1.15.8.4
      1 /*	$NetBSD: sequencer.c,v 1.15.8.4 2002/02/11 20:09:37 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) netbsd.org).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: sequencer.c,v 1.15.8.4 2002/02/11 20:09:37 jdolecek Exp $");
     41 
     42 #include "sequencer.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/vnode.h>
     48 #include <sys/select.h>
     49 #include <sys/poll.h>
     50 #include <sys/malloc.h>
     51 #include <sys/proc.h>
     52 #include <sys/systm.h>
     53 #include <sys/syslog.h>
     54 #include <sys/kernel.h>
     55 #include <sys/signalvar.h>
     56 #include <sys/conf.h>
     57 #include <sys/audioio.h>
     58 #include <sys/midiio.h>
     59 #include <sys/device.h>
     60 
     61 #include <dev/midi_if.h>
     62 #include <dev/midivar.h>
     63 #include <dev/sequencervar.h>
     64 
     65 #define ADDTIMEVAL(a, b) ( \
     66 	(a)->tv_sec += (b)->tv_sec, \
     67 	(a)->tv_usec += (b)->tv_usec, \
     68 	(a)->tv_usec > 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\
     69 	)
     70 
     71 #define SUBTIMEVAL(a, b) ( \
     72 	(a)->tv_sec -= (b)->tv_sec, \
     73 	(a)->tv_usec -= (b)->tv_usec, \
     74 	(a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\
     75 	)
     76 
     77 #ifdef AUDIO_DEBUG
     78 #define DPRINTF(x)	if (sequencerdebug) printf x
     79 #define DPRINTFN(n,x)	if (sequencerdebug >= (n)) printf x
     80 int	sequencerdebug = 0;
     81 #else
     82 #define DPRINTF(x)
     83 #define DPRINTFN(n,x)
     84 #endif
     85 
     86 #define SEQ_CMD(b)  ((b)->arr[0])
     87 
     88 #define SEQ_EDEV(b)  ((b)->arr[1])
     89 #define SEQ_ECMD(b)  ((b)->arr[2])
     90 #define SEQ_ECHAN(b) ((b)->arr[3])
     91 #define SEQ_ENOTE(b) ((b)->arr[4])
     92 #define SEQ_EPARM(b) ((b)->arr[5])
     93 
     94 #define SEQ_EP1(b)   ((b)->arr[4])
     95 #define SEQ_EP2(b)   ((b)->arr[5])
     96 
     97 #define SEQ_XCMD(b)  ((b)->arr[1])
     98 #define SEQ_XDEV(b)  ((b)->arr[2])
     99 #define SEQ_XCHAN(b) ((b)->arr[3])
    100 #define SEQ_XNOTE(b) ((b)->arr[4])
    101 #define SEQ_XVEL(b)  ((b)->arr[5])
    102 
    103 #define SEQ_TCMD(b)  ((b)->arr[1])
    104 #define SEQ_TPARM(b) ((b)->arr[4])
    105 
    106 #define SEQ_NOTE_MAX 128
    107 #define SEQ_NOTE_XXX 255
    108 #define SEQ_VEL_OFF 0
    109 
    110 #define RECALC_TICK(t) ((t)->tick = 60 * 1000000L / ((t)->tempo * (t)->timebase))
    111 
    112 struct sequencer_softc seqdevs[NSEQUENCER];
    113 
    114 void sequencerattach __P((int));
    115 void seq_reset __P((struct sequencer_softc *));
    116 int seq_do_command __P((struct sequencer_softc *, seq_event_rec *));
    117 int seq_do_extcommand __P((struct sequencer_softc *, seq_event_rec *));
    118 int seq_do_chnvoice __P((struct sequencer_softc *, seq_event_rec *));
    119 int seq_do_chncommon __P((struct sequencer_softc *, seq_event_rec *));
    120 int seq_do_timing __P((struct sequencer_softc *, seq_event_rec *));
    121 int seq_do_local __P((struct sequencer_softc *, seq_event_rec *));
    122 int seq_do_sysex __P((struct sequencer_softc *, seq_event_rec *));
    123 int seq_do_fullsize __P((struct sequencer_softc *, seq_event_rec *,
    124 			 struct uio *));
    125 int seq_timer __P((struct sequencer_softc *, int, int, seq_event_rec *));
    126 static int seq_input_event __P((struct sequencer_softc *, seq_event_rec *));
    127 int seq_drain __P((struct sequencer_softc *));
    128 void seq_startoutput __P((struct sequencer_softc *));
    129 void seq_timeout __P((void *));
    130 int seq_to_new __P((seq_event_rec *, struct uio *));
    131 static int seq_sleep_timo(int *, char *, int);
    132 static int seq_sleep(int *, char *);
    133 static void seq_wakeup(int *);
    134 
    135 struct midi_softc;
    136 int midiseq_out __P((struct midi_dev *, u_char *, u_int, int));
    137 struct midi_dev *midiseq_open __P((int, int));
    138 void midiseq_close __P((struct midi_dev *));
    139 void midiseq_reset __P((struct midi_dev *));
    140 int midiseq_noteon __P((struct midi_dev *, int, int, int));
    141 int midiseq_noteoff __P((struct midi_dev *, int, int, int));
    142 int midiseq_keypressure __P((struct midi_dev *, int, int, int));
    143 int midiseq_pgmchange __P((struct midi_dev *, int, int));
    144 int midiseq_chnpressure __P((struct midi_dev *, int, int));
    145 int midiseq_ctlchange __P((struct midi_dev *, int, int, int));
    146 int midiseq_pitchbend __P((struct midi_dev *, int, int));
    147 int midiseq_loadpatch __P((struct midi_dev *, struct sysex_info *,
    148 			   struct uio *));
    149 int midiseq_putc __P((struct midi_dev *, int));
    150 void midiseq_in __P((struct midi_dev *, u_char *, int));
    151 
    152 void
    153 sequencerattach(n)
    154 	int n;
    155 {
    156 
    157 	for (n = 0; n < NSEQUENCER; n++)
    158 		callout_init(&seqdevs[n].sc_callout);
    159 }
    160 
    161 int
    162 sequenceropen(dev, flags, ifmt, p)
    163 	dev_t dev;
    164 	int flags, ifmt;
    165 	struct proc *p;
    166 {
    167 	int unit = SEQUENCERUNIT(dev);
    168 	struct sequencer_softc *sc;
    169 	struct midi_dev *md;
    170 	int nmidi;
    171 
    172 	DPRINTF(("sequenceropen\n"));
    173 
    174 	if (unit >= NSEQUENCER)
    175 		return (ENXIO);
    176 	sc = &seqdevs[unit];
    177 	if (sc->isopen)
    178 		return EBUSY;
    179 	if (SEQ_IS_OLD(unit))
    180 		sc->mode = SEQ_OLD;
    181 	else
    182 		sc->mode = SEQ_NEW;
    183 	sc->isopen++;
    184 	sc->flags = flags & (FREAD|FWRITE);
    185 	sc->rchan = 0;
    186 	sc->wchan = 0;
    187 	sc->pbus = 0;
    188 	sc->async = 0;
    189 	sc->input_stamp = ~0;
    190 
    191 	sc->nmidi = 0;
    192 	nmidi = midi_unit_count();
    193 
    194 	sc->devs = malloc(nmidi * sizeof(struct midi_dev *),
    195 			  M_DEVBUF, M_WAITOK);
    196 	for (unit = 0; unit < nmidi; unit++) {
    197 		md = midiseq_open(unit, flags);
    198 		if (md) {
    199 			sc->devs[sc->nmidi++] = md;
    200 			md->seq = sc;
    201 		}
    202 	}
    203 
    204 	sc->timer.timebase = 100;
    205 	sc->timer.tempo = 60;
    206 	sc->doingsysex = 0;
    207 	RECALC_TICK(&sc->timer);
    208 	sc->timer.last = 0;
    209 	microtime(&sc->timer.start);
    210 
    211 	SEQ_QINIT(&sc->inq);
    212 	SEQ_QINIT(&sc->outq);
    213 	sc->lowat = SEQ_MAXQ / 2;
    214 
    215 	seq_reset(sc);
    216 
    217 	DPRINTF(("sequenceropen: mode=%d, nmidi=%d\n", sc->mode, sc->nmidi));
    218 	return 0;
    219 }
    220 
    221 static int
    222 seq_sleep_timo(chan, label, timo)
    223 	int *chan;
    224 	char *label;
    225 	int timo;
    226 {
    227 	int st;
    228 
    229 	if (!label)
    230 		label = "seq";
    231 
    232 	DPRINTFN(5, ("seq_sleep_timo: %p %s %d\n", chan, label, timo));
    233 	*chan = 1;
    234 	st = tsleep(chan, PWAIT | PCATCH, label, timo);
    235 	*chan = 0;
    236 #ifdef MIDI_DEBUG
    237 	if (st != 0)
    238 	    printf("seq_sleep: %d\n", st);
    239 #endif
    240 	return st;
    241 }
    242 
    243 static int
    244 seq_sleep(chan, label)
    245 	int *chan;
    246 	char *label;
    247 {
    248 	return seq_sleep_timo(chan, label, 0);
    249 }
    250 
    251 static void
    252 seq_wakeup(chan)
    253 	int *chan;
    254 {
    255 	if (*chan) {
    256 		DPRINTFN(5, ("seq_wakeup: %p\n", chan));
    257 		wakeup(chan);
    258 		*chan = 0;
    259 	}
    260 }
    261 
    262 int
    263 seq_drain(sc)
    264 	struct sequencer_softc *sc;
    265 {
    266 	int error;
    267 
    268 	DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq)));
    269 	seq_startoutput(sc);
    270 	error = 0;
    271 	while(!SEQ_QEMPTY(&sc->outq) && !error)
    272 		error = seq_sleep_timo(&sc->wchan, "seq_dr", 60*hz);
    273 	return (error);
    274 }
    275 
    276 void
    277 seq_timeout(addr)
    278 	void *addr;
    279 {
    280 	struct sequencer_softc *sc = addr;
    281 	DPRINTFN(4, ("seq_timeout: %p\n", sc));
    282 	sc->timeout = 0;
    283 	seq_startoutput(sc);
    284 	if (SEQ_QLEN(&sc->outq) < sc->lowat) {
    285 		seq_wakeup(&sc->wchan);
    286 		selnotify(&sc->wsel, 0);
    287 		if (sc->async)
    288 			psignal(sc->async, SIGIO);
    289 	}
    290 
    291 }
    292 
    293 void
    294 seq_startoutput(sc)
    295 	struct sequencer_softc *sc;
    296 {
    297 	struct sequencer_queue *q = &sc->outq;
    298 	seq_event_rec cmd;
    299 
    300 	if (sc->timeout)
    301 		return;
    302 	DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q)));
    303 	while(!SEQ_QEMPTY(q) && !sc->timeout) {
    304 		SEQ_QGET(q, cmd);
    305 		seq_do_command(sc, &cmd);
    306 	}
    307 }
    308 
    309 int
    310 sequencerclose(dev, flags, ifmt, p)
    311 	dev_t dev;
    312 	int flags, ifmt;
    313 	struct proc *p;
    314 {
    315 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    316 	int n, s;
    317 
    318 	DPRINTF(("sequencerclose: %p\n", sc));
    319 
    320 	seq_drain(sc);
    321 	s = splaudio();
    322 	if (sc->timeout) {
    323 		callout_stop(&sc->sc_callout);
    324 		sc->timeout = 0;
    325 	}
    326 	splx(s);
    327 
    328 	for (n = 0; n < sc->nmidi; n++)
    329 		midiseq_close(sc->devs[n]);
    330 	free(sc->devs, M_DEVBUF);
    331 	sc->isopen = 0;
    332 	return (0);
    333 }
    334 
    335 static int
    336 seq_input_event(sc, cmd)
    337 	struct sequencer_softc *sc;
    338 	seq_event_rec *cmd;
    339 {
    340 	struct sequencer_queue *q = &sc->inq;
    341 
    342 	DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x %02x %02x %02x\n",
    343 		     cmd->arr[0], cmd->arr[1], cmd->arr[2], cmd->arr[3],
    344 		     cmd->arr[4], cmd->arr[5], cmd->arr[6], cmd->arr[7]));
    345 	if (SEQ_QFULL(q))
    346 		return (ENOMEM);
    347 	SEQ_QPUT(q, *cmd);
    348 	seq_wakeup(&sc->rchan);
    349 	selnotify(&sc->rsel, 0);
    350 	if (sc->async)
    351 		psignal(sc->async, SIGIO);
    352 	return 0;
    353 }
    354 
    355 void
    356 seq_event_intr(addr, iev)
    357 	void *addr;
    358 	seq_event_rec *iev;
    359 {
    360 	struct sequencer_softc *sc = addr;
    361 	union {
    362 		u_int32_t l;
    363 		u_int8_t b[4];
    364 	} u;
    365 	u_long t;
    366 	struct timeval now;
    367 	seq_event_rec ev;
    368 
    369 	microtime(&now);
    370 	SUBTIMEVAL(&now, &sc->timer.start);
    371 	t = now.tv_sec * 1000000 + now.tv_usec;
    372 	t /= sc->timer.tick;
    373 	if (t != sc->input_stamp) {
    374 		ev.arr[0] = SEQ_TIMING;
    375 		ev.arr[1] = TMR_WAIT_ABS;
    376 		ev.arr[2] = 0;
    377 		ev.arr[3] = 0;
    378 		u.l = t;
    379 		ev.arr[4] = u.b[0];
    380 		ev.arr[5] = u.b[1];
    381 		ev.arr[6] = u.b[2];
    382 		ev.arr[7] = u.b[3];
    383 		seq_input_event(sc, &ev);
    384 		sc->input_stamp = t;
    385 	}
    386 	seq_input_event(sc, iev);
    387 }
    388 
    389 int
    390 sequencerread(dev, uio, ioflag)
    391 	dev_t dev;
    392 	struct uio *uio;
    393 	int ioflag;
    394 {
    395 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    396 	struct sequencer_queue *q = &sc->inq;
    397 	seq_event_rec ev;
    398 	int error, s;
    399 
    400 	DPRINTFN(20, ("sequencerread: %p, count=%d, ioflag=%x\n",
    401 		     sc, (int) uio->uio_resid, ioflag));
    402 
    403 	if (sc->mode == SEQ_OLD) {
    404 		DPRINTFN(-1,("sequencerread: old read\n"));
    405 		return (EINVAL); /* XXX unimplemented */
    406 	}
    407 
    408 	error = 0;
    409 	while (SEQ_QEMPTY(q)) {
    410 		if (ioflag & IO_NDELAY)
    411 			return EWOULDBLOCK;
    412 		else {
    413 			error = seq_sleep(&sc->rchan, "seq rd");
    414 			if (error)
    415 				return error;
    416 		}
    417 	}
    418 	s = splaudio();
    419 	while (uio->uio_resid >= sizeof ev && !error && !SEQ_QEMPTY(q)) {
    420 		SEQ_QGET(q, ev);
    421 		error = uiomove(&ev, sizeof ev, uio);
    422 	}
    423 	splx(s);
    424 	return error;
    425 }
    426 
    427 int
    428 sequencerwrite(dev, uio, ioflag)
    429 	dev_t dev;
    430 	struct uio *uio;
    431 	int ioflag;
    432 {
    433 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    434 	struct sequencer_queue *q = &sc->outq;
    435 	int error;
    436 	seq_event_rec cmdbuf;
    437 	int size;
    438 
    439 	DPRINTFN(2, ("sequencerwrite: %p, count=%d\n", sc, (int) uio->uio_resid));
    440 
    441 	error = 0;
    442 	size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE;
    443 	while (uio->uio_resid >= size) {
    444 		error = uiomove(&cmdbuf, size, uio);
    445 		if (error)
    446 			break;
    447 		if (sc->mode == SEQ_OLD)
    448 			if (seq_to_new(&cmdbuf, uio))
    449 				continue;
    450 		if (SEQ_CMD(&cmdbuf) == SEQ_FULLSIZE) {
    451 			/* We do it like OSS does, asynchronously */
    452 			error = seq_do_fullsize(sc, &cmdbuf, uio);
    453 			if (error)
    454 				break;
    455 			continue;
    456 		}
    457 		while (SEQ_QFULL(q)) {
    458 			seq_startoutput(sc);
    459 			if (SEQ_QFULL(q)) {
    460 				if (ioflag & IO_NDELAY)
    461 					return EWOULDBLOCK;
    462 				error = seq_sleep(&sc->wchan, "seq_wr");
    463 				if (error)
    464 					return error;
    465 			}
    466 		}
    467 		SEQ_QPUT(q, cmdbuf);
    468 	}
    469 	seq_startoutput(sc);
    470 
    471 #ifdef SEQUENCER_DEBUG
    472 	if (error)
    473 		DPRINTFN(2, ("sequencerwrite: error=%d\n", error));
    474 #endif
    475 	return error;
    476 }
    477 
    478 int
    479 sequencerioctl(dev, cmd, addr, flag, p)
    480 	dev_t dev;
    481 	u_long cmd;
    482 	caddr_t addr;
    483 	int flag;
    484 	struct proc *p;
    485 {
    486 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    487 	struct synth_info *si;
    488 	struct midi_dev *md;
    489 	int devno;
    490 	int error;
    491 	int t;
    492 
    493 	DPRINTFN(2, ("sequencerioctl: %p cmd=0x%08lx\n", sc, cmd));
    494 
    495 	error = 0;
    496 	switch (cmd) {
    497 	case FIONBIO:
    498 		/* All handled in the upper FS layer. */
    499 		break;
    500 
    501 	case FIOASYNC:
    502 		if (*(int *)addr) {
    503 			if (sc->async)
    504 				return EBUSY;
    505 			sc->async = p;
    506 			DPRINTF(("sequencer_ioctl: FIOASYNC %p\n", p));
    507 		} else
    508 			sc->async = 0;
    509 		break;
    510 
    511 	case SEQUENCER_RESET:
    512 		seq_reset(sc);
    513 		break;
    514 
    515 	case SEQUENCER_PANIC:
    516 		seq_reset(sc);
    517 		/* Do more?  OSS doesn't */
    518 		break;
    519 
    520 	case SEQUENCER_SYNC:
    521 		if (sc->flags == FREAD)
    522 			return 0;
    523 		seq_drain(sc);
    524 		error = 0;
    525 		break;
    526 
    527 	case SEQUENCER_INFO:
    528 		si = (struct synth_info*)addr;
    529 		devno = si->device;
    530 		if (devno < 0 || devno >= sc->nmidi)
    531 			return EINVAL;
    532 		md = sc->devs[devno];
    533 		strncpy(si->name, md->name, sizeof si->name);
    534 		si->synth_type = SYNTH_TYPE_MIDI;
    535 		si->synth_subtype = md->subtype;
    536 		si->nr_voices = md->nr_voices;
    537 		si->instr_bank_size = md->instr_bank_size;
    538 		si->capabilities = md->capabilities;
    539 		break;
    540 
    541 	case SEQUENCER_NRSYNTHS:
    542 		*(int *)addr = sc->nmidi;
    543 		break;
    544 
    545 	case SEQUENCER_NRMIDIS:
    546 		*(int *)addr = sc->nmidi;
    547 		break;
    548 
    549 	case SEQUENCER_OUTOFBAND:
    550 		DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
    551 			     *(u_char *)addr, *(u_char *)(addr+1),
    552 			     *(u_char *)(addr+2), *(u_char *)(addr+3),
    553 			     *(u_char *)(addr+4), *(u_char *)(addr+5),
    554 			     *(u_char *)(addr+6), *(u_char *)(addr+7)));
    555 		error = seq_do_command(sc, (seq_event_rec *)addr);
    556 		break;
    557 
    558 	case SEQUENCER_TMR_TIMEBASE:
    559 		t = *(int *)addr;
    560 		if (t < 1)
    561 			t = 1;
    562 		if (t > 10000)
    563 			t = 10000;
    564 		sc->timer.timebase = t;
    565 		*(int *)addr = t;
    566 		RECALC_TICK(&sc->timer);
    567 		break;
    568 
    569 	case SEQUENCER_TMR_START:
    570 		error = seq_timer(sc, TMR_START, 0, 0);
    571 		break;
    572 
    573 	case SEQUENCER_TMR_STOP:
    574 		error = seq_timer(sc, TMR_STOP, 0, 0);
    575 		break;
    576 
    577 	case SEQUENCER_TMR_CONTINUE:
    578 		error = seq_timer(sc, TMR_CONTINUE, 0, 0);
    579 		break;
    580 
    581 	case SEQUENCER_TMR_TEMPO:
    582 		t = *(int *)addr;
    583 		if (t < 8)
    584 			t = 8;
    585 		if (t > 250)
    586 			t = 250;
    587 		sc->timer.tempo = t;
    588 		*(int *)addr = t;
    589 		RECALC_TICK(&sc->timer);
    590 		break;
    591 
    592 	case SEQUENCER_TMR_SOURCE:
    593 		*(int *)addr = SEQUENCER_TMR_INTERNAL;
    594 		break;
    595 
    596 	case SEQUENCER_TMR_METRONOME:
    597 		/* noop */
    598 		break;
    599 
    600 	case SEQUENCER_THRESHOLD:
    601 		t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec);
    602 		if (t < 1)
    603 			t = 1;
    604 		if (t > SEQ_MAXQ)
    605 			t = SEQ_MAXQ;
    606 		sc->lowat = t;
    607 		break;
    608 
    609 	case SEQUENCER_CTRLRATE:
    610 		*(int *)addr = (sc->timer.tempo*sc->timer.timebase + 30) / 60;
    611 		break;
    612 
    613 	case SEQUENCER_GETTIME:
    614 	{
    615 		struct timeval now;
    616 		u_long t;
    617 		microtime(&now);
    618 		SUBTIMEVAL(&now, &sc->timer.start);
    619 		t = now.tv_sec * 1000000 + now.tv_usec;
    620 		t /= sc->timer.tick;
    621 		*(int *)addr = t;
    622 		break;
    623 	}
    624 
    625 	default:
    626 		DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd));
    627 		error = EINVAL;
    628 		break;
    629 	}
    630 	return error;
    631 }
    632 
    633 int
    634 sequencerpoll(dev, events, p)
    635 	dev_t dev;
    636 	int events;
    637 	struct proc *p;
    638 {
    639 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    640 	int revents = 0;
    641 
    642 	DPRINTF(("sequencerpoll: %p events=0x%x\n", sc, events));
    643 
    644 	if (events & (POLLIN | POLLRDNORM))
    645 		if (!SEQ_QEMPTY(&sc->inq))
    646 			revents |= events & (POLLIN | POLLRDNORM);
    647 
    648 	if (events & (POLLOUT | POLLWRNORM))
    649 		if (SEQ_QLEN(&sc->outq) < sc->lowat)
    650 			revents |= events & (POLLOUT | POLLWRNORM);
    651 
    652 	if (revents == 0) {
    653 		if (events & (POLLIN | POLLRDNORM))
    654 			selrecord(p, &sc->rsel);
    655 
    656 		if (events & (POLLOUT | POLLWRNORM))
    657 			selrecord(p, &sc->wsel);
    658 	}
    659 
    660 	return revents;
    661 }
    662 
    663 static void
    664 filt_sequencerrdetach(struct knote *kn)
    665 {
    666 	struct sequencer_softc *sc = (void *) kn->kn_hook;
    667 	int s;
    668 
    669 	s = splaudio();
    670 	SLIST_REMOVE(&sc->rsel.si_klist, kn, knote, kn_selnext);
    671 	splx(s);
    672 }
    673 
    674 static int
    675 filt_sequencerread(struct knote *kn, long hint)
    676 {
    677 	struct sequencer_softc *sc = (void *) kn->kn_hook;
    678 
    679 	/* XXXLUKEM (thorpej): make sure this is correct */
    680 
    681 	if (SEQ_QEMPTY(&sc->inq))
    682 		return (0);
    683 	kn->kn_data = sizeof(seq_event_rec);
    684 	return (1);
    685 }
    686 
    687 static const struct filterops sequencerread_filtops =
    688 	{ 1, NULL, filt_sequencerrdetach, filt_sequencerread };
    689 
    690 static void
    691 filt_sequencerwdetach(struct knote *kn)
    692 {
    693 	struct sequencer_softc *sc = (void *) kn->kn_hook;
    694 	int s;
    695 
    696 	s = splaudio();
    697 	SLIST_REMOVE(&sc->wsel.si_klist, kn, knote, kn_selnext);
    698 	splx(s);
    699 }
    700 
    701 static int
    702 filt_sequencerwrite(struct knote *kn, long hint)
    703 {
    704 	struct sequencer_softc *sc = (void *) kn->kn_hook;
    705 
    706 	/* XXXLUKEM (thorpej): make sure this is correct */
    707 
    708 	if (SEQ_QLEN(&sc->outq) >= sc->lowat)
    709 		return (0);
    710 	kn->kn_data = sizeof(seq_event_rec);
    711 	return (1);
    712 }
    713 
    714 static const struct filterops sequencerwrite_filtops =
    715 	{ 1, NULL, filt_sequencerwdetach, filt_sequencerwrite };
    716 
    717 int
    718 sequencerkqfilter(dev_t dev, struct knote *kn)
    719 {
    720 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    721 	struct klist *klist;
    722 	int s;
    723 
    724 	switch (kn->kn_filter) {
    725 	case EVFILT_READ:
    726 		klist = &sc->rsel.si_klist;
    727 		kn->kn_fop = &sequencerread_filtops;
    728 		break;
    729 
    730 	case EVFILT_WRITE:
    731 		klist = &sc->wsel.si_klist;
    732 		kn->kn_fop = &sequencerwrite_filtops;
    733 		break;
    734 
    735 	default:
    736 		return (1);
    737 	}
    738 
    739 	kn->kn_hook = (void *) sc;
    740 
    741 	s = splaudio();
    742 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    743 	splx(s);
    744 
    745 	return (0);
    746 }
    747 
    748 void
    749 seq_reset(sc)
    750 	struct sequencer_softc *sc;
    751 {
    752 	int i, chn;
    753 	struct midi_dev *md;
    754 
    755 	for (i = 0; i < sc->nmidi; i++) {
    756 		md = sc->devs[i];
    757 		midiseq_reset(md);
    758 		for (chn = 0; chn < MAXCHAN; chn++) {
    759 			midiseq_ctlchange(md, chn, MIDI_CTRL_ALLOFF, 0);
    760 			midiseq_ctlchange(md, chn, MIDI_CTRL_RESET, 0);
    761 			midiseq_pitchbend(md, chn, MIDI_BEND_NEUTRAL);
    762 		}
    763 	}
    764 }
    765 
    766 int
    767 seq_do_command(sc, b)
    768 	struct sequencer_softc *sc;
    769 	seq_event_rec *b;
    770 {
    771 	int dev;
    772 
    773 	DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, SEQ_CMD(b)));
    774 
    775 	switch(SEQ_CMD(b)) {
    776 	case SEQ_LOCAL:
    777 		return seq_do_local(sc, b);
    778 	case SEQ_TIMING:
    779 		return seq_do_timing(sc, b);
    780 	case SEQ_CHN_VOICE:
    781 		return seq_do_chnvoice(sc, b);
    782 	case SEQ_CHN_COMMON:
    783 		return seq_do_chncommon(sc, b);
    784 	case SEQ_SYSEX:
    785 		return seq_do_sysex(sc, b);
    786 	/* COMPAT */
    787 	case SEQOLD_MIDIPUTC:
    788 		dev = b->arr[2];
    789 		if (dev < 0 || dev >= sc->nmidi)
    790 			return (ENXIO);
    791 		return midiseq_putc(sc->devs[dev], b->arr[1]);
    792 	default:
    793 		DPRINTFN(-1,("seq_do_command: unimpl command %02x\n",
    794 			     SEQ_CMD(b)));
    795 		return (EINVAL);
    796 	}
    797 }
    798 
    799 int
    800 seq_do_chnvoice(sc, b)
    801 	struct sequencer_softc *sc;
    802 	seq_event_rec *b;
    803 {
    804 	int cmd, dev, chan, note, parm, voice;
    805 	int error;
    806 	struct midi_dev *md;
    807 
    808 	dev = SEQ_EDEV(b);
    809 	if (dev < 0 || dev >= sc->nmidi)
    810 		return ENXIO;
    811 	md = sc->devs[dev];
    812 	cmd = SEQ_ECMD(b);
    813 	chan = SEQ_ECHAN(b);
    814 	note = SEQ_ENOTE(b);
    815 	parm = SEQ_EPARM(b);
    816 	DPRINTFN(2,("seq_do_chnvoice: cmd=%02x dev=%d chan=%d note=%d parm=%d\n",
    817 		    cmd, dev, chan, note, parm));
    818 	voice = chan;
    819 	if (cmd == MIDI_NOTEON && parm == 0) {
    820 		cmd = MIDI_NOTEOFF;
    821 		parm = MIDI_HALF_VEL;
    822 	}
    823 	switch(cmd) {
    824 	case MIDI_NOTEON:
    825 		DPRINTFN(5, ("seq_do_chnvoice: noteon %p %d %d %d\n",
    826 			     md, voice, note, parm));
    827 		error = midiseq_noteon(md, voice, note, parm);
    828 		break;
    829 	case MIDI_NOTEOFF:
    830 		error = midiseq_noteoff(md, voice, note, parm);
    831 		break;
    832 	case MIDI_KEY_PRESSURE:
    833 		error = midiseq_keypressure(md, voice, note, parm);
    834 		break;
    835 	default:
    836 		DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n", cmd));
    837 		error = EINVAL;
    838 		break;
    839 	}
    840 	return error;
    841 }
    842 
    843 int
    844 seq_do_chncommon(sc, b)
    845 	struct sequencer_softc *sc;
    846 	seq_event_rec *b;
    847 {
    848 	int cmd, dev, chan, p1, w14;
    849 	int error;
    850 	struct midi_dev *md;
    851 	union {
    852 		int16_t s;
    853 		u_int8_t b[2];
    854 	} u;
    855 
    856 	dev = SEQ_EDEV(b);
    857 	if (dev < 0 || dev >= sc->nmidi)
    858 		return ENXIO;
    859 	md = sc->devs[dev];
    860 	cmd = SEQ_ECMD(b);
    861 	chan = SEQ_ECHAN(b);
    862 	p1 = SEQ_EP1(b);
    863 	u.b[0] = b->arr[6];
    864 	u.b[1] = b->arr[7];
    865 	w14 = u.s;
    866 	DPRINTFN(2,("seq_do_chncommon: %02x\n", cmd));
    867 
    868 	error = 0;
    869 	switch(cmd) {
    870 	case MIDI_PGM_CHANGE:
    871 		error = midiseq_pgmchange(md, chan, p1);
    872 		break;
    873 	case MIDI_CTL_CHANGE:
    874 		if (chan > 15 || p1 > 127)
    875 			return 0; /* EINVAL */
    876 		error = midiseq_ctlchange(md, chan, p1, w14);
    877 		break;
    878 	case MIDI_PITCH_BEND:
    879 		error = midiseq_pitchbend(md, chan, w14);
    880 		break;
    881 	case MIDI_CHN_PRESSURE:
    882 		error = midiseq_chnpressure(md, chan, p1);
    883 		break;
    884 	default:
    885 		DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n", cmd));
    886 		error = EINVAL;
    887 		break;
    888 	}
    889 	return (error);
    890 }
    891 
    892 int
    893 seq_do_timing(sc, b)
    894 	struct sequencer_softc *sc;
    895 	seq_event_rec *b;
    896 {
    897 	union {
    898 		int32_t i;
    899 		u_int8_t b[4];
    900 	} u;
    901 	u.b[0] = b->arr[4];
    902 	u.b[1] = b->arr[5];
    903 	u.b[2] = b->arr[6];
    904 	u.b[3] = b->arr[7];
    905 	return seq_timer(sc, SEQ_TCMD(b), u.i, b);
    906 }
    907 
    908 int
    909 seq_do_local(sc, b)
    910 	struct sequencer_softc *sc;
    911 	seq_event_rec *b;
    912 {
    913 	return (EINVAL);
    914 }
    915 
    916 int
    917 seq_do_sysex(sc, b)
    918 	struct sequencer_softc *sc;
    919 	seq_event_rec *b;
    920 {
    921 	int dev, i;
    922 	struct midi_dev *md;
    923 	u_int8_t c, *buf = &b->arr[2];
    924 
    925 	dev = SEQ_EDEV(b);
    926 	if (dev < 0 || dev >= sc->nmidi)
    927 		return (ENXIO);
    928 	DPRINTF(("seq_do_sysex: dev=%d\n", dev));
    929 	md = sc->devs[dev];
    930 
    931 	if (!sc->doingsysex) {
    932 		c = MIDI_SYSEX_START;
    933 		midiseq_out(md, &c, 1, 0);
    934 		sc->doingsysex = 1;
    935 	}
    936 
    937 	for (i = 0; i < 6 && buf[i] != 0xff; i++)
    938 		;
    939 	midiseq_out(md, buf, i, 0);
    940 	if (i < 6 || (i > 0 && buf[i-1] == MIDI_SYSEX_END))
    941 		sc->doingsysex = 0;
    942 	return (0);
    943 }
    944 
    945 int
    946 seq_timer(sc, cmd, parm, b)
    947 	struct sequencer_softc *sc;
    948 	int cmd, parm;
    949 	seq_event_rec *b;
    950 {
    951 	struct syn_timer *t = &sc->timer;
    952 	struct timeval when;
    953 	int ticks;
    954 	int error;
    955 	long long usec;
    956 
    957 	DPRINTFN(2,("seq_timer: %02x %d\n", cmd, parm));
    958 
    959 	error = 0;
    960 	switch(cmd) {
    961 	case TMR_WAIT_REL:
    962 		parm += t->last;
    963 		/* fall into */
    964 	case TMR_WAIT_ABS:
    965 		t->last = parm;
    966 		usec = (long long)parm * (long long)t->tick; /* convert to usec */
    967 		when.tv_sec = usec / 1000000;
    968 		when.tv_usec = usec % 1000000;
    969 		DPRINTFN(4, ("seq_timer: parm=%d, sleep when=%ld.%06ld", parm,
    970 			     when.tv_sec, when.tv_usec));
    971 		ADDTIMEVAL(&when, &t->start); /* abstime for end */
    972 		ticks = hzto(&when);
    973 		DPRINTFN(4, (" when+start=%ld.%06ld, tick=%d\n",
    974 			     when.tv_sec, when.tv_usec, ticks));
    975 		if (ticks > 0) {
    976 #ifdef DIAGNOSTIC
    977 			if (ticks > 20 * hz) {
    978 				/* Waiting more than 20s */
    979 				printf("seq_timer: funny ticks=%d, usec=%lld, parm=%d, tick=%ld\n",
    980 				       ticks, usec, parm, t->tick);
    981 			}
    982 #endif
    983 			sc->timeout = 1;
    984 			callout_reset(&sc->sc_callout, ticks,
    985 			    seq_timeout, sc);
    986 		}
    987 #ifdef SEQUENCER_DEBUG
    988 		else if (tick < 0)
    989 			DPRINTF(("seq_timer: ticks = %d\n", ticks));
    990 #endif
    991 		break;
    992 	case TMR_START:
    993 		microtime(&t->start);
    994 		t->running = 1;
    995 		break;
    996 	case TMR_STOP:
    997 		microtime(&t->stop);
    998 		t->running = 0;
    999 		break;
   1000 	case TMR_CONTINUE:
   1001 		microtime(&when);
   1002 		SUBTIMEVAL(&when, &t->stop);
   1003 		ADDTIMEVAL(&t->start, &when);
   1004 		t->running = 1;
   1005 		break;
   1006 	case TMR_TEMPO:
   1007 		/* parm is ticks per minute / timebase */
   1008 		if (parm < 8)
   1009 			parm = 8;
   1010 		if (parm > 360)
   1011 			parm = 360;
   1012 		t->tempo = parm;
   1013 		RECALC_TICK(t);
   1014 		break;
   1015 	case TMR_ECHO:
   1016 		error = seq_input_event(sc, b);
   1017 		break;
   1018 	case TMR_RESET:
   1019 		t->last = 0;
   1020 		microtime(&t->start);
   1021 		break;
   1022 	default:
   1023 		DPRINTF(("seq_timer: unknown %02x\n", cmd));
   1024 		error = EINVAL;
   1025 		break;
   1026 	}
   1027 	return (error);
   1028 }
   1029 
   1030 int
   1031 seq_do_fullsize(sc, b, uio)
   1032 	struct sequencer_softc *sc;
   1033 	seq_event_rec *b;
   1034 	struct uio *uio;
   1035 {
   1036 	struct sysex_info sysex;
   1037 	u_int dev;
   1038 
   1039 #ifdef DIAGNOSTIC
   1040 	if (sizeof(seq_event_rec) != SEQ_SYSEX_HDRSIZE) {
   1041 		printf("seq_do_fullsize: sysex size ??\n");
   1042 		return EINVAL;
   1043 	}
   1044 #endif
   1045 	memcpy(&sysex, b, sizeof sysex);
   1046 	dev = sysex.device_no;
   1047 	DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
   1048 		     sysex.key, dev, sysex.len));
   1049 	return (midiseq_loadpatch(sc->devs[dev], &sysex, uio));
   1050 }
   1051 
   1052 /* Convert an old sequencer event to a new one. */
   1053 int
   1054 seq_to_new(ev, uio)
   1055 	seq_event_rec *ev;
   1056 	struct uio *uio;
   1057 {
   1058 	int cmd, chan, note, parm;
   1059 	u_int32_t delay;
   1060 	int error;
   1061 
   1062 	cmd = SEQ_CMD(ev);
   1063 	chan = ev->arr[1];
   1064 	note = ev->arr[2];
   1065 	parm = ev->arr[3];
   1066 	DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm));
   1067 
   1068 	if (cmd >= 0x80) {
   1069 		/* Fill the event record */
   1070 		if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) {
   1071 			error = uiomove(&ev->arr[SEQOLD_CMDSIZE],
   1072 					sizeof *ev - SEQOLD_CMDSIZE, uio);
   1073 			if (error)
   1074 				return error;
   1075 		} else
   1076 			return EINVAL;
   1077 	}
   1078 
   1079 	switch(cmd) {
   1080 	case SEQOLD_NOTEOFF:
   1081 		note = 255;
   1082 		SEQ_ECMD(ev) = MIDI_NOTEOFF;
   1083 		goto onoff;
   1084 	case SEQOLD_NOTEON:
   1085 		SEQ_ECMD(ev) = MIDI_NOTEON;
   1086 	onoff:
   1087 		SEQ_CMD(ev) = SEQ_CHN_VOICE;
   1088 		SEQ_EDEV(ev) = 0;
   1089 		SEQ_ECHAN(ev) = chan;
   1090 		SEQ_ENOTE(ev) = note;
   1091 		SEQ_EPARM(ev) = parm;
   1092 		break;
   1093 	case SEQOLD_WAIT:
   1094 		delay = *(u_int32_t *)ev->arr >> 8;
   1095 		SEQ_CMD(ev) = SEQ_TIMING;
   1096 		SEQ_TCMD(ev) = TMR_WAIT_REL;
   1097 		*(u_int32_t *)&ev->arr[4] = delay;
   1098 		break;
   1099 	case SEQOLD_SYNCTIMER:
   1100 		SEQ_CMD(ev) = SEQ_TIMING;
   1101 		SEQ_TCMD(ev) = TMR_RESET;
   1102 		break;
   1103 	case SEQOLD_PGMCHANGE:
   1104 		SEQ_ECMD(ev) = MIDI_PGM_CHANGE;
   1105 		SEQ_CMD(ev) = SEQ_CHN_COMMON;
   1106 		SEQ_EDEV(ev) = 0;
   1107 		SEQ_ECHAN(ev) = chan;
   1108 		SEQ_EP1(ev) = note;
   1109 		break;
   1110 	case SEQOLD_MIDIPUTC:
   1111 		break;		/* interpret in normal mode */
   1112 	case SEQOLD_ECHO:
   1113 	case SEQOLD_PRIVATE:
   1114 	case SEQOLD_EXTENDED:
   1115 	default:
   1116 		DPRINTF(("seq_to_new: not impl 0x%02x\n", cmd));
   1117 		return EINVAL;
   1118 	/* In case new events show up */
   1119 	case SEQ_TIMING:
   1120 	case SEQ_CHN_VOICE:
   1121 	case SEQ_CHN_COMMON:
   1122 	case SEQ_FULLSIZE:
   1123 		break;
   1124 	}
   1125 	return 0;
   1126 }
   1127 
   1128 /**********************************************/
   1129 
   1130 void
   1131 midiseq_in(md, msg, len)
   1132 	struct midi_dev *md;
   1133 	u_char *msg;
   1134 	int len;
   1135 {
   1136 	int unit = md->unit;
   1137 	seq_event_rec ev;
   1138 	int status, chan;
   1139 
   1140 	DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
   1141 		     md, msg[0], msg[1], msg[2]));
   1142 
   1143 	status = MIDI_GET_STATUS(msg[0]);
   1144 	chan = MIDI_GET_CHAN(msg[0]);
   1145 	switch (status) {
   1146 	case MIDI_NOTEON:
   1147 		if (msg[2] == 0) {
   1148 			status = MIDI_NOTEOFF;
   1149 			msg[2] = MIDI_HALF_VEL;
   1150 		}
   1151 		/* fall into */
   1152 	case MIDI_NOTEOFF:
   1153 	case MIDI_KEY_PRESSURE:
   1154 		SEQ_MK_CHN_VOICE(&ev, unit, status, chan, msg[1], msg[2]);
   1155 		break;
   1156 	case MIDI_CTL_CHANGE:
   1157 		SEQ_MK_CHN_COMMON(&ev, unit, status, chan, msg[1], 0, msg[2]);
   1158 		break;
   1159 	case MIDI_PGM_CHANGE:
   1160 	case MIDI_CHN_PRESSURE:
   1161 		SEQ_MK_CHN_COMMON(&ev, unit, status, chan, msg[1], 0, 0);
   1162 		break;
   1163 	case MIDI_PITCH_BEND:
   1164 		SEQ_MK_CHN_COMMON(&ev, unit, status, chan, 0, 0,
   1165 				  (msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7));
   1166 		break;
   1167 	default:
   1168 		return;
   1169 	}
   1170 	seq_event_intr(md->seq, &ev);
   1171 }
   1172 
   1173 struct midi_dev *
   1174 midiseq_open(unit, flags)
   1175 	int unit;
   1176 	int flags;
   1177 {
   1178 	extern struct cfdriver midi_cd;
   1179 	int error;
   1180 	struct midi_dev *md;
   1181 	struct midi_softc *sc;
   1182 	struct midi_info mi;
   1183 
   1184 	DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags));
   1185 	error = midiopen(makedev(0, unit), flags, 0, 0);
   1186 	if (error)
   1187 		return (0);
   1188 	sc = midi_cd.cd_devs[unit];
   1189 	sc->seqopen = 1;
   1190 	md = malloc(sizeof *md, M_DEVBUF, M_WAITOK|M_ZERO);
   1191 	sc->seq_md = md;
   1192 	md->msc = sc;
   1193 	midi_getinfo(makedev(0, unit), &mi);
   1194 	md->unit = unit;
   1195 	md->name = mi.name;
   1196 	md->subtype = 0;
   1197 	md->nr_voices = 128;	/* XXX */
   1198 	md->instr_bank_size = 128; /* XXX */
   1199 	if (mi.props & MIDI_PROP_CAN_INPUT)
   1200 		md->capabilities |= SYNTH_CAP_INPUT;
   1201 	return (md);
   1202 }
   1203 
   1204 void
   1205 midiseq_close(md)
   1206 	struct midi_dev *md;
   1207 {
   1208 	DPRINTFN(2, ("midiseq_close: %d\n", md->unit));
   1209 	midiclose(makedev(0, md->unit), 0, 0, 0);
   1210 	free(md, M_DEVBUF);
   1211 }
   1212 
   1213 void
   1214 midiseq_reset(md)
   1215 	struct midi_dev *md;
   1216 {
   1217 	/* XXX send GM reset? */
   1218 	DPRINTFN(3, ("midiseq_reset: %d\n", md->unit));
   1219 }
   1220 
   1221 int
   1222 midiseq_out(md, buf, cc, chk)
   1223 	struct midi_dev *md;
   1224 	u_char *buf;
   1225 	u_int cc;
   1226 	int chk;
   1227 {
   1228 	DPRINTFN(5, ("midiseq_out: m=%p, unit=%d, buf[0]=0x%02x, cc=%d\n",
   1229 		     md->msc, md->unit, buf[0], cc));
   1230 
   1231 	/* The MIDI "status" byte does not have to be repeated. */
   1232 	if (chk && md->last_cmd == buf[0])
   1233 		buf++, cc--;
   1234 	else
   1235 		md->last_cmd = buf[0];
   1236 	return midi_writebytes(md->unit, buf, cc);
   1237 }
   1238 
   1239 int
   1240 midiseq_noteon(md, chan, note, vel)
   1241 	struct midi_dev *md;
   1242 	int chan, note, vel;
   1243 {
   1244 	u_char buf[3];
   1245 
   1246 	DPRINTFN(6, ("midiseq_noteon 0x%02x %d %d\n",
   1247 		     MIDI_NOTEON | chan, note, vel));
   1248 	if (chan < 0 || chan > 15 ||
   1249 	    note < 0 || note > 127)
   1250 		return EINVAL;
   1251 	if (vel < 0) vel = 0;
   1252 	if (vel > 127) vel = 127;
   1253 	buf[0] = MIDI_NOTEON | chan;
   1254 	buf[1] = note;
   1255 	buf[2] = vel;
   1256 	return midiseq_out(md, buf, 3, 1);
   1257 }
   1258 
   1259 int
   1260 midiseq_noteoff(md, chan, note, vel)
   1261 	struct midi_dev *md;
   1262 	int chan, note, vel;
   1263 {
   1264 	u_char buf[3];
   1265 
   1266 	if (chan < 0 || chan > 15 ||
   1267 	    note < 0 || note > 127)
   1268 		return EINVAL;
   1269 	if (vel < 0) vel = 0;
   1270 	if (vel > 127) vel = 127;
   1271 	buf[0] = MIDI_NOTEOFF | chan;
   1272 	buf[1] = note;
   1273 	buf[2] = vel;
   1274 	return midiseq_out(md, buf, 3, 1);
   1275 }
   1276 
   1277 int
   1278 midiseq_keypressure(md, chan, note, vel)
   1279 	struct midi_dev *md;
   1280 	int chan, note, vel;
   1281 {
   1282 	u_char buf[3];
   1283 
   1284 	if (chan < 0 || chan > 15 ||
   1285 	    note < 0 || note > 127)
   1286 		return EINVAL;
   1287 	if (vel < 0) vel = 0;
   1288 	if (vel > 127) vel = 127;
   1289 	buf[0] = MIDI_KEY_PRESSURE | chan;
   1290 	buf[1] = note;
   1291 	buf[2] = vel;
   1292 	return midiseq_out(md, buf, 3, 1);
   1293 }
   1294 
   1295 int
   1296 midiseq_pgmchange(md, chan, parm)
   1297 	struct midi_dev *md;
   1298 	int chan, parm;
   1299 {
   1300 	u_char buf[2];
   1301 
   1302 	if (chan < 0 || chan > 15 ||
   1303 	    parm < 0 || parm > 127)
   1304 		return EINVAL;
   1305 	buf[0] = MIDI_PGM_CHANGE | chan;
   1306 	buf[1] = parm;
   1307 	return midiseq_out(md, buf, 2, 1);
   1308 }
   1309 
   1310 int
   1311 midiseq_chnpressure(md, chan, parm)
   1312 	struct midi_dev *md;
   1313 	int chan, parm;
   1314 {
   1315 	u_char buf[2];
   1316 
   1317 	if (chan < 0 || chan > 15 ||
   1318 	    parm < 0 || parm > 127)
   1319 		return EINVAL;
   1320 	buf[0] = MIDI_CHN_PRESSURE | chan;
   1321 	buf[1] = parm;
   1322 	return midiseq_out(md, buf, 2, 1);
   1323 }
   1324 
   1325 int
   1326 midiseq_ctlchange(md, chan, parm, w14)
   1327 	struct midi_dev *md;
   1328 	int chan, parm, w14;
   1329 {
   1330 	u_char buf[3];
   1331 
   1332 	if (chan < 0 || chan > 15 ||
   1333 	    parm < 0 || parm > 127)
   1334 		return EINVAL;
   1335 	buf[0] = MIDI_CTL_CHANGE | chan;
   1336 	buf[1] = parm;
   1337 	buf[2] = w14 & 0x7f;
   1338 	return midiseq_out(md, buf, 3, 1);
   1339 }
   1340 
   1341 int
   1342 midiseq_pitchbend(md, chan, parm)
   1343 	struct midi_dev *md;
   1344 	int chan, parm;
   1345 {
   1346 	u_char buf[3];
   1347 
   1348 	if (chan < 0 || chan > 15)
   1349 		return EINVAL;
   1350 	buf[0] = MIDI_PITCH_BEND | chan;
   1351 	buf[1] = parm & 0x7f;
   1352 	buf[2] = (parm >> 7) & 0x7f;
   1353 	return midiseq_out(md, buf, 3, 1);
   1354 }
   1355 
   1356 int
   1357 midiseq_loadpatch(md, sysex, uio)
   1358 	struct midi_dev *md;
   1359 	struct sysex_info *sysex;
   1360 	struct uio *uio;
   1361 {
   1362 	u_char c, buf[128];
   1363 	int i, cc, error;
   1364 
   1365 	if (sysex->key != SEQ_SYSEX_PATCH) {
   1366 		DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n",
   1367 			     sysex->key));
   1368 		return (EINVAL);
   1369 	}
   1370 	if (uio->uio_resid < sysex->len)
   1371 		/* adjust length, should be an error */
   1372 		sysex->len = uio->uio_resid;
   1373 
   1374 	DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len));
   1375 	if (sysex->len == 0)
   1376 		return EINVAL;
   1377 	error = uiomove(&c, 1, uio);
   1378 	if (error)
   1379 		return error;
   1380 	if (c != MIDI_SYSEX_START)		/* must start like this */
   1381 		return EINVAL;
   1382 	error = midiseq_out(md, &c, 1, 0);
   1383 	if (error)
   1384 		return error;
   1385 	--sysex->len;
   1386 	while (sysex->len > 0) {
   1387 		cc = sysex->len;
   1388 		if (cc > sizeof buf)
   1389 			cc = sizeof buf;
   1390 		error = uiomove(buf, cc, uio);
   1391 		if (error)
   1392 			break;
   1393 		for(i = 0; i < cc && !MIDI_IS_STATUS(buf[i]); i++)
   1394 			;
   1395 		error = midiseq_out(md, buf, i, 0);
   1396 		if (error)
   1397 			break;
   1398 		sysex->len -= i;
   1399 		if (i != cc)
   1400 			break;
   1401 	}
   1402 	/* Any leftover data in uio is rubbish;
   1403 	 * the SYSEX should be one write ending in SYSEX_END.
   1404 	 */
   1405 	uio->uio_resid = 0;
   1406 	c = MIDI_SYSEX_END;
   1407 	return midiseq_out(md, &c, 1, 0);
   1408 }
   1409 
   1410 int
   1411 midiseq_putc(md, data)
   1412 	struct midi_dev *md;
   1413 	int data;
   1414 {
   1415 	u_char c = data;
   1416 	DPRINTFN(4,("midiseq_putc: 0x%02x\n", data));
   1417 	return midiseq_out(md, &c, 1, 0);
   1418 }
   1419 
   1420 #include "midi.h"
   1421 #if NMIDI == 0
   1422 /*
   1423  * If someone has a sequencer, but no midi devices there will
   1424  * be unresolved references, so we provide little stubs.
   1425  */
   1426 
   1427 int
   1428 midi_unit_count()
   1429 {
   1430 	return (0);
   1431 }
   1432 
   1433 int
   1434 midiopen(dev, flags, ifmt, p)
   1435 	dev_t dev;
   1436 	int flags, ifmt;
   1437 	struct proc *p;
   1438 {
   1439 	return (ENXIO);
   1440 }
   1441 
   1442 struct cfdriver midi_cd;
   1443 
   1444 void
   1445 midi_getinfo(dev, mi)
   1446 	dev_t dev;
   1447 	struct midi_info *mi;
   1448 {
   1449 }
   1450 
   1451 int
   1452 midiclose(dev, flags, ifmt, p)
   1453 	dev_t dev;
   1454 	int flags, ifmt;
   1455 	struct proc *p;
   1456 {
   1457 	return (ENXIO);
   1458 }
   1459 
   1460 int
   1461 midi_writebytes(unit, buf, cc)
   1462 	int unit;
   1463 	u_char *buf;
   1464 	int cc;
   1465 {
   1466 	return (ENXIO);
   1467 }
   1468 #endif /* NMIDI == 0 */
   1469