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