Home | History | Annotate | Line # | Download | only in dev
sequencer.c revision 1.30.14.19
      1 /*	$NetBSD: sequencer.c,v 1.30.14.19 2006/05/25 21:05:29 chap 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.30.14.19 2006/05/25 21:05:29 chap 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_NOTE_MAX 128
     87 #define SEQ_NOTE_XXX 255
     88 
     89 #define RECALC_TICK(t) ((t)->tick = 60 * 1000000L / ((t)->tempo * (t)->timebase))
     90 
     91 struct sequencer_softc seqdevs[NSEQUENCER];
     92 
     93 void sequencerattach(int);
     94 static void seq_reset(struct sequencer_softc *);
     95 static int seq_do_command(struct sequencer_softc *, seq_event_t *);
     96 static int seq_do_chnvoice(struct sequencer_softc *, seq_event_t *);
     97 static int seq_do_chncommon(struct sequencer_softc *, seq_event_t *);
     98 static void seq_timer_waitabs(struct sequencer_softc *, uint32_t);
     99 static int seq_do_timing(struct sequencer_softc *, seq_event_t *);
    100 static int seq_do_local(struct sequencer_softc *, seq_event_t *);
    101 static int seq_do_sysex(struct sequencer_softc *, seq_event_t *);
    102 static int seq_do_fullsize(struct sequencer_softc *, seq_event_t *, struct uio *);
    103 static int seq_input_event(struct sequencer_softc *, seq_event_t *);
    104 static int seq_drain(struct sequencer_softc *);
    105 static void seq_startoutput(struct sequencer_softc *);
    106 static void seq_timeout(void *);
    107 static int seq_to_new(seq_event_t *, struct uio *);
    108 static int seq_sleep_timo(int *, const char *, int);
    109 static int seq_sleep(int *, const char *);
    110 static void seq_wakeup(int *);
    111 
    112 struct midi_softc;
    113 static int midiseq_out(struct midi_dev *, u_char *, u_int, int);
    114 static struct midi_dev *midiseq_open(int, int);
    115 static void midiseq_close(struct midi_dev *);
    116 static void midiseq_reset(struct midi_dev *);
    117 static int midiseq_noteon(struct midi_dev *, int, int, seq_event_t *);
    118 static int midiseq_noteoff(struct midi_dev *, int, int, seq_event_t *);
    119 static int midiseq_keypressure(struct midi_dev *, int, int, seq_event_t *);
    120 static int midiseq_pgmchange(struct midi_dev *, int, seq_event_t *);
    121 static int midiseq_chnpressure(struct midi_dev *, int, seq_event_t *);
    122 static int midiseq_ctlchange(struct midi_dev *, int, seq_event_t *);
    123 static int midiseq_pitchbend(struct midi_dev *, int, seq_event_t *);
    124 static int midiseq_loadpatch(struct midi_dev *, struct sysex_info *, struct uio *);
    125 void midiseq_in(struct midi_dev *, u_char *, int);
    126 
    127 static dev_type_open(sequenceropen);
    128 static dev_type_close(sequencerclose);
    129 static dev_type_read(sequencerread);
    130 static dev_type_write(sequencerwrite);
    131 static dev_type_ioctl(sequencerioctl);
    132 static dev_type_poll(sequencerpoll);
    133 static dev_type_kqfilter(sequencerkqfilter);
    134 
    135 const struct cdevsw sequencer_cdevsw = {
    136 	sequenceropen, sequencerclose, sequencerread, sequencerwrite,
    137 	sequencerioctl, nostop, notty, sequencerpoll, nommap,
    138 	sequencerkqfilter,
    139 };
    140 
    141 void
    142 sequencerattach(int n)
    143 {
    144 
    145 	for (n = 0; n < NSEQUENCER; n++)
    146 		callout_init(&seqdevs[n].sc_callout);
    147 }
    148 
    149 static int
    150 sequenceropen(dev_t dev, int flags, int ifmt, struct lwp *l)
    151 {
    152 	int unit = SEQUENCERUNIT(dev);
    153 	struct sequencer_softc *sc;
    154 	struct midi_dev *md;
    155 	int nmidi;
    156 
    157 	DPRINTF(("sequenceropen\n"));
    158 
    159 	if (unit >= NSEQUENCER)
    160 		return (ENXIO);
    161 	sc = &seqdevs[unit];
    162 	if (sc->isopen)
    163 		return EBUSY;
    164 	if (SEQ_IS_OLD(unit))
    165 		sc->mode = SEQ_OLD;
    166 	else
    167 		sc->mode = SEQ_NEW;
    168 	sc->isopen++;
    169 	sc->flags = flags & (FREAD|FWRITE);
    170 	sc->rchan = 0;
    171 	sc->wchan = 0;
    172 	sc->pbus = 0;
    173 	sc->async = 0;
    174 	sc->input_stamp = ~0;
    175 
    176 	sc->nmidi = 0;
    177 	nmidi = midi_unit_count();
    178 
    179 	sc->devs = malloc(nmidi * sizeof(struct midi_dev *),
    180 			  M_DEVBUF, M_WAITOK);
    181 	for (unit = 0; unit < nmidi; unit++) {
    182 		md = midiseq_open(unit, flags);
    183 		if (md) {
    184 			sc->devs[sc->nmidi++] = md;
    185 			md->seq = sc;
    186 		}
    187 	}
    188 
    189 	sc->timer.timebase = 100;
    190 	sc->timer.tempo = 60;
    191 	sc->doingsysex = 0;
    192 	RECALC_TICK(&sc->timer);
    193 	sc->timer.last = 0;
    194 	microtime(&sc->timer.start);
    195 
    196 	SEQ_QINIT(&sc->inq);
    197 	SEQ_QINIT(&sc->outq);
    198 	sc->lowat = SEQ_MAXQ / 2;
    199 
    200 	seq_reset(sc);
    201 
    202 	DPRINTF(("sequenceropen: mode=%d, nmidi=%d\n", sc->mode, sc->nmidi));
    203 	return 0;
    204 }
    205 
    206 static int
    207 seq_sleep_timo(int *chan, const char *label, int timo)
    208 {
    209 	int st;
    210 
    211 	if (!label)
    212 		label = "seq";
    213 
    214 	DPRINTFN(5, ("seq_sleep_timo: %p %s %d\n", chan, label, timo));
    215 	*chan = 1;
    216 	st = tsleep(chan, PWAIT | PCATCH, label, timo);
    217 	*chan = 0;
    218 #ifdef MIDI_DEBUG
    219 	if (st != 0)
    220 	    printf("seq_sleep: %d\n", st);
    221 #endif
    222 	return st;
    223 }
    224 
    225 static int
    226 seq_sleep(int *chan, const char *label)
    227 {
    228 	return seq_sleep_timo(chan, label, 0);
    229 }
    230 
    231 static void
    232 seq_wakeup(int *chan)
    233 {
    234 	if (*chan) {
    235 		DPRINTFN(5, ("seq_wakeup: %p\n", chan));
    236 		wakeup(chan);
    237 		*chan = 0;
    238 	}
    239 }
    240 
    241 static int
    242 seq_drain(struct sequencer_softc *sc)
    243 {
    244 	int error;
    245 
    246 	DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq)));
    247 	seq_startoutput(sc);
    248 	error = 0;
    249 	while(!SEQ_QEMPTY(&sc->outq) && !error)
    250 		error = seq_sleep_timo(&sc->wchan, "seq_dr", 60*hz);
    251 	return (error);
    252 }
    253 
    254 static void
    255 seq_timeout(void *addr)
    256 {
    257 	struct sequencer_softc *sc = addr;
    258 	DPRINTFN(4, ("seq_timeout: %p\n", sc));
    259 	sc->timeout = 0;
    260 	seq_startoutput(sc);
    261 	if (SEQ_QLEN(&sc->outq) < sc->lowat) {
    262 		seq_wakeup(&sc->wchan);
    263 		selnotify(&sc->wsel, 0);
    264 		if (sc->async)
    265 			psignal(sc->async, SIGIO);
    266 	}
    267 
    268 }
    269 
    270 static void
    271 seq_startoutput(struct sequencer_softc *sc)
    272 {
    273 	struct sequencer_queue *q = &sc->outq;
    274 	seq_event_t cmd;
    275 
    276 	if (sc->timeout)
    277 		return;
    278 	DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q)));
    279 	while(!SEQ_QEMPTY(q) && !sc->timeout) {
    280 		SEQ_QGET(q, cmd);
    281 		seq_do_command(sc, &cmd);
    282 	}
    283 }
    284 
    285 static int
    286 sequencerclose(dev_t dev, int flags, int ifmt, struct lwp *l)
    287 {
    288 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    289 	int n, s;
    290 
    291 	DPRINTF(("sequencerclose: %p\n", sc));
    292 
    293 	seq_drain(sc);
    294 	s = splaudio();
    295 	if (sc->timeout) {
    296 		callout_stop(&sc->sc_callout);
    297 		sc->timeout = 0;
    298 	}
    299 	splx(s);
    300 
    301 	for (n = 0; n < sc->nmidi; n++)
    302 		midiseq_close(sc->devs[n]);
    303 	free(sc->devs, M_DEVBUF);
    304 	sc->isopen = 0;
    305 	return (0);
    306 }
    307 
    308 static int
    309 seq_input_event(struct sequencer_softc *sc, seq_event_t *cmd)
    310 {
    311 	struct sequencer_queue *q = &sc->inq;
    312 
    313 	DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x %02x %02x %02x\n",
    314 		     cmd->tag,
    315 		     cmd->unknown.byte[0], cmd->unknown.byte[1],
    316 		     cmd->unknown.byte[2], cmd->unknown.byte[3],
    317 		     cmd->unknown.byte[4], cmd->unknown.byte[5],
    318 		     cmd->unknown.byte[6]));
    319 	if (SEQ_QFULL(q))
    320 		return (ENOMEM);
    321 	SEQ_QPUT(q, *cmd);
    322 	seq_wakeup(&sc->rchan);
    323 	selnotify(&sc->rsel, 0);
    324 	if (sc->async)
    325 		psignal(sc->async, SIGIO);
    326 	return 0;
    327 }
    328 
    329 void
    330 seq_event_intr(void *addr, seq_event_t *iev)
    331 {
    332 	struct sequencer_softc *sc = addr;
    333 	u_long t;
    334 	struct timeval now;
    335 
    336 	microtime(&now);
    337 	SUBTIMEVAL(&now, &sc->timer.start);
    338 	t = now.tv_sec * 1000000 + now.tv_usec;
    339 	t /= sc->timer.tick;
    340 	if (t != sc->input_stamp) {
    341 		seq_input_event(sc, &SEQ_MK_TIMING(WAIT_ABS, .divisions=t));
    342 		sc->input_stamp = t;
    343 	}
    344 	seq_input_event(sc, iev);
    345 }
    346 
    347 static int
    348 sequencerread(dev_t dev, struct uio *uio, int ioflag)
    349 {
    350 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    351 	struct sequencer_queue *q = &sc->inq;
    352 	seq_event_t ev;
    353 	int error, s;
    354 
    355 	DPRINTFN(20, ("sequencerread: %p, count=%d, ioflag=%x\n",
    356 		     sc, (int) uio->uio_resid, ioflag));
    357 
    358 	if (sc->mode == SEQ_OLD) {
    359 		DPRINTFN(-1,("sequencerread: old read\n"));
    360 		return (EINVAL); /* XXX unimplemented */
    361 	}
    362 
    363 	error = 0;
    364 	while (SEQ_QEMPTY(q)) {
    365 		if (ioflag & IO_NDELAY)
    366 			return EWOULDBLOCK;
    367 		else {
    368 			error = seq_sleep(&sc->rchan, "seq rd");
    369 			if (error)
    370 				return error;
    371 		}
    372 	}
    373 	s = splaudio();
    374 	while (uio->uio_resid >= sizeof ev && !error && !SEQ_QEMPTY(q)) {
    375 		SEQ_QGET(q, ev);
    376 		error = uiomove(&ev, sizeof ev, uio);
    377 	}
    378 	splx(s);
    379 	return error;
    380 }
    381 
    382 static int
    383 sequencerwrite(dev_t dev, struct uio *uio, int ioflag)
    384 {
    385 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    386 	struct sequencer_queue *q = &sc->outq;
    387 	int error;
    388 	seq_event_t cmdbuf;
    389 	int size;
    390 
    391 	DPRINTFN(2, ("sequencerwrite: %p, count=%d\n", sc, (int) uio->uio_resid));
    392 
    393 	error = 0;
    394 	size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE;
    395 	while (uio->uio_resid >= size) {
    396 		error = uiomove(&cmdbuf, size, uio);
    397 		if (error)
    398 			break;
    399 		if (sc->mode == SEQ_OLD)
    400 			if (seq_to_new(&cmdbuf, uio))
    401 				continue;
    402 		if (cmdbuf.tag == SEQ_FULLSIZE) {
    403 			/* We do it like OSS does, asynchronously */
    404 			error = seq_do_fullsize(sc, &cmdbuf, uio);
    405 			if (error)
    406 				break;
    407 			continue;
    408 		}
    409 		while (SEQ_QFULL(q)) {
    410 			seq_startoutput(sc);
    411 			if (SEQ_QFULL(q)) {
    412 				if (ioflag & IO_NDELAY)
    413 					return EWOULDBLOCK;
    414 				error = seq_sleep(&sc->wchan, "seq_wr");
    415 				if (error)
    416 					return error;
    417 			}
    418 		}
    419 		SEQ_QPUT(q, cmdbuf);
    420 	}
    421 	seq_startoutput(sc);
    422 
    423 #ifdef SEQUENCER_DEBUG
    424 	if (error)
    425 		DPRINTFN(2, ("sequencerwrite: error=%d\n", error));
    426 #endif
    427 	return error;
    428 }
    429 
    430 static int
    431 sequencerioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct lwp *l)
    432 {
    433 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    434 	struct synth_info *si;
    435 	struct midi_dev *md;
    436 	int devno;
    437 	int error;
    438 	int t;
    439 
    440 	DPRINTFN(2, ("sequencerioctl: %p cmd=0x%08lx\n", sc, cmd));
    441 
    442 	error = 0;
    443 	switch (cmd) {
    444 	case FIONBIO:
    445 		/* All handled in the upper FS layer. */
    446 		break;
    447 
    448 	case FIOASYNC:
    449 		if (*(int *)addr) {
    450 			if (sc->async)
    451 				return EBUSY;
    452 			sc->async = l->l_proc;
    453 			DPRINTF(("sequencer_ioctl: FIOASYNC %p\n", l));
    454 		} else
    455 			sc->async = 0;
    456 		break;
    457 
    458 	case SEQUENCER_RESET:
    459 		seq_reset(sc);
    460 		break;
    461 
    462 	case SEQUENCER_PANIC:
    463 		seq_reset(sc);
    464 		/* Do more?  OSS doesn't */
    465 		break;
    466 
    467 	case SEQUENCER_SYNC:
    468 		if (sc->flags == FREAD)
    469 			return 0;
    470 		seq_drain(sc);
    471 		error = 0;
    472 		break;
    473 
    474 	case SEQUENCER_INFO:
    475 		si = (struct synth_info*)addr;
    476 		devno = si->device;
    477 		if (devno < 0 || devno >= sc->nmidi)
    478 			return EINVAL;
    479 		md = sc->devs[devno];
    480 		strncpy(si->name, md->name, sizeof si->name);
    481 		si->synth_type = SYNTH_TYPE_MIDI;
    482 		si->synth_subtype = md->subtype;
    483 		si->nr_voices = md->nr_voices;
    484 		si->instr_bank_size = md->instr_bank_size;
    485 		si->capabilities = md->capabilities;
    486 		break;
    487 
    488 	case SEQUENCER_NRSYNTHS:
    489 		*(int *)addr = sc->nmidi;
    490 		break;
    491 
    492 	case SEQUENCER_NRMIDIS:
    493 		*(int *)addr = sc->nmidi;
    494 		break;
    495 
    496 	case SEQUENCER_OUTOFBAND:
    497 		DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
    498 			     *(u_char *)addr, *(u_char *)(addr+1),
    499 			     *(u_char *)(addr+2), *(u_char *)(addr+3),
    500 			     *(u_char *)(addr+4), *(u_char *)(addr+5),
    501 			     *(u_char *)(addr+6), *(u_char *)(addr+7)));
    502 		if ( !(sc->flags & FWRITE ) )
    503 		        return EBADF;
    504 		error = seq_do_command(sc, (seq_event_t *)addr);
    505 		break;
    506 
    507 	case SEQUENCER_TMR_TIMEBASE:
    508 		t = *(int *)addr;
    509 		if (t < 1)
    510 			t = 1;
    511 		if (t > 10000)
    512 			t = 10000;
    513 		sc->timer.timebase = t;
    514 		*(int *)addr = t;
    515 		RECALC_TICK(&sc->timer);
    516 		break;
    517 
    518 	case SEQUENCER_TMR_START:
    519 		error = seq_do_timing(sc, &SEQ_MK_TIMING(START));
    520 		break;
    521 
    522 	case SEQUENCER_TMR_STOP:
    523 		error = seq_do_timing(sc, &SEQ_MK_TIMING(STOP));
    524 		break;
    525 
    526 	case SEQUENCER_TMR_CONTINUE:
    527 		error = seq_do_timing(sc, &SEQ_MK_TIMING(CONTINUE));
    528 		break;
    529 
    530 	case SEQUENCER_TMR_TEMPO:
    531 		error = seq_do_timing(sc,
    532 		    &SEQ_MK_TIMING(TEMPO, .bpm=*(int *)addr));
    533 		if (!error)
    534 		    *(int *)addr = sc->timer.tempo;
    535 		break;
    536 
    537 	case SEQUENCER_TMR_SOURCE:
    538 		*(int *)addr = SEQUENCER_TMR_INTERNAL;
    539 		break;
    540 
    541 	case SEQUENCER_TMR_METRONOME:
    542 		/* noop */
    543 		break;
    544 
    545 	case SEQUENCER_THRESHOLD:
    546 		t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec);
    547 		if (t < 1)
    548 			t = 1;
    549 		if (t > SEQ_MAXQ)
    550 			t = SEQ_MAXQ;
    551 		sc->lowat = t;
    552 		break;
    553 
    554 	case SEQUENCER_CTRLRATE:
    555 		*(int *)addr = (sc->timer.tempo*sc->timer.timebase + 30) / 60;
    556 		break;
    557 
    558 	case SEQUENCER_GETTIME:
    559 	{
    560 		struct timeval now;
    561 		u_long tx;
    562 		microtime(&now);
    563 		SUBTIMEVAL(&now, &sc->timer.start);
    564 		tx = now.tv_sec * 1000000 + now.tv_usec;
    565 		tx /= sc->timer.tick;
    566 		*(int *)addr = tx;
    567 		break;
    568 	}
    569 
    570 	default:
    571 		DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd));
    572 		error = EINVAL;
    573 		break;
    574 	}
    575 	return error;
    576 }
    577 
    578 static int
    579 sequencerpoll(dev_t dev, int events, struct lwp *l)
    580 {
    581 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    582 	int revents = 0;
    583 
    584 	DPRINTF(("sequencerpoll: %p events=0x%x\n", sc, events));
    585 
    586 	if (events & (POLLIN | POLLRDNORM))
    587 		if ((sc->flags&FREAD) && !SEQ_QEMPTY(&sc->inq))
    588 			revents |= events & (POLLIN | POLLRDNORM);
    589 
    590 	if (events & (POLLOUT | POLLWRNORM))
    591 		if ((sc->flags&FWRITE) && SEQ_QLEN(&sc->outq) < sc->lowat)
    592 			revents |= events & (POLLOUT | POLLWRNORM);
    593 
    594 	if (revents == 0) {
    595 		if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM)))
    596 			selrecord(l, &sc->rsel);
    597 
    598 		if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM)))
    599 			selrecord(l, &sc->wsel);
    600 	}
    601 
    602 	return revents;
    603 }
    604 
    605 static void
    606 filt_sequencerrdetach(struct knote *kn)
    607 {
    608 	struct sequencer_softc *sc = kn->kn_hook;
    609 	int s;
    610 
    611 	s = splaudio();
    612 	SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext);
    613 	splx(s);
    614 }
    615 
    616 static int
    617 filt_sequencerread(struct knote *kn, long hint)
    618 {
    619 	struct sequencer_softc *sc = kn->kn_hook;
    620 
    621 	/* XXXLUKEM (thorpej): make sure this is correct */
    622 
    623 	if (SEQ_QEMPTY(&sc->inq))
    624 		return (0);
    625 	kn->kn_data = sizeof(seq_event_rec);
    626 	return (1);
    627 }
    628 
    629 static const struct filterops sequencerread_filtops =
    630 	{ 1, NULL, filt_sequencerrdetach, filt_sequencerread };
    631 
    632 static void
    633 filt_sequencerwdetach(struct knote *kn)
    634 {
    635 	struct sequencer_softc *sc = kn->kn_hook;
    636 	int s;
    637 
    638 	s = splaudio();
    639 	SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext);
    640 	splx(s);
    641 }
    642 
    643 static int
    644 filt_sequencerwrite(struct knote *kn, long hint)
    645 {
    646 	struct sequencer_softc *sc = kn->kn_hook;
    647 
    648 	/* XXXLUKEM (thorpej): make sure this is correct */
    649 
    650 	if (SEQ_QLEN(&sc->outq) >= sc->lowat)
    651 		return (0);
    652 	kn->kn_data = sizeof(seq_event_rec);
    653 	return (1);
    654 }
    655 
    656 static const struct filterops sequencerwrite_filtops =
    657 	{ 1, NULL, filt_sequencerwdetach, filt_sequencerwrite };
    658 
    659 static int
    660 sequencerkqfilter(dev_t dev, struct knote *kn)
    661 {
    662 	struct sequencer_softc *sc = &seqdevs[SEQUENCERUNIT(dev)];
    663 	struct klist *klist;
    664 	int s;
    665 
    666 	switch (kn->kn_filter) {
    667 	case EVFILT_READ:
    668 		klist = &sc->rsel.sel_klist;
    669 		kn->kn_fop = &sequencerread_filtops;
    670 		break;
    671 
    672 	case EVFILT_WRITE:
    673 		klist = &sc->wsel.sel_klist;
    674 		kn->kn_fop = &sequencerwrite_filtops;
    675 		break;
    676 
    677 	default:
    678 		return (1);
    679 	}
    680 
    681 	kn->kn_hook = sc;
    682 
    683 	s = splaudio();
    684 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    685 	splx(s);
    686 
    687 	return (0);
    688 }
    689 
    690 static void
    691 seq_reset(struct sequencer_softc *sc)
    692 {
    693 	int i, chn;
    694 	struct midi_dev *md;
    695 
    696 	if ( !(sc->flags & FWRITE) )
    697 	        return;
    698 	for (i = 0; i < sc->nmidi; i++) {
    699 		md = sc->devs[i];
    700 		midiseq_reset(md);
    701 		for (chn = 0; chn < MAXCHAN; chn++) {
    702 			midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE,
    703 			    .controller=MIDI_CTRL_ALLOFF));
    704 			midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE,
    705 			    .controller=MIDI_CTRL_RESET));
    706 			midiseq_pitchbend(md, chn, &SEQ_MK_CHN(PITCH_BEND,
    707 			    .value=MIDI_BEND_NEUTRAL));
    708 		}
    709 	}
    710 }
    711 
    712 static int
    713 seq_do_command(struct sequencer_softc *sc, seq_event_t *b)
    714 {
    715 	int dev;
    716 
    717 	DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, SEQ_CMD(b)));
    718 
    719 	switch(b->tag) {
    720 	case SEQ_LOCAL:
    721 		return seq_do_local(sc, b);
    722 	case SEQ_TIMING:
    723 		return seq_do_timing(sc, b);
    724 	case SEQ_CHN_VOICE:
    725 		return seq_do_chnvoice(sc, b);
    726 	case SEQ_CHN_COMMON:
    727 		return seq_do_chncommon(sc, b);
    728 	case SEQ_SYSEX:
    729 		return seq_do_sysex(sc, b);
    730 	/* COMPAT */
    731 	case SEQOLD_MIDIPUTC:
    732 		dev = b->unknown.byte[1];
    733 		if (dev < 0 || dev >= sc->nmidi)
    734 			return (ENXIO);
    735 		return midiseq_out(sc->devs[dev], b->unknown.byte, 1, 0);
    736 	default:
    737 		DPRINTFN(-1,("seq_do_command: unimpl command %02x\n", b->tag));
    738 		return (EINVAL);
    739 	}
    740 }
    741 
    742 static int
    743 seq_do_chnvoice(struct sequencer_softc *sc, seq_event_t *b)
    744 {
    745 	int dev;
    746 	int error;
    747 	struct midi_dev *md;
    748 
    749 	dev = b->voice.device;
    750 	if (dev < 0 || dev >= sc->nmidi ||
    751 	    b->voice.channel > 15 ||
    752 	    b->voice.key >= SEQ_NOTE_MAX)
    753 		return ENXIO;
    754 	md = sc->devs[dev];
    755 	switch(b->voice.op) {
    756 	case MIDI_NOTEON: /* no need to special-case hidden noteoff here */
    757 		error = midiseq_noteon(md, b->voice.channel, b->voice.key, b);
    758 		break;
    759 	case MIDI_NOTEOFF:
    760 		error = midiseq_noteoff(md, b->voice.channel, b->voice.key, b);
    761 		break;
    762 	case MIDI_KEY_PRESSURE:
    763 		error = midiseq_keypressure(md,
    764 		    b->voice.channel, b->voice.key, b);
    765 		break;
    766 	default:
    767 		DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n",
    768 			b->voice.op));
    769 		error = EINVAL;
    770 		break;
    771 	}
    772 	return error;
    773 }
    774 
    775 static int
    776 seq_do_chncommon(struct sequencer_softc *sc, seq_event_t *b)
    777 {
    778 	int dev;
    779 	int error;
    780 	struct midi_dev *md;
    781 
    782 	dev = b->common.device;
    783 	if (dev < 0 || dev >= sc->nmidi ||
    784 	    b->common.channel > 15)
    785 		return ENXIO;
    786 	md = sc->devs[dev];
    787 	DPRINTFN(2,("seq_do_chncommon: %02x\n", b->common.op));
    788 
    789 	error = 0;
    790 	switch(b->common.op) {
    791 	case MIDI_PGM_CHANGE:
    792 		error = midiseq_pgmchange(md, b->common.channel, b);
    793 		break;
    794 	case MIDI_CTL_CHANGE:
    795 		error = midiseq_ctlchange(md, b->common.channel, b);
    796 		break;
    797 	case MIDI_PITCH_BEND:
    798 		error = midiseq_pitchbend(md, b->common.channel, b);
    799 		break;
    800 	case MIDI_CHN_PRESSURE:
    801 		error = midiseq_chnpressure(md, b->common.channel, b);
    802 		break;
    803 	default:
    804 		DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n",
    805 			b->common.op));
    806 		error = EINVAL;
    807 		break;
    808 	}
    809 	return error;
    810 }
    811 
    812 static int
    813 seq_do_local(struct sequencer_softc *sc, seq_event_t *b)
    814 {
    815 	return (EINVAL);
    816 }
    817 
    818 static int
    819 seq_do_sysex(struct sequencer_softc *sc, seq_event_t *b)
    820 {
    821 	int dev, i;
    822 	struct midi_dev *md;
    823 	uint8_t *bf = b->sysex.buffer;
    824 
    825 	dev = b->sysex.device;
    826 	if (dev < 0 || dev >= sc->nmidi)
    827 		return (ENXIO);
    828 	DPRINTF(("seq_do_sysex: dev=%d\n", dev));
    829 	md = sc->devs[dev];
    830 
    831 	if (!sc->doingsysex) {
    832 		midiseq_out(md, (uint8_t[]){MIDI_SYSEX_START}, 1, 0);
    833 		sc->doingsysex = 1;
    834 	}
    835 
    836 	for (i = 0; i < 6 && bf[i] != 0xff; i++)
    837 		;
    838 	midiseq_out(md, bf, i, 0);
    839 	if (i < 6 || (i > 0 && bf[i-1] == MIDI_SYSEX_END))
    840 		sc->doingsysex = 0;
    841 	return 0;
    842 }
    843 
    844 static void
    845 seq_timer_waitabs(struct sequencer_softc *sc, uint32_t divs)
    846 {
    847 	struct timeval when;
    848 	long long usec;
    849 	struct syn_timer *t;
    850 	int ticks;
    851 
    852 	t = &sc->timer;
    853 	t->last = divs;
    854 	usec = (long long)divs * (long long)t->tick; /* convert to usec */
    855 	when.tv_sec = usec / 1000000;
    856 	when.tv_usec = usec % 1000000;
    857 	DPRINTFN(4, ("seq_timer_waitabs: divs=%d, sleep when=%ld.%06ld", divs,
    858 		     when.tv_sec, when.tv_usec));
    859 	ADDTIMEVAL(&when, &t->start); /* abstime for end */
    860 	ticks = hzto(&when);
    861 	DPRINTFN(4, (" when+start=%ld.%06ld, tick=%d\n",
    862 		     when.tv_sec, when.tv_usec, ticks));
    863 	if (ticks > 0) {
    864 #ifdef DIAGNOSTIC
    865 		if (ticks > 20 * hz) {
    866 			/* Waiting more than 20s */
    867 			printf("seq_timer_waitabs: funny ticks=%d, "
    868 			       "usec=%lld, parm=%d, tick=%ld\n",
    869 			       ticks, usec, parm, t->tick);
    870 		}
    871 #endif
    872 		sc->timeout = 1;
    873 		callout_reset(&sc->sc_callout, ticks,
    874 		    seq_timeout, sc);
    875 	}
    876 #ifdef SEQUENCER_DEBUG
    877 	else if (tick < 0)
    878 		DPRINTF(("seq_timer_waitabs: ticks = %d\n", ticks));
    879 #endif
    880 }
    881 
    882 static int
    883 seq_do_timing(struct sequencer_softc *sc, seq_event_t *b)
    884 {
    885 	struct syn_timer *t = &sc->timer;
    886 	struct timeval when;
    887 	int error;
    888 
    889 	error = 0;
    890 	switch(b->timing.op) {
    891 	case TMR_WAIT_REL:
    892 		seq_timer_waitabs(sc, b->t_WAIT_REL.divisions + t->last);
    893 		break;
    894 	case TMR_WAIT_ABS:
    895 		seq_timer_waitabs(sc, b->t_WAIT_ABS.divisions);
    896 		break;
    897 	case TMR_START:
    898 		microtime(&t->start);
    899 		t->running = 1;
    900 		break;
    901 	case TMR_STOP:
    902 		microtime(&t->stop);
    903 		t->running = 0;
    904 		break;
    905 	case TMR_CONTINUE:
    906 		microtime(&when);
    907 		SUBTIMEVAL(&when, &t->stop);
    908 		ADDTIMEVAL(&t->start, &when);
    909 		t->running = 1;
    910 		break;
    911 	case TMR_TEMPO:
    912 		/* bpm is unambiguously MIDI clocks per minute / 24 */
    913 		/* (24 MIDI clocks are usually but not always a quarter note) */
    914 		if (b->t_TEMPO.bpm < 8) /* where are these limits specified? */
    915 			t->tempo = 8;
    916 		else if (b->t_TEMPO.bpm > 360) /* ? */
    917 			t->tempo = 360;
    918 		else
    919 			t->tempo = b->t_TEMPO.bpm;
    920 		RECALC_TICK(t);
    921 		break;
    922 	case TMR_ECHO:
    923 		error = seq_input_event(sc, b);
    924 		break;
    925 	case TMR_RESET:
    926 		t->last = 0;
    927 		microtime(&t->start);
    928 		break;
    929 	case TMR_SPP:
    930 	case TMR_TIMESIG:
    931 		DPRINTF(("seq_do_timing: unimplemented %02x\n", b->timing.op));
    932 		error = EINVAL; /* not quite accurate... */
    933 		break;
    934 	default:
    935 		DPRINTF(("seq_timer: unknown %02x\n", cmd));
    936 		error = EINVAL;
    937 		break;
    938 	}
    939 	return (error);
    940 }
    941 
    942 static int
    943 seq_do_fullsize(struct sequencer_softc *sc, seq_event_t *b, struct uio *uio)
    944 {
    945 	struct sysex_info sysex;
    946 	u_int dev;
    947 
    948 #ifdef DIAGNOSTIC
    949 	if (sizeof(seq_event_rec) != SEQ_SYSEX_HDRSIZE) {
    950 		printf("seq_do_fullsize: sysex size ??\n");
    951 		return EINVAL;
    952 	}
    953 #endif
    954 	memcpy(&sysex, b, sizeof sysex);
    955 	dev = sysex.device_no;
    956 	if (dev < 0 || dev >= sc->nmidi)
    957 		return (ENXIO);
    958 	DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
    959 		     sysex.key, dev, sysex.len));
    960 	return (midiseq_loadpatch(sc->devs[dev], &sysex, uio));
    961 }
    962 
    963 /* Convert an old sequencer event to a new one. */
    964 static int
    965 seq_to_new(seq_event_t *ev, struct uio *uio)
    966 {
    967 	int cmd, chan, note, parm;
    968 	uint32_t tmp_delay;
    969 	int error;
    970 	uint8_t *bfp;
    971 
    972 	cmd = ev->tag;
    973 	bfp = ev->unknown.byte;
    974 	chan = *bfp++;
    975 	note = *bfp++;
    976 	parm = *bfp++;
    977 	DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm));
    978 
    979 	if (cmd >= 0x80) {
    980 		/* Fill the event record */
    981 		if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) {
    982 			error = uiomove(bfp, sizeof *ev - SEQOLD_CMDSIZE, uio);
    983 			if (error)
    984 				return error;
    985 		} else
    986 			return EINVAL;
    987 	}
    988 
    989 	switch(cmd) {
    990 	case SEQOLD_NOTEOFF:
    991 		/*
    992 		 * What's with the SEQ_NOTE_XXX?  In OSS this seems to have
    993 		 * been undocumented magic for messing with the overall volume
    994 		 * of a 'voice', equated precariously with 'channel' and
    995 		 * pretty much unimplementable except by directly frobbing a
    996 		 * synth chip. For us, who treat everything as interfaced over
    997 		 * MIDI, this will just be unceremoniously discarded as
    998 		 * invalid in midiseq_noteoff, making the whole event an
    999 		 * elaborate no-op, and that doesn't seem to be any different
   1000 		 * from what happens on linux with a MIDI-interfaced device,
   1001 		 * by the way. The moral is ... use the new /dev/music API, ok?
   1002 		 */
   1003 		*ev = SEQ_MK_CHN(NOTEOFF, .device=0, .channel=chan,
   1004 		    .key=SEQ_NOTE_XXX, .velocity=parm);
   1005 		break;
   1006 	case SEQOLD_NOTEON:
   1007 		*ev = SEQ_MK_CHN(NOTEON,
   1008 		    .device=0, .channel=chan, .key=note, .velocity=parm);
   1009 		break;
   1010 	case SEQOLD_WAIT:
   1011 		/*
   1012 		 * This event cannot even /exist/ on non-littleendian machines,
   1013 		 * and so help me, that's exactly the way OSS defined it.
   1014 		 * Also, the OSS programmer's guide states (p. 74, v1.11)
   1015 		 * that seqold time units are system clock ticks, unlike
   1016 		 * the new 'divisions' which are determined by timebase. In
   1017 		 * that case we would need to do scaling here - but no such
   1018 		 * behavior is visible in linux either--which also treats this
   1019 		 * value, surprisingly, as an absolute, not relative, time.
   1020 		 * My guess is that this event has gone unused so long that
   1021 		 * nobody could agree we got it wrong no matter what we do.
   1022 		 */
   1023 		tmp_delay = *(uint32_t *)ev >> 8;
   1024 		*ev = SEQ_MK_TIMING(WAIT_ABS, .divisions=tmp_delay);
   1025 		break;
   1026 	case SEQOLD_SYNCTIMER:
   1027 		/*
   1028 		 * The TMR_RESET event is not defined in any OSS materials
   1029 		 * I can find; it may have been invented here just to provide
   1030 		 * an accurate _to_new translation of this event.
   1031 		 */
   1032 		*ev = SEQ_MK_TIMING(RESET);
   1033 		break;
   1034 	case SEQOLD_PGMCHANGE:
   1035 		*ev = SEQ_MK_CHN(PGM_CHANGE,
   1036 		    .device=0, .channel=chan, .program=note);
   1037 		break;
   1038 	case SEQOLD_MIDIPUTC:
   1039 		break;		/* interpret in normal mode */
   1040 	case SEQOLD_ECHO:
   1041 	case SEQOLD_PRIVATE:
   1042 	case SEQOLD_EXTENDED:
   1043 	default:
   1044 		DPRINTF(("seq_to_new: not impl 0x%02x\n", cmd));
   1045 		return EINVAL;
   1046 	/* In case new-style events show up */
   1047 	case SEQ_TIMING:
   1048 	case SEQ_CHN_VOICE:
   1049 	case SEQ_CHN_COMMON:
   1050 	case SEQ_FULLSIZE:
   1051 		break;
   1052 	}
   1053 	return 0;
   1054 }
   1055 
   1056 /**********************************************/
   1057 
   1058 void
   1059 midiseq_in(struct midi_dev *md, u_char *msg, int len)
   1060 {
   1061 	int unit = md->unit;
   1062 	seq_event_t ev;
   1063 	int status, chan;
   1064 
   1065 	DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
   1066 		     md, msg[0], msg[1], msg[2]));
   1067 
   1068 	status = MIDI_GET_STATUS(msg[0]);
   1069 	chan = MIDI_GET_CHAN(msg[0]);
   1070 	switch (status) {
   1071 	case MIDI_NOTEON: /* midi(4) always canonicalizes hidden note-off */
   1072 		ev = SEQ_MK_CHN(NOTEON, .device=unit, .channel=chan,
   1073 		    .key=msg[1], .velocity=msg[2]);
   1074 		break;
   1075 	case MIDI_NOTEOFF:
   1076 		ev = SEQ_MK_CHN(NOTEOFF, .device=unit, .channel=chan,
   1077 		    .key=msg[1], .velocity=msg[2]);
   1078 		break;
   1079 	case MIDI_KEY_PRESSURE:
   1080 		ev = SEQ_MK_CHN(KEY_PRESSURE, .device=unit, .channel=chan,
   1081 		    .key=msg[1], .pressure=msg[2]);
   1082 		break;
   1083 	case MIDI_CTL_CHANGE: /* XXX not correct for MSB */
   1084 		ev = SEQ_MK_CHN(CTL_CHANGE, .device=unit, .channel=chan,
   1085 		    .controller=msg[1], .value=msg[2]);
   1086 		break;
   1087 	case MIDI_PGM_CHANGE:
   1088 		ev = SEQ_MK_CHN(PGM_CHANGE, .device=unit, .channel=chan,
   1089 		    .program=msg[1]);
   1090 		break;
   1091 	case MIDI_CHN_PRESSURE:
   1092 		ev = SEQ_MK_CHN(CHN_PRESSURE, .device=unit, .channel=chan,
   1093 		    .pressure=msg[1]);
   1094 		break;
   1095 	case MIDI_PITCH_BEND:
   1096 		ev = SEQ_MK_CHN(PITCH_BEND, .device=unit, .channel=chan,
   1097 		    .value=(msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7));
   1098 		break;
   1099 	default: /* this is now the point where MIDI_ACKs disappear */
   1100 		return;
   1101 	}
   1102 	seq_event_intr(md->seq, &ev);
   1103 }
   1104 
   1105 static struct midi_dev *
   1106 midiseq_open(int unit, int flags)
   1107 {
   1108 	extern struct cfdriver midi_cd;
   1109 	extern const struct cdevsw midi_cdevsw;
   1110 	int error;
   1111 	struct midi_dev *md;
   1112 	struct midi_softc *sc;
   1113 	struct midi_info mi;
   1114 
   1115 	midi_getinfo(makedev(0, unit), &mi);
   1116 	if ( !(mi.props & MIDI_PROP_CAN_INPUT) )
   1117 	        flags &= ~FREAD;
   1118 	if ( 0 == ( flags & ( FREAD | FWRITE ) ) )
   1119 	        return 0;
   1120 	DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags));
   1121 	error = (*midi_cdevsw.d_open)(makedev(0, unit), flags, 0, 0);
   1122 	if (error)
   1123 		return (0);
   1124 	sc = midi_cd.cd_devs[unit];
   1125 	sc->seqopen = 1;
   1126 	md = malloc(sizeof *md, M_DEVBUF, M_WAITOK|M_ZERO);
   1127 	sc->seq_md = md;
   1128 	md->msc = sc;
   1129 	md->unit = unit;
   1130 	md->name = mi.name;
   1131 	md->subtype = 0;
   1132 	md->nr_voices = 128;	/* XXX */
   1133 	md->instr_bank_size = 128; /* XXX */
   1134 	if (mi.props & MIDI_PROP_CAN_INPUT)
   1135 		md->capabilities |= SYNTH_CAP_INPUT;
   1136 	return (md);
   1137 }
   1138 
   1139 static void
   1140 midiseq_close(struct midi_dev *md)
   1141 {
   1142 	extern const struct cdevsw midi_cdevsw;
   1143 
   1144 	DPRINTFN(2, ("midiseq_close: %d\n", md->unit));
   1145 	(*midi_cdevsw.d_close)(makedev(0, md->unit), 0, 0, 0);
   1146 	free(md, M_DEVBUF);
   1147 }
   1148 
   1149 static void
   1150 midiseq_reset(struct midi_dev *md)
   1151 {
   1152 	/* XXX send GM reset? */
   1153 	DPRINTFN(3, ("midiseq_reset: %d\n", md->unit));
   1154 }
   1155 
   1156 static int
   1157 midiseq_out(struct midi_dev *md, u_char *bf, u_int cc, int chk)
   1158 {
   1159 	DPRINTFN(5, ("midiseq_out: m=%p, unit=%d, bf[0]=0x%02x, cc=%d\n",
   1160 		     md->msc, md->unit, bf[0], cc));
   1161 
   1162 	/* midi(4) does running status compression where appropriate. */
   1163 	return midi_writebytes(md->unit, bf, cc);
   1164 }
   1165 
   1166 /*
   1167  * If the writing process hands us a hidden note-off in a note-on event,
   1168  * we will simply write it that way; no need to special case it here,
   1169  * as midi(4) will always canonicalize or compress as appropriate anyway.
   1170  */
   1171 static int
   1172 midiseq_noteon(struct midi_dev *md, int chan, int key, seq_event_t *ev)
   1173 {
   1174 	return midiseq_out(md, (uint8_t[]){
   1175 	    MIDI_NOTEON | chan, key, ev->c_NOTEON.velocity & 0x7f}, 3, 1);
   1176 }
   1177 
   1178 static int
   1179 midiseq_noteoff(struct midi_dev *md, int chan, int key, seq_event_t *ev)
   1180 {
   1181 	return midiseq_out(md, (uint8_t[]){
   1182 	    MIDI_NOTEOFF | chan, key, ev->c_NOTEOFF.velocity & 0x7f}, 3, 1);
   1183 }
   1184 
   1185 static int
   1186 midiseq_keypressure(struct midi_dev *md, int chan, int key, seq_event_t *ev)
   1187 {
   1188 	return midiseq_out(md, (uint8_t[]){
   1189 	    MIDI_KEY_PRESSURE | chan, key,
   1190 	    ev->c_KEY_PRESSURE.pressure & 0x7f}, 3, 1);
   1191 }
   1192 
   1193 static int
   1194 midiseq_pgmchange(struct midi_dev *md, int chan, seq_event_t *ev)
   1195 {
   1196 	if (ev->c_PGM_CHANGE.program > 127)
   1197 		return EINVAL;
   1198 	return midiseq_out(md, (uint8_t[]){
   1199 	    MIDI_PGM_CHANGE | chan, ev->c_PGM_CHANGE.program}, 2, 1);
   1200 }
   1201 
   1202 static int
   1203 midiseq_chnpressure(struct midi_dev *md, int chan, seq_event_t *ev)
   1204 {
   1205 	if (ev->c_CHN_PRESSURE.pressure > 127)
   1206 		return EINVAL;
   1207 	return midiseq_out(md, (uint8_t[]){
   1208 	    MIDI_CHN_PRESSURE | chan, ev->c_CHN_PRESSURE.pressure}, 2, 1);
   1209 }
   1210 
   1211 static int
   1212 midiseq_ctlchange(struct midi_dev *md, int chan, seq_event_t *ev)
   1213 {
   1214 	if (ev->c_CTL_CHANGE.controller > 127)
   1215 		return EINVAL;
   1216 	return midiseq_out( md, (uint8_t[]){
   1217 	    MIDI_CTL_CHANGE | chan, ev->c_CTL_CHANGE.controller,
   1218 	    ev->c_CTL_CHANGE.value & 0x7f /* XXX this is SO wrong */
   1219 	    }, 3, 1);
   1220 }
   1221 
   1222 static int
   1223 midiseq_pitchbend(struct midi_dev *md, int chan, seq_event_t *ev)
   1224 {
   1225 	return midiseq_out(md, (uint8_t[]){
   1226 	    MIDI_PITCH_BEND | chan,
   1227 	    ev->c_PITCH_BEND.value & 0x7f,
   1228 	    (ev->c_PITCH_BEND.value >> 7) & 0x7f}, 3, 1);
   1229 }
   1230 
   1231 static int
   1232 midiseq_loadpatch(struct midi_dev *md,
   1233                   struct sysex_info *sysex, struct uio *uio)
   1234 {
   1235 	u_char c, bf[128];
   1236 	int i, cc, error;
   1237 
   1238 	if (sysex->key != SEQ_SYSEX_PATCH) {
   1239 		DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n",
   1240 			     sysex->key));
   1241 		return (EINVAL);
   1242 	}
   1243 	if (uio->uio_resid < sysex->len)
   1244 		/* adjust length, should be an error */
   1245 		sysex->len = uio->uio_resid;
   1246 
   1247 	DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len));
   1248 	if (sysex->len == 0)
   1249 		return EINVAL;
   1250 	error = uiomove(&c, 1, uio);
   1251 	if (error)
   1252 		return error;
   1253 	if (c != MIDI_SYSEX_START)		/* must start like this */
   1254 		return EINVAL;
   1255 	error = midiseq_out(md, &c, 1, 0);
   1256 	if (error)
   1257 		return error;
   1258 	--sysex->len;
   1259 	while (sysex->len > 0) {
   1260 		cc = sysex->len;
   1261 		if (cc > sizeof bf)
   1262 			cc = sizeof bf;
   1263 		error = uiomove(bf, cc, uio);
   1264 		if (error)
   1265 			break;
   1266 		for(i = 0; i < cc && !MIDI_IS_STATUS(bf[i]); i++)
   1267 			;
   1268 		/*
   1269 		 * XXX midi(4)'s buffer might not accomodate this, and the
   1270 		 * function will not block us (though in this case we have
   1271 		 * a process and could in principle block).
   1272 		 */
   1273 		error = midiseq_out(md, bf, i, 0);
   1274 		if (error)
   1275 			break;
   1276 		sysex->len -= i;
   1277 		if (i != cc)
   1278 			break;
   1279 	}
   1280 	/*
   1281 	 * Any leftover data in uio is rubbish;
   1282 	 * the SYSEX should be one write ending in SYSEX_END.
   1283 	 */
   1284 	uio->uio_resid = 0;
   1285 	c = MIDI_SYSEX_END;
   1286 	return midiseq_out(md, &c, 1, 0);
   1287 }
   1288 
   1289 #include "midi.h"
   1290 #if NMIDI == 0
   1291 static dev_type_open(midiopen);
   1292 static dev_type_close(midiclose);
   1293 
   1294 const struct cdevsw midi_cdevsw = {
   1295 	midiopen, midiclose, noread, nowrite, noioctl,
   1296 	nostop, notty, nopoll, nommap,
   1297 };
   1298 
   1299 /*
   1300  * If someone has a sequencer, but no midi devices there will
   1301  * be unresolved references, so we provide little stubs.
   1302  */
   1303 
   1304 int
   1305 midi_unit_count()
   1306 {
   1307 	return (0);
   1308 }
   1309 
   1310 static int
   1311 midiopen(dev_t dev, int flags, int ifmt, struct lwp *l)
   1312 {
   1313 	return (ENXIO);
   1314 }
   1315 
   1316 struct cfdriver midi_cd;
   1317 
   1318 void
   1319 midi_getinfo(dev_t dev, struct midi_info *mi)
   1320 {
   1321 }
   1322 
   1323 static int
   1324 midiclose(dev_t dev, int flags, int ifmt, struct lwp *l)
   1325 {
   1326 	return (ENXIO);
   1327 }
   1328 
   1329 int
   1330 midi_writebytes(int unit, u_char *bf, int cc)
   1331 {
   1332 	return (ENXIO);
   1333 }
   1334 #endif /* NMIDI == 0 */
   1335