Home | History | Annotate | Line # | Download | only in dev
      1  1.84  riastrad /*	$NetBSD: sequencer.c,v 1.84 2023/10/17 09:59:46 riastradh Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4  1.53  jmcneill  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7  1.13  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8  1.53  jmcneill  * by Lennart Augustsson (augustss (at) NetBSD.org) and by Andrew Doran.
      9   1.1  augustss  *
     10   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11   1.1  augustss  * modification, are permitted provided that the following conditions
     12   1.1  augustss  * are met:
     13   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18   1.1  augustss  *
     19   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  augustss  */
     31  1.17     lukem 
     32  1.53  jmcneill /*
     33  1.53  jmcneill  * Locking:
     34  1.53  jmcneill  *
     35  1.53  jmcneill  * o sc_lock: provides atomic access to all data structures.  Taken from
     36  1.53  jmcneill  *   both process and soft interrupt context.
     37  1.53  jmcneill  *
     38  1.53  jmcneill  * o sc_dvlock: serializes operations on /dev/sequencer.  Taken from
     39  1.53  jmcneill  *   process context.  Dropped while waiting for data in sequencerread()
     40  1.53  jmcneill  *   to allow concurrent reads/writes while no data available.
     41  1.53  jmcneill  *
     42  1.53  jmcneill  * o sc_isopen: we allow only one concurrent open, only to prevent user
     43  1.53  jmcneill  *   and/or application error.
     44  1.53  jmcneill  *
     45  1.53  jmcneill  * o MIDI softc locks.  These can be spinlocks and there can be many of
     46  1.53  jmcneill  *   them, because we can open many MIDI devices.  We take these only in two
     47  1.53  jmcneill  *   places: when enabling redirection from the MIDI device and when
     48  1.53  jmcneill  *   disabling it (open/close).  midiseq_in() is called by the MIDI driver
     49  1.53  jmcneill  *   with its own lock held when passing data into this module.  To avoid
     50  1.53  jmcneill  *   lock order and context problems, we package the received message as a
     51  1.53  jmcneill  *   sequencer_pcqitem_t and put onto a producer-consumer queue.  A soft
     52  1.53  jmcneill  *   interrupt is scheduled to dequeue and decode the message later where we
     53  1.53  jmcneill  *   can safely acquire the sequencer device's sc_lock.  PCQ is lockless for
     54  1.53  jmcneill  *   multiple producer, single consumer settings like this one.
     55  1.53  jmcneill  */
     56  1.53  jmcneill 
     57  1.17     lukem #include <sys/cdefs.h>
     58  1.84  riastrad __KERNEL_RCSID(0, "$NetBSD: sequencer.c,v 1.84 2023/10/17 09:59:46 riastradh Exp $");
     59   1.1  augustss 
     60  1.66  pgoyette #ifdef _KERNEL_OPT
     61  1.66  pgoyette #include "midi.h"
     62  1.66  pgoyette #endif
     63   1.1  augustss 
     64   1.1  augustss #include <sys/param.h>
     65  1.77  riastrad #include <sys/types.h>
     66  1.77  riastrad 
     67  1.77  riastrad #include <sys/atomic.h>
     68  1.77  riastrad #include <sys/audioio.h>
     69  1.77  riastrad #include <sys/conf.h>
     70  1.77  riastrad #include <sys/device.h>
     71  1.77  riastrad #include <sys/fcntl.h>
     72  1.77  riastrad #include <sys/intr.h>
     73   1.1  augustss #include <sys/ioctl.h>
     74  1.77  riastrad #include <sys/kauth.h>
     75  1.77  riastrad #include <sys/kernel.h>
     76  1.77  riastrad #include <sys/kmem.h>
     77  1.77  riastrad #include <sys/midiio.h>
     78  1.77  riastrad #include <sys/pcq.h>
     79   1.1  augustss #include <sys/poll.h>
     80   1.1  augustss #include <sys/proc.h>
     81  1.77  riastrad #include <sys/select.h>
     82  1.77  riastrad #include <sys/signalvar.h>
     83  1.77  riastrad #include <sys/syslog.h>
     84   1.1  augustss #include <sys/systm.h>
     85  1.53  jmcneill #include <sys/vnode.h>
     86  1.77  riastrad #include <sys/vnode.h>
     87   1.1  augustss 
     88   1.8  augustss #include <dev/midi_if.h>
     89   1.1  augustss #include <dev/midivar.h>
     90   1.1  augustss #include <dev/sequencervar.h>
     91   1.1  augustss 
     92  1.64  christos #include "ioconf.h"
     93  1.64  christos 
     94  1.69  riastrad /*
     95  1.69  riastrad  * XXX Kludge.  This module uses midi_cd, and depends on the `midi'
     96  1.69  riastrad  * module, but there's no obvious way to get midi_cd declared in
     97  1.69  riastrad  * ioconf.h without actually pulling MIDI into the module in
     98  1.69  riastrad  * sys/modules/sequencer/sequencer.ioconf.  Please fix me!
     99  1.69  riastrad  *
    100  1.69  riastrad  * XXX XXX XXX Apparently sequencer.ioconf doesn't actually make the
    101  1.69  riastrad  * sequencer cdev!  Did this ever work?
    102  1.70  riastrad  *
    103  1.70  riastrad  * XXX XXX XXX Apparently there are even some kernels that include a
    104  1.70  riastrad  * sequencer pseudo-device but exclude any midi device.  How do they
    105  1.70  riastrad  * even link??
    106  1.69  riastrad  */
    107  1.70  riastrad extern struct cfdriver midi_cd;
    108  1.69  riastrad #ifdef _MODULE
    109  1.69  riastrad extern struct cfdriver sequencer_cd;
    110  1.69  riastrad #endif
    111  1.69  riastrad 
    112   1.1  augustss #define ADDTIMEVAL(a, b) ( \
    113   1.1  augustss 	(a)->tv_sec += (b)->tv_sec, \
    114   1.1  augustss 	(a)->tv_usec += (b)->tv_usec, \
    115   1.1  augustss 	(a)->tv_usec > 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\
    116   1.1  augustss 	)
    117   1.1  augustss 
    118   1.1  augustss #define SUBTIMEVAL(a, b) ( \
    119   1.1  augustss 	(a)->tv_sec -= (b)->tv_sec, \
    120   1.1  augustss 	(a)->tv_usec -= (b)->tv_usec, \
    121   1.1  augustss 	(a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\
    122   1.1  augustss 	)
    123   1.1  augustss 
    124   1.1  augustss #ifdef AUDIO_DEBUG
    125  1.83     oster #define DPRINTF(x)	do { if (sequencerdebug) printf x; } while (0)
    126  1.83     oster #define DPRINTFN(n,x)	do { if (sequencerdebug >= (n)) printf x; } while (0)
    127   1.1  augustss int	sequencerdebug = 0;
    128   1.1  augustss #else
    129  1.84  riastrad #define DPRINTF(x)	do { } while (0)
    130  1.83     oster #define DPRINTFN(n,x)	do { } while (0)
    131   1.1  augustss #endif
    132   1.1  augustss 
    133   1.1  augustss #define SEQ_NOTE_MAX 128
    134   1.1  augustss #define SEQ_NOTE_XXX 255
    135   1.1  augustss 
    136  1.32      chap #define RECALC_USPERDIV(t) \
    137  1.32      chap ((t)->usperdiv = 60*1000000L/((t)->tempo_beatpermin*(t)->timebase_divperbeat))
    138   1.1  augustss 
    139  1.53  jmcneill typedef union sequencer_pcqitem {
    140  1.53  jmcneill 	void	*qi_ptr;
    141  1.53  jmcneill 	char	qi_msg[4];
    142  1.53  jmcneill } sequencer_pcqitem_t;
    143  1.53  jmcneill 
    144  1.32      chap static void seq_reset(struct sequencer_softc *);
    145  1.32      chap static int seq_do_command(struct sequencer_softc *, seq_event_t *);
    146  1.32      chap static int seq_do_chnvoice(struct sequencer_softc *, seq_event_t *);
    147  1.32      chap static int seq_do_chncommon(struct sequencer_softc *, seq_event_t *);
    148  1.32      chap static void seq_timer_waitabs(struct sequencer_softc *, uint32_t);
    149  1.32      chap static int seq_do_timing(struct sequencer_softc *, seq_event_t *);
    150  1.32      chap static int seq_do_local(struct sequencer_softc *, seq_event_t *);
    151  1.32      chap static int seq_do_sysex(struct sequencer_softc *, seq_event_t *);
    152  1.77  riastrad static int seq_do_fullsize(struct sequencer_softc *, seq_event_t *,
    153  1.77  riastrad     struct uio *);
    154  1.32      chap static int seq_input_event(struct sequencer_softc *, seq_event_t *);
    155  1.32      chap static int seq_drain(struct sequencer_softc *);
    156  1.32      chap static void seq_startoutput(struct sequencer_softc *);
    157  1.32      chap static void seq_timeout(void *);
    158  1.32      chap static int seq_to_new(seq_event_t *, struct uio *);
    159  1.39        ad static void seq_softintr(void *);
    160   1.1  augustss 
    161  1.32      chap static int midiseq_out(struct midi_dev *, u_char *, u_int, int);
    162  1.32      chap static struct midi_dev *midiseq_open(int, int);
    163  1.32      chap static void midiseq_close(struct midi_dev *);
    164  1.32      chap static void midiseq_reset(struct midi_dev *);
    165  1.32      chap static int midiseq_noteon(struct midi_dev *, int, int, seq_event_t *);
    166  1.32      chap static int midiseq_noteoff(struct midi_dev *, int, int, seq_event_t *);
    167  1.32      chap static int midiseq_keypressure(struct midi_dev *, int, int, seq_event_t *);
    168  1.32      chap static int midiseq_pgmchange(struct midi_dev *, int, seq_event_t *);
    169  1.32      chap static int midiseq_chnpressure(struct midi_dev *, int, seq_event_t *);
    170  1.32      chap static int midiseq_ctlchange(struct midi_dev *, int, seq_event_t *);
    171  1.32      chap static int midiseq_pitchbend(struct midi_dev *, int, seq_event_t *);
    172  1.77  riastrad static int midiseq_loadpatch(struct midi_dev *, struct sysex_info *,
    173  1.77  riastrad     struct uio *);
    174  1.26     perry void midiseq_in(struct midi_dev *, u_char *, int);
    175   1.1  augustss 
    176  1.32      chap static dev_type_open(sequenceropen);
    177  1.32      chap static dev_type_close(sequencerclose);
    178  1.32      chap static dev_type_read(sequencerread);
    179  1.32      chap static dev_type_write(sequencerwrite);
    180  1.32      chap static dev_type_ioctl(sequencerioctl);
    181  1.32      chap static dev_type_poll(sequencerpoll);
    182  1.32      chap static dev_type_kqfilter(sequencerkqfilter);
    183  1.20   gehenna 
    184  1.20   gehenna const struct cdevsw sequencer_cdevsw = {
    185  1.58  dholland 	.d_open = sequenceropen,
    186  1.58  dholland 	.d_close = sequencerclose,
    187  1.58  dholland 	.d_read = sequencerread,
    188  1.58  dholland 	.d_write = sequencerwrite,
    189  1.58  dholland 	.d_ioctl = sequencerioctl,
    190  1.58  dholland 	.d_stop = nostop,
    191  1.58  dholland 	.d_tty = notty,
    192  1.58  dholland 	.d_poll = sequencerpoll,
    193  1.58  dholland 	.d_mmap = nommap,
    194  1.58  dholland 	.d_kqfilter = sequencerkqfilter,
    195  1.59  dholland 	.d_discard = nodiscard,
    196  1.58  dholland 	.d_flag = D_OTHER | D_MPSAFE
    197  1.20   gehenna };
    198  1.77  riastrad static LIST_HEAD(, sequencer_softc) sequencers =
    199  1.77  riastrad     LIST_HEAD_INITIALIZER(sequencers);
    200  1.56  christos static kmutex_t sequencer_lock;
    201  1.56  christos 
    202  1.56  christos static void
    203  1.60       mrg sequencerdestroy(struct sequencer_softc *sc)
    204  1.60       mrg {
    205  1.77  riastrad 
    206  1.60       mrg 	callout_halt(&sc->sc_callout, &sc->lock);
    207  1.56  christos 	callout_destroy(&sc->sc_callout);
    208  1.56  christos 	softint_disestablish(sc->sih);
    209  1.56  christos 	cv_destroy(&sc->rchan);
    210  1.56  christos 	cv_destroy(&sc->wchan);
    211  1.56  christos 	cv_destroy(&sc->lchan);
    212  1.56  christos 	if (sc->pcq)
    213  1.56  christos 		pcq_destroy(sc->pcq);
    214  1.56  christos 	kmem_free(sc, sizeof(*sc));
    215  1.56  christos }
    216  1.56  christos 
    217  1.56  christos static struct sequencer_softc *
    218  1.60       mrg sequencercreate(int unit)
    219  1.60       mrg {
    220  1.56  christos 	struct sequencer_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    221  1.77  riastrad 
    222  1.56  christos 	sc->sc_unit = unit;
    223  1.56  christos 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
    224  1.56  christos 	sc->sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    225  1.56  christos 	    seq_softintr, sc);
    226  1.56  christos 	mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
    227  1.56  christos 	cv_init(&sc->rchan, "midiseqr");
    228  1.56  christos 	cv_init(&sc->wchan, "midiseqw");
    229  1.56  christos 	cv_init(&sc->lchan, "midiseql");
    230  1.56  christos 	sc->pcq = pcq_create(SEQ_MAXQ, KM_SLEEP);
    231  1.56  christos 	if (sc->pcq == NULL) {
    232  1.56  christos 		sequencerdestroy(sc);
    233  1.56  christos 		return NULL;
    234  1.56  christos 	}
    235  1.56  christos 	return sc;
    236  1.56  christos }
    237  1.56  christos 
    238  1.56  christos 
    239  1.56  christos static struct sequencer_softc *
    240  1.60       mrg sequencerget(int unit)
    241  1.60       mrg {
    242  1.56  christos 	struct sequencer_softc *sc;
    243  1.77  riastrad 
    244  1.78  riastrad 	KASSERTMSG(unit >= 0, "unit=%d", unit);
    245  1.78  riastrad 
    246  1.78  riastrad 	if (unit < 0)
    247  1.56  christos 		return NULL;
    248  1.77  riastrad 
    249  1.56  christos 	mutex_enter(&sequencer_lock);
    250  1.56  christos 	LIST_FOREACH(sc, &sequencers, sc_link) {
    251  1.56  christos 		if (sc->sc_unit == unit) {
    252  1.56  christos 			mutex_exit(&sequencer_lock);
    253  1.56  christos 			return sc;
    254  1.56  christos 		}
    255  1.56  christos 	}
    256  1.56  christos 	mutex_exit(&sequencer_lock);
    257  1.77  riastrad 
    258  1.77  riastrad 	/*
    259  1.77  riastrad 	 * XXXSMP -- nothing excludes another thread from creating the
    260  1.77  riastrad 	 * same unit here
    261  1.77  riastrad 	 */
    262  1.56  christos 	if ((sc = sequencercreate(unit)) == NULL)
    263  1.56  christos 		return NULL;
    264  1.77  riastrad 
    265  1.56  christos 	mutex_enter(&sequencer_lock);
    266  1.56  christos 	LIST_INSERT_HEAD(&sequencers, sc, sc_link);
    267  1.56  christos 	mutex_exit(&sequencer_lock);
    268  1.77  riastrad 
    269  1.56  christos 	return sc;
    270  1.56  christos }
    271  1.56  christos 
    272  1.56  christos #ifdef notyet
    273  1.77  riastrad static void
    274  1.60       mrg sequencerput(struct sequencer_softc *sc)
    275  1.60       mrg {
    276  1.77  riastrad 
    277  1.56  christos 	mutex_enter(&sequencer_lock);
    278  1.56  christos 	LIST_REMOVE(sc, sc_link);
    279  1.56  christos 	mutex_exit(&sequencer_lock);
    280  1.56  christos 	sequencerdestroy(sc);
    281  1.56  christos }
    282  1.56  christos #endif
    283  1.20   gehenna 
    284   1.1  augustss void
    285  1.32      chap sequencerattach(int n)
    286   1.1  augustss {
    287  1.77  riastrad 
    288  1.56  christos 	mutex_init(&sequencer_lock, MUTEX_DEFAULT, IPL_NONE);
    289  1.53  jmcneill }
    290  1.53  jmcneill 
    291  1.53  jmcneill /*
    292  1.53  jmcneill  * Release reference to device acquired with sequencer_enter().
    293  1.53  jmcneill  */
    294  1.53  jmcneill static void
    295  1.53  jmcneill sequencer_exit(struct sequencer_softc *sc)
    296  1.53  jmcneill {
    297  1.53  jmcneill 
    298  1.53  jmcneill 	sc->dvlock--;
    299  1.53  jmcneill 	cv_broadcast(&sc->lchan);
    300  1.53  jmcneill 	mutex_exit(&sc->lock);
    301  1.53  jmcneill }
    302  1.53  jmcneill 
    303  1.53  jmcneill /*
    304  1.53  jmcneill  * Look up sequencer device and acquire locks for device access.
    305  1.53  jmcneill  */
    306  1.53  jmcneill static int
    307  1.53  jmcneill sequencer_enter(dev_t dev, struct sequencer_softc **scp)
    308  1.53  jmcneill {
    309  1.53  jmcneill 	struct sequencer_softc *sc;
    310  1.53  jmcneill 
    311  1.53  jmcneill 	/* First, find the device and take sc_lock. */
    312  1.56  christos 	if ((sc = sequencerget(SEQUENCERUNIT(dev))) == NULL)
    313  1.53  jmcneill 		return ENXIO;
    314  1.77  riastrad 
    315  1.53  jmcneill 	mutex_enter(&sc->lock);
    316  1.53  jmcneill 	while (sc->dvlock) {
    317  1.53  jmcneill 		cv_wait(&sc->lchan, &sc->lock);
    318  1.53  jmcneill 	}
    319  1.53  jmcneill 	sc->dvlock++;
    320  1.53  jmcneill 	if (sc->dying) {
    321  1.53  jmcneill 		sequencer_exit(sc);
    322  1.53  jmcneill 		return EIO;
    323  1.39        ad 	}
    324  1.53  jmcneill 	*scp = sc;
    325  1.53  jmcneill 	return 0;
    326   1.1  augustss }
    327   1.1  augustss 
    328  1.32      chap static int
    329  1.37  christos sequenceropen(dev_t dev, int flags, int ifmt, struct lwp *l)
    330   1.1  augustss {
    331   1.1  augustss 	struct sequencer_softc *sc;
    332   1.1  augustss 	struct midi_dev *md;
    333  1.53  jmcneill 	struct midi_softc *msc;
    334  1.62       mrg 	int error, unit, mdno;
    335   1.1  augustss 
    336   1.1  augustss 	DPRINTF(("sequenceropen\n"));
    337   1.1  augustss 
    338  1.53  jmcneill 	if ((error = sequencer_enter(dev, &sc)) != 0)
    339  1.53  jmcneill 		return error;
    340  1.53  jmcneill 	if (sc->isopen != 0) {
    341  1.53  jmcneill 		sequencer_exit(sc);
    342   1.1  augustss 		return EBUSY;
    343  1.53  jmcneill 	}
    344  1.53  jmcneill 
    345  1.56  christos 	if (SEQ_IS_OLD(SEQUENCERUNIT(dev)))
    346   1.1  augustss 		sc->mode = SEQ_OLD;
    347   1.1  augustss 	else
    348   1.1  augustss 		sc->mode = SEQ_NEW;
    349   1.1  augustss 	sc->isopen++;
    350   1.1  augustss 	sc->flags = flags & (FREAD|FWRITE);
    351   1.1  augustss 	sc->pbus = 0;
    352   1.1  augustss 	sc->async = 0;
    353   1.1  augustss 	sc->input_stamp = ~0;
    354   1.1  augustss 
    355   1.1  augustss 	sc->nmidi = 0;
    356  1.53  jmcneill 	sc->ndevs = midi_unit_count();
    357  1.32      chap 	sc->timer.timebase_divperbeat = 100;
    358  1.32      chap 	sc->timer.tempo_beatpermin = 60;
    359  1.32      chap 	RECALC_USPERDIV(&sc->timer);
    360  1.32      chap 	sc->timer.divs_lastevent = sc->timer.divs_lastchange = 0;
    361  1.32      chap 	microtime(&sc->timer.reftime);
    362   1.1  augustss 
    363   1.1  augustss 	SEQ_QINIT(&sc->inq);
    364   1.1  augustss 	SEQ_QINIT(&sc->outq);
    365   1.1  augustss 	sc->lowat = SEQ_MAXQ / 2;
    366   1.1  augustss 
    367  1.53  jmcneill 	if (sc->ndevs > 0) {
    368  1.53  jmcneill 		mutex_exit(&sc->lock);
    369  1.53  jmcneill 		sc->devs = kmem_alloc(sc->ndevs * sizeof(struct midi_dev *),
    370  1.53  jmcneill 		    KM_SLEEP);
    371  1.53  jmcneill 		for (unit = 0; unit < sc->ndevs; unit++) {
    372  1.53  jmcneill 			md = midiseq_open(unit, flags);
    373  1.53  jmcneill 			if (md) {
    374  1.53  jmcneill 				sc->devs[sc->nmidi++] = md;
    375  1.53  jmcneill 				md->seq = sc;
    376  1.53  jmcneill 				md->doingsysex = 0;
    377  1.62       mrg 				DPRINTF(("%s: midi unit %d opened as seq %p\n",
    378  1.62       mrg 				    __func__, unit, md));
    379  1.63       mrg 			} else {
    380  1.62       mrg 				DPRINTF(("%s: midi unit %d not opened as seq\n",
    381  1.62       mrg 				    __func__, unit));
    382  1.63       mrg 			}
    383  1.53  jmcneill 		}
    384  1.53  jmcneill 		mutex_enter(&sc->lock);
    385  1.53  jmcneill 	} else {
    386  1.53  jmcneill 		sc->devs = NULL;
    387  1.53  jmcneill 	}
    388  1.53  jmcneill 
    389  1.53  jmcneill 	/* Only now redirect input from MIDI devices. */
    390  1.62       mrg 	for (mdno = 0; mdno < sc->nmidi; mdno++) {
    391  1.62       mrg 		msc = device_lookup_private(&midi_cd, sc->devs[mdno]->unit);
    392  1.60       mrg 		if (msc) {
    393  1.60       mrg 			mutex_enter(msc->lock);
    394  1.60       mrg 			msc->seqopen = 1;
    395  1.60       mrg 			mutex_exit(msc->lock);
    396  1.60       mrg 		}
    397  1.53  jmcneill 	}
    398  1.53  jmcneill 
    399   1.5  augustss 	seq_reset(sc);
    400  1.53  jmcneill 	sequencer_exit(sc);
    401   1.5  augustss 
    402  1.53  jmcneill 	DPRINTF(("%s: mode=%d, nmidi=%d\n", __func__, sc->mode, sc->nmidi));
    403   1.1  augustss 	return 0;
    404   1.1  augustss }
    405   1.1  augustss 
    406   1.1  augustss static int
    407  1.32      chap seq_drain(struct sequencer_softc *sc)
    408   1.1  augustss {
    409   1.1  augustss 	int error;
    410   1.1  augustss 
    411  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
    412  1.53  jmcneill 
    413   1.1  augustss 	DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq)));
    414   1.1  augustss 	seq_startoutput(sc);
    415   1.1  augustss 	error = 0;
    416  1.53  jmcneill 	while (!SEQ_QEMPTY(&sc->outq) && !error)
    417  1.53  jmcneill 		error = cv_timedwait_sig(&sc->wchan, &sc->lock, 60*hz);
    418  1.77  riastrad 	return error;
    419   1.1  augustss }
    420   1.1  augustss 
    421  1.32      chap static void
    422  1.32      chap seq_timeout(void *addr)
    423   1.1  augustss {
    424   1.1  augustss 	struct sequencer_softc *sc = addr;
    425  1.53  jmcneill 	proc_t *p;
    426  1.53  jmcneill 	pid_t pid;
    427  1.39        ad 
    428   1.1  augustss 	DPRINTFN(4, ("seq_timeout: %p\n", sc));
    429  1.53  jmcneill 
    430  1.53  jmcneill 	mutex_enter(&sc->lock);
    431  1.53  jmcneill 	if (sc->timeout == 0) {
    432  1.75  riastrad 		mutex_exit(&sc->lock);
    433  1.53  jmcneill 		return;
    434  1.53  jmcneill 	}
    435   1.1  augustss 	sc->timeout = 0;
    436   1.1  augustss 	seq_startoutput(sc);
    437  1.53  jmcneill 	if (SEQ_QLEN(&sc->outq) >= sc->lowat) {
    438  1.53  jmcneill 		mutex_exit(&sc->lock);
    439  1.53  jmcneill 		return;
    440  1.53  jmcneill 	}
    441  1.53  jmcneill 	cv_broadcast(&sc->wchan);
    442  1.53  jmcneill 	selnotify(&sc->wsel, 0, NOTE_SUBMIT);
    443  1.53  jmcneill 	if ((pid = sc->async) != 0) {
    444  1.72        ad 		mutex_enter(&proc_lock);
    445  1.53  jmcneill 		if ((p = proc_find(pid)) != NULL)
    446  1.53  jmcneill 			psignal(p, SIGIO);
    447  1.72        ad 		mutex_exit(&proc_lock);
    448   1.1  augustss 	}
    449  1.53  jmcneill 	mutex_exit(&sc->lock);
    450   1.1  augustss }
    451   1.1  augustss 
    452  1.32      chap static void
    453  1.32      chap seq_startoutput(struct sequencer_softc *sc)
    454   1.1  augustss {
    455   1.1  augustss 	struct sequencer_queue *q = &sc->outq;
    456  1.32      chap 	seq_event_t cmd;
    457   1.1  augustss 
    458  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
    459  1.53  jmcneill 
    460   1.1  augustss 	if (sc->timeout)
    461   1.1  augustss 		return;
    462   1.1  augustss 	DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q)));
    463  1.53  jmcneill 	while (!SEQ_QEMPTY(q) && !sc->timeout) {
    464   1.1  augustss 		SEQ_QGET(q, cmd);
    465   1.1  augustss 		seq_do_command(sc, &cmd);
    466   1.1  augustss 	}
    467   1.1  augustss }
    468   1.1  augustss 
    469  1.32      chap static int
    470  1.53  jmcneill sequencerclose(dev_t dev, int flags, int ifmt, struct lwp *l)
    471   1.1  augustss {
    472  1.53  jmcneill 	struct sequencer_softc *sc;
    473  1.53  jmcneill 	struct midi_softc *msc;
    474  1.53  jmcneill 	int unit, error;
    475   1.1  augustss 
    476  1.61       mrg 	DPRINTF(("%s: %"PRIx64"\n", __func__, dev));
    477   1.1  augustss 
    478  1.53  jmcneill 	if ((error = sequencer_enter(dev, &sc)) != 0)
    479  1.53  jmcneill 		return error;
    480   1.1  augustss 	seq_drain(sc);
    481   1.7  augustss 	if (sc->timeout) {
    482  1.53  jmcneill 		callout_halt(&sc->sc_callout, &sc->lock);
    483   1.7  augustss 		sc->timeout = 0;
    484   1.7  augustss 	}
    485  1.53  jmcneill 	/* Bin input from MIDI devices. */
    486  1.53  jmcneill 	for (unit = 0; unit < sc->nmidi; unit++) {
    487  1.60       mrg 		msc = device_lookup_private(&midi_cd, unit);
    488  1.60       mrg 		if (msc) {
    489  1.60       mrg 			mutex_enter(msc->lock);
    490  1.60       mrg 			msc->seqopen = 0;
    491  1.60       mrg 			mutex_exit(msc->lock);
    492  1.60       mrg 		}
    493  1.53  jmcneill 	}
    494  1.53  jmcneill 	mutex_exit(&sc->lock);
    495  1.53  jmcneill 
    496  1.53  jmcneill 	for (unit = 0; unit < sc->nmidi; unit++)
    497  1.53  jmcneill 		if (sc->devs[unit] != NULL)
    498  1.53  jmcneill 			midiseq_close(sc->devs[unit]);
    499  1.53  jmcneill 	if (sc->devs != NULL) {
    500  1.53  jmcneill 		KASSERT(sc->ndevs > 0);
    501  1.53  jmcneill 		kmem_free(sc->devs, sc->ndevs * sizeof(struct midi_dev *));
    502  1.53  jmcneill 		sc->devs = NULL;
    503  1.53  jmcneill 	}
    504   1.1  augustss 
    505  1.53  jmcneill 	mutex_enter(&sc->lock);
    506   1.1  augustss 	sc->isopen = 0;
    507  1.53  jmcneill 	sequencer_exit(sc);
    508   1.1  augustss 
    509  1.61       mrg 	DPRINTF(("%s: %"PRIx64" done\n", __func__, dev));
    510  1.39        ad 
    511  1.77  riastrad 	return 0;
    512  1.39        ad }
    513  1.39        ad 
    514  1.12  augustss static int
    515  1.32      chap seq_input_event(struct sequencer_softc *sc, seq_event_t *cmd)
    516   1.1  augustss {
    517  1.53  jmcneill 	struct sequencer_queue *q;
    518  1.53  jmcneill 
    519  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
    520   1.1  augustss 
    521  1.53  jmcneill 	DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x "
    522  1.53  jmcneill 	    "%02x %02x %02x\n", cmd->tag,
    523  1.53  jmcneill 	    cmd->unknown.byte[0], cmd->unknown.byte[1],
    524  1.53  jmcneill 	    cmd->unknown.byte[2], cmd->unknown.byte[3],
    525  1.53  jmcneill 	    cmd->unknown.byte[4], cmd->unknown.byte[5],
    526  1.53  jmcneill 	    cmd->unknown.byte[6]));
    527  1.53  jmcneill 	q = &sc->inq;
    528   1.1  augustss 	if (SEQ_QFULL(q))
    529  1.77  riastrad 		return ENOMEM;
    530   1.1  augustss 	SEQ_QPUT(q, *cmd);
    531  1.53  jmcneill 	cv_broadcast(&sc->rchan);
    532  1.53  jmcneill 	selnotify(&sc->rsel, 0, NOTE_SUBMIT);
    533  1.53  jmcneill 	if (sc->async != 0) {
    534  1.53  jmcneill 		proc_t *p;
    535  1.53  jmcneill 
    536  1.72        ad 		mutex_enter(&proc_lock);
    537  1.53  jmcneill 		if ((p = proc_find(sc->async)) != NULL)
    538  1.53  jmcneill 			psignal(p, SIGIO);
    539  1.72        ad 		mutex_exit(&proc_lock);
    540  1.53  jmcneill 	}
    541   1.1  augustss 	return 0;
    542   1.1  augustss }
    543   1.1  augustss 
    544  1.53  jmcneill static void
    545  1.53  jmcneill seq_softintr(void *addr)
    546   1.1  augustss {
    547  1.53  jmcneill 	struct sequencer_softc *sc;
    548  1.53  jmcneill 	struct timeval now;
    549  1.53  jmcneill 	seq_event_t ev;
    550  1.53  jmcneill 	int status, chan, unit;
    551  1.53  jmcneill 	sequencer_pcqitem_t qi;
    552   1.1  augustss 	u_long t;
    553   1.1  augustss 
    554  1.53  jmcneill 	sc = addr;
    555  1.53  jmcneill 
    556  1.53  jmcneill 	mutex_enter(&sc->lock);
    557  1.53  jmcneill 
    558  1.53  jmcneill 	qi.qi_ptr = pcq_get(sc->pcq);
    559  1.53  jmcneill 	if (qi.qi_ptr == NULL) {
    560  1.53  jmcneill 		mutex_exit(&sc->lock);
    561  1.53  jmcneill 		return;
    562  1.53  jmcneill 	}
    563  1.53  jmcneill 	KASSERT((qi.qi_msg[3] & 0x80) != 0);
    564  1.53  jmcneill 	unit = qi.qi_msg[3] & ~0x80;
    565  1.53  jmcneill 	status = MIDI_GET_STATUS(qi.qi_msg[0]);
    566  1.53  jmcneill 	chan = MIDI_GET_CHAN(qi.qi_msg[0]);
    567  1.53  jmcneill 	switch (status) {
    568  1.53  jmcneill 	case MIDI_NOTEON: /* midi(4) always canonicalizes hidden note-off */
    569  1.53  jmcneill 		ev = SEQ_MK_CHN(NOTEON, .device=unit, .channel=chan,
    570  1.53  jmcneill 		    .key=qi.qi_msg[1], .velocity=qi.qi_msg[2]);
    571  1.53  jmcneill 		break;
    572  1.53  jmcneill 	case MIDI_NOTEOFF:
    573  1.53  jmcneill 		ev = SEQ_MK_CHN(NOTEOFF, .device=unit, .channel=chan,
    574  1.53  jmcneill 		    .key=qi.qi_msg[1], .velocity=qi.qi_msg[2]);
    575  1.53  jmcneill 		break;
    576  1.53  jmcneill 	case MIDI_KEY_PRESSURE:
    577  1.53  jmcneill 		ev = SEQ_MK_CHN(KEY_PRESSURE, .device=unit, .channel=chan,
    578  1.53  jmcneill 		    .key=qi.qi_msg[1], .pressure=qi.qi_msg[2]);
    579  1.53  jmcneill 		break;
    580  1.53  jmcneill 	case MIDI_CTL_CHANGE: /* XXX not correct for MSB */
    581  1.53  jmcneill 		ev = SEQ_MK_CHN(CTL_CHANGE, .device=unit, .channel=chan,
    582  1.53  jmcneill 		    .controller=qi.qi_msg[1], .value=qi.qi_msg[2]);
    583  1.53  jmcneill 		break;
    584  1.53  jmcneill 	case MIDI_PGM_CHANGE:
    585  1.53  jmcneill 		ev = SEQ_MK_CHN(PGM_CHANGE, .device=unit, .channel=chan,
    586  1.53  jmcneill 		    .program=qi.qi_msg[1]);
    587  1.53  jmcneill 		break;
    588  1.53  jmcneill 	case MIDI_CHN_PRESSURE:
    589  1.53  jmcneill 		ev = SEQ_MK_CHN(CHN_PRESSURE, .device=unit, .channel=chan,
    590  1.53  jmcneill 		    .pressure=qi.qi_msg[1]);
    591  1.53  jmcneill 		break;
    592  1.53  jmcneill 	case MIDI_PITCH_BEND:
    593  1.53  jmcneill 		ev = SEQ_MK_CHN(PITCH_BEND, .device=unit, .channel=chan,
    594  1.53  jmcneill 		    .value=(qi.qi_msg[1] & 0x7f) | ((qi.qi_msg[2] & 0x7f) << 7));
    595  1.53  jmcneill 		break;
    596  1.53  jmcneill 	default: /* this is now the point where MIDI_ACKs disappear */
    597  1.53  jmcneill 		mutex_exit(&sc->lock);
    598  1.53  jmcneill 		return;
    599  1.53  jmcneill 	}
    600   1.1  augustss 	microtime(&now);
    601  1.32      chap 	if (!sc->timer.running)
    602  1.32      chap 		now = sc->timer.stoptime;
    603  1.32      chap 	SUBTIMEVAL(&now, &sc->timer.reftime);
    604   1.1  augustss 	t = now.tv_sec * 1000000 + now.tv_usec;
    605  1.32      chap 	t /= sc->timer.usperdiv;
    606  1.32      chap 	t += sc->timer.divs_lastchange;
    607   1.1  augustss 	if (t != sc->input_stamp) {
    608  1.32      chap 		seq_input_event(sc, &SEQ_MK_TIMING(WAIT_ABS, .divisions=t));
    609  1.62       mrg 		sc->input_stamp = t; /* XXX what happens if timer is reset? */
    610   1.1  augustss 	}
    611  1.53  jmcneill 	seq_input_event(sc, &ev);
    612  1.53  jmcneill 	mutex_exit(&sc->lock);
    613   1.1  augustss }
    614   1.1  augustss 
    615  1.32      chap static int
    616  1.32      chap sequencerread(dev_t dev, struct uio *uio, int ioflag)
    617   1.1  augustss {
    618  1.53  jmcneill 	struct sequencer_softc *sc;
    619  1.53  jmcneill 	struct sequencer_queue *q;
    620  1.32      chap 	seq_event_t ev;
    621  1.53  jmcneill 	int error;
    622   1.1  augustss 
    623  1.62       mrg 	DPRINTFN(2, ("sequencerread: %"PRIx64", count=%d, ioflag=%x\n",
    624  1.53  jmcneill 	   dev, (int)uio->uio_resid, ioflag));
    625  1.53  jmcneill 
    626  1.53  jmcneill 	if ((error = sequencer_enter(dev, &sc)) != 0)
    627  1.53  jmcneill 		return error;
    628  1.53  jmcneill 	q = &sc->inq;
    629   1.1  augustss 
    630   1.1  augustss 	if (sc->mode == SEQ_OLD) {
    631  1.53  jmcneill 		sequencer_exit(sc);
    632   1.5  augustss 		DPRINTFN(-1,("sequencerread: old read\n"));
    633  1.53  jmcneill 		return EINVAL; /* XXX unimplemented */
    634   1.1  augustss 	}
    635   1.1  augustss 	while (SEQ_QEMPTY(q)) {
    636  1.53  jmcneill 		if (ioflag & IO_NDELAY) {
    637  1.53  jmcneill 			error = EWOULDBLOCK;
    638  1.53  jmcneill 			break;
    639  1.53  jmcneill 		}
    640  1.53  jmcneill 		/* Drop lock to allow concurrent read/write. */
    641  1.53  jmcneill 		KASSERT(sc->dvlock != 0);
    642  1.53  jmcneill 		sc->dvlock--;
    643  1.53  jmcneill 		error = cv_wait_sig(&sc->rchan, &sc->lock);
    644  1.53  jmcneill 		while (sc->dvlock != 0) {
    645  1.53  jmcneill 			cv_wait(&sc->lchan, &sc->lock);
    646  1.53  jmcneill 		}
    647  1.53  jmcneill 		sc->dvlock++;
    648  1.53  jmcneill 		if (error) {
    649  1.53  jmcneill 			break;
    650   1.1  augustss 		}
    651   1.1  augustss 	}
    652  1.53  jmcneill 	while (uio->uio_resid >= sizeof(ev) && !error && !SEQ_QEMPTY(q)) {
    653   1.1  augustss 		SEQ_QGET(q, ev);
    654  1.53  jmcneill 		mutex_exit(&sc->lock);
    655  1.53  jmcneill 		error = uiomove(&ev, sizeof(ev), uio);
    656  1.53  jmcneill 		mutex_enter(&sc->lock);
    657   1.1  augustss 	}
    658  1.53  jmcneill 	sequencer_exit(sc);
    659   1.1  augustss 	return error;
    660   1.1  augustss }
    661   1.1  augustss 
    662  1.32      chap static int
    663  1.32      chap sequencerwrite(dev_t dev, struct uio *uio, int ioflag)
    664   1.1  augustss {
    665  1.53  jmcneill 	struct sequencer_softc *sc;
    666  1.53  jmcneill 	struct sequencer_queue *q;
    667   1.1  augustss 	int error;
    668  1.32      chap 	seq_event_t cmdbuf;
    669   1.1  augustss 	int size;
    670  1.77  riastrad 
    671  1.53  jmcneill 	DPRINTFN(2, ("sequencerwrite: %"PRIx64", count=%d\n", dev,
    672  1.53  jmcneill 	    (int)uio->uio_resid));
    673  1.53  jmcneill 
    674  1.53  jmcneill 	if ((error = sequencer_enter(dev, &sc)) != 0)
    675  1.53  jmcneill 		return error;
    676  1.53  jmcneill 	q = &sc->outq;
    677   1.1  augustss 
    678   1.1  augustss 	size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE;
    679  1.53  jmcneill 	while (uio->uio_resid >= size && error == 0) {
    680  1.53  jmcneill 		mutex_exit(&sc->lock);
    681   1.1  augustss 		error = uiomove(&cmdbuf, size, uio);
    682  1.53  jmcneill 		if (error == 0) {
    683  1.53  jmcneill 			if (sc->mode == SEQ_OLD && seq_to_new(&cmdbuf, uio)) {
    684  1.53  jmcneill 				mutex_enter(&sc->lock);
    685  1.53  jmcneill 				continue;
    686  1.53  jmcneill 			}
    687  1.53  jmcneill 			if (cmdbuf.tag == SEQ_FULLSIZE) {
    688  1.53  jmcneill 				/* We do it like OSS does, asynchronously */
    689  1.53  jmcneill 				error = seq_do_fullsize(sc, &cmdbuf, uio);
    690  1.53  jmcneill 				if (error == 0) {
    691  1.53  jmcneill 					mutex_enter(&sc->lock);
    692  1.53  jmcneill 					continue;
    693  1.53  jmcneill 				}
    694  1.53  jmcneill 			}
    695  1.53  jmcneill 		}
    696  1.53  jmcneill 		mutex_enter(&sc->lock);
    697  1.53  jmcneill 		if (error != 0) {
    698   1.1  augustss 			break;
    699   1.1  augustss 		}
    700   1.1  augustss 		while (SEQ_QFULL(q)) {
    701   1.1  augustss 			seq_startoutput(sc);
    702   1.1  augustss 			if (SEQ_QFULL(q)) {
    703  1.53  jmcneill 				if (ioflag & IO_NDELAY) {
    704  1.53  jmcneill 					error = EWOULDBLOCK;
    705  1.53  jmcneill 					break;
    706  1.53  jmcneill 				}
    707  1.53  jmcneill 				error = cv_wait_sig(&sc->wchan, &sc->lock);
    708  1.53  jmcneill 				if (error) {
    709  1.53  jmcneill 					 break;
    710  1.53  jmcneill 				}
    711   1.1  augustss 			}
    712   1.1  augustss 		}
    713  1.53  jmcneill 		if (error == 0) {
    714  1.53  jmcneill 			SEQ_QPUT(q, cmdbuf);
    715  1.53  jmcneill 		}
    716   1.1  augustss 	}
    717  1.53  jmcneill 	if (error == 0) {
    718  1.53  jmcneill 		seq_startoutput(sc);
    719  1.53  jmcneill 	} else {
    720   1.1  augustss 		DPRINTFN(2, ("sequencerwrite: error=%d\n", error));
    721  1.53  jmcneill 	}
    722  1.53  jmcneill 	sequencer_exit(sc);
    723   1.1  augustss 	return error;
    724   1.1  augustss }
    725   1.1  augustss 
    726  1.32      chap static int
    727  1.53  jmcneill sequencerioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    728   1.1  augustss {
    729  1.53  jmcneill 	struct sequencer_softc *sc;
    730   1.1  augustss 	struct synth_info *si;
    731   1.2  augustss 	struct midi_dev *md;
    732  1.53  jmcneill 	int devno, error, t;
    733  1.53  jmcneill 	struct timeval now;
    734  1.53  jmcneill 	u_long tx;
    735   1.1  augustss 
    736  1.53  jmcneill 	DPRINTFN(2, ("sequencerioctl: %"PRIx64" cmd=0x%08lx\n", dev, cmd));
    737   1.1  augustss 
    738  1.53  jmcneill 	if ((error = sequencer_enter(dev, &sc)) != 0)
    739  1.53  jmcneill 		return error;
    740   1.1  augustss 	switch (cmd) {
    741   1.1  augustss 	case FIONBIO:
    742   1.1  augustss 		/* All handled in the upper FS layer. */
    743   1.1  augustss 		break;
    744   1.1  augustss 
    745   1.1  augustss 	case FIOASYNC:
    746   1.1  augustss 		if (*(int *)addr) {
    747  1.81  riastrad 			if (sc->async != 0) {
    748  1.81  riastrad 				error = EBUSY;
    749  1.81  riastrad 				break;
    750  1.81  riastrad 			}
    751  1.53  jmcneill 			sc->async = curproc->p_pid;
    752  1.61       mrg 			DPRINTF(("%s: FIOASYNC %d\n", __func__,
    753  1.53  jmcneill 			    sc->async));
    754  1.53  jmcneill 		} else {
    755   1.1  augustss 			sc->async = 0;
    756  1.53  jmcneill 		}
    757   1.1  augustss 		break;
    758   1.1  augustss 
    759   1.1  augustss 	case SEQUENCER_RESET:
    760   1.1  augustss 		seq_reset(sc);
    761   1.1  augustss 		break;
    762   1.1  augustss 
    763   1.1  augustss 	case SEQUENCER_PANIC:
    764   1.1  augustss 		seq_reset(sc);
    765   1.1  augustss 		/* Do more?  OSS doesn't */
    766   1.1  augustss 		break;
    767   1.1  augustss 
    768   1.1  augustss 	case SEQUENCER_SYNC:
    769  1.53  jmcneill 		if (sc->flags != FREAD)
    770  1.53  jmcneill 			seq_drain(sc);
    771   1.1  augustss 		break;
    772   1.1  augustss 
    773   1.1  augustss 	case SEQUENCER_INFO:
    774   1.1  augustss 		si = (struct synth_info*)addr;
    775   1.1  augustss 		devno = si->device;
    776  1.53  jmcneill 		if (devno < 0 || devno >= sc->nmidi) {
    777  1.53  jmcneill 			error = EINVAL;
    778  1.53  jmcneill 			break;
    779  1.53  jmcneill 		}
    780   1.2  augustss 		md = sc->devs[devno];
    781   1.2  augustss 		strncpy(si->name, md->name, sizeof si->name);
    782   1.1  augustss 		si->synth_type = SYNTH_TYPE_MIDI;
    783   1.2  augustss 		si->synth_subtype = md->subtype;
    784   1.2  augustss 		si->nr_voices = md->nr_voices;
    785   1.2  augustss 		si->instr_bank_size = md->instr_bank_size;
    786   1.2  augustss 		si->capabilities = md->capabilities;
    787   1.1  augustss 		break;
    788   1.1  augustss 
    789   1.1  augustss 	case SEQUENCER_NRSYNTHS:
    790   1.3  augustss 		*(int *)addr = sc->nmidi;
    791   1.1  augustss 		break;
    792   1.1  augustss 
    793   1.1  augustss 	case SEQUENCER_NRMIDIS:
    794   1.1  augustss 		*(int *)addr = sc->nmidi;
    795   1.1  augustss 		break;
    796   1.1  augustss 
    797   1.1  augustss 	case SEQUENCER_OUTOFBAND:
    798  1.27     perry 		DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
    799  1.53  jmcneill 		    *(u_char *)addr, *((u_char *)addr+1),
    800  1.53  jmcneill 		    *((u_char *)addr+2), *((u_char *)addr+3),
    801  1.53  jmcneill 		    *((u_char *)addr+4), *((u_char *)addr+5),
    802  1.53  jmcneill 		    *((u_char *)addr+6), *((u_char *)addr+7)));
    803  1.53  jmcneill 		if ((sc->flags & FWRITE) == 0) {
    804  1.53  jmcneill 			error = EBADF;
    805  1.53  jmcneill 		} else {
    806  1.53  jmcneill 			error = seq_do_command(sc, (seq_event_t *)addr);
    807  1.53  jmcneill 		}
    808   1.1  augustss 		break;
    809   1.1  augustss 
    810   1.1  augustss 	case SEQUENCER_TMR_TIMEBASE:
    811   1.1  augustss 		t = *(int *)addr;
    812   1.1  augustss 		if (t < 1)
    813   1.1  augustss 			t = 1;
    814  1.14  augustss 		if (t > 10000)
    815  1.14  augustss 			t = 10000;
    816   1.1  augustss 		*(int *)addr = t;
    817  1.32      chap 		sc->timer.timebase_divperbeat = t;
    818  1.32      chap 		sc->timer.divs_lastchange = sc->timer.divs_lastevent;
    819  1.32      chap 		microtime(&sc->timer.reftime);
    820  1.32      chap 		RECALC_USPERDIV(&sc->timer);
    821   1.1  augustss 		break;
    822   1.1  augustss 
    823   1.1  augustss 	case SEQUENCER_TMR_START:
    824  1.32      chap 		error = seq_do_timing(sc, &SEQ_MK_TIMING(START));
    825   1.1  augustss 		break;
    826   1.1  augustss 
    827   1.1  augustss 	case SEQUENCER_TMR_STOP:
    828  1.32      chap 		error = seq_do_timing(sc, &SEQ_MK_TIMING(STOP));
    829   1.1  augustss 		break;
    830   1.1  augustss 
    831   1.1  augustss 	case SEQUENCER_TMR_CONTINUE:
    832  1.32      chap 		error = seq_do_timing(sc, &SEQ_MK_TIMING(CONTINUE));
    833   1.1  augustss 		break;
    834   1.1  augustss 
    835   1.1  augustss 	case SEQUENCER_TMR_TEMPO:
    836  1.32      chap 		error = seq_do_timing(sc,
    837  1.32      chap 		    &SEQ_MK_TIMING(TEMPO, .bpm=*(int *)addr));
    838  1.62       mrg 		RECALC_USPERDIV(&sc->timer);
    839  1.53  jmcneill 		if (error == 0)
    840  1.53  jmcneill 			*(int *)addr = sc->timer.tempo_beatpermin;
    841   1.1  augustss 		break;
    842   1.1  augustss 
    843   1.1  augustss 	case SEQUENCER_TMR_SOURCE:
    844   1.1  augustss 		*(int *)addr = SEQUENCER_TMR_INTERNAL;
    845   1.1  augustss 		break;
    846   1.1  augustss 
    847   1.1  augustss 	case SEQUENCER_TMR_METRONOME:
    848   1.1  augustss 		/* noop */
    849   1.1  augustss 		break;
    850   1.1  augustss 
    851   1.1  augustss 	case SEQUENCER_THRESHOLD:
    852   1.1  augustss 		t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec);
    853   1.1  augustss 		if (t < 1)
    854   1.1  augustss 			t = 1;
    855   1.1  augustss 		if (t > SEQ_MAXQ)
    856   1.1  augustss 			t = SEQ_MAXQ;
    857   1.1  augustss 		sc->lowat = t;
    858   1.1  augustss 		break;
    859   1.1  augustss 
    860   1.1  augustss 	case SEQUENCER_CTRLRATE:
    861  1.32      chap 		*(int *)addr = (sc->timer.tempo_beatpermin
    862  1.53  jmcneill 		    *sc->timer.timebase_divperbeat + 30) / 60;
    863   1.1  augustss 		break;
    864   1.1  augustss 
    865   1.1  augustss 	case SEQUENCER_GETTIME:
    866   1.1  augustss 		microtime(&now);
    867  1.32      chap 		SUBTIMEVAL(&now, &sc->timer.reftime);
    868  1.28  christos 		tx = now.tv_sec * 1000000 + now.tv_usec;
    869  1.32      chap 		tx /= sc->timer.usperdiv;
    870  1.32      chap 		tx += sc->timer.divs_lastchange;
    871  1.28  christos 		*(int *)addr = tx;
    872   1.1  augustss 		break;
    873   1.1  augustss 
    874   1.1  augustss 	default:
    875   1.5  augustss 		DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd));
    876   1.1  augustss 		error = EINVAL;
    877   1.1  augustss 		break;
    878   1.1  augustss 	}
    879  1.53  jmcneill 	sequencer_exit(sc);
    880  1.53  jmcneill 
    881   1.1  augustss 	return error;
    882   1.1  augustss }
    883   1.1  augustss 
    884  1.32      chap static int
    885  1.32      chap sequencerpoll(dev_t dev, int events, struct lwp *l)
    886   1.1  augustss {
    887  1.56  christos 	struct sequencer_softc *sc;
    888   1.1  augustss 	int revents = 0;
    889  1.77  riastrad 
    890  1.56  christos 	if ((sc = sequencerget(SEQUENCERUNIT(dev))) == NULL)
    891  1.56  christos 		return ENXIO;
    892   1.1  augustss 
    893  1.61       mrg 	DPRINTF(("%s: %p events=0x%x\n", __func__, sc, events));
    894   1.1  augustss 
    895  1.53  jmcneill 	mutex_enter(&sc->lock);
    896   1.1  augustss 	if (events & (POLLIN | POLLRDNORM))
    897  1.32      chap 		if ((sc->flags&FREAD) && !SEQ_QEMPTY(&sc->inq))
    898   1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    899   1.1  augustss 
    900   1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    901  1.32      chap 		if ((sc->flags&FWRITE) && SEQ_QLEN(&sc->outq) < sc->lowat)
    902   1.1  augustss 			revents |= events & (POLLOUT | POLLWRNORM);
    903   1.1  augustss 
    904   1.1  augustss 	if (revents == 0) {
    905  1.32      chap 		if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM)))
    906  1.30  christos 			selrecord(l, &sc->rsel);
    907   1.1  augustss 
    908  1.32      chap 		if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM)))
    909  1.30  christos 			selrecord(l, &sc->wsel);
    910   1.1  augustss 	}
    911  1.53  jmcneill 	mutex_exit(&sc->lock);
    912   1.1  augustss 
    913   1.1  augustss 	return revents;
    914  1.21  jdolecek }
    915  1.21  jdolecek 
    916  1.21  jdolecek static void
    917  1.21  jdolecek filt_sequencerrdetach(struct knote *kn)
    918  1.21  jdolecek {
    919  1.21  jdolecek 	struct sequencer_softc *sc = kn->kn_hook;
    920  1.21  jdolecek 
    921  1.53  jmcneill 	mutex_enter(&sc->lock);
    922  1.73   thorpej 	selremove_knote(&sc->rsel, kn);
    923  1.53  jmcneill 	mutex_exit(&sc->lock);
    924  1.21  jdolecek }
    925  1.21  jdolecek 
    926  1.21  jdolecek static int
    927  1.37  christos filt_sequencerread(struct knote *kn, long hint)
    928  1.21  jdolecek {
    929  1.21  jdolecek 	struct sequencer_softc *sc = kn->kn_hook;
    930  1.53  jmcneill 	int rv;
    931  1.21  jdolecek 
    932  1.53  jmcneill 	if (hint != NOTE_SUBMIT) {
    933  1.53  jmcneill 		mutex_enter(&sc->lock);
    934  1.53  jmcneill 	}
    935  1.53  jmcneill 	if (SEQ_QEMPTY(&sc->inq)) {
    936  1.53  jmcneill 		rv = 0;
    937  1.53  jmcneill 	} else {
    938  1.53  jmcneill 		kn->kn_data = sizeof(seq_event_rec);
    939  1.53  jmcneill 		rv = 1;
    940  1.53  jmcneill 	}
    941  1.53  jmcneill 	if (hint != NOTE_SUBMIT) {
    942  1.53  jmcneill 		mutex_exit(&sc->lock);
    943  1.53  jmcneill 	}
    944  1.53  jmcneill 	return rv;
    945  1.21  jdolecek }
    946  1.21  jdolecek 
    947  1.67      maya static const struct filterops sequencerread_filtops = {
    948  1.74   thorpej 	.f_flags = FILTEROP_ISFD,
    949  1.67      maya 	.f_attach = NULL,
    950  1.67      maya 	.f_detach = filt_sequencerrdetach,
    951  1.67      maya 	.f_event = filt_sequencerread,
    952  1.67      maya };
    953  1.21  jdolecek 
    954  1.21  jdolecek static void
    955  1.21  jdolecek filt_sequencerwdetach(struct knote *kn)
    956  1.21  jdolecek {
    957  1.21  jdolecek 	struct sequencer_softc *sc = kn->kn_hook;
    958  1.21  jdolecek 
    959  1.53  jmcneill 	mutex_enter(&sc->lock);
    960  1.73   thorpej 	selremove_knote(&sc->wsel, kn);
    961  1.53  jmcneill 	mutex_exit(&sc->lock);
    962  1.21  jdolecek }
    963  1.21  jdolecek 
    964  1.21  jdolecek static int
    965  1.37  christos filt_sequencerwrite(struct knote *kn, long hint)
    966  1.21  jdolecek {
    967  1.21  jdolecek 	struct sequencer_softc *sc = kn->kn_hook;
    968  1.53  jmcneill 	int rv;
    969  1.21  jdolecek 
    970  1.53  jmcneill 	if (hint != NOTE_SUBMIT) {
    971  1.53  jmcneill 		mutex_enter(&sc->lock);
    972  1.53  jmcneill 	}
    973  1.53  jmcneill 	if (SEQ_QLEN(&sc->outq) >= sc->lowat) {
    974  1.53  jmcneill 		rv = 0;
    975  1.53  jmcneill 	} else {
    976  1.53  jmcneill 		kn->kn_data = sizeof(seq_event_rec);
    977  1.53  jmcneill 		rv = 1;
    978  1.53  jmcneill 	}
    979  1.53  jmcneill 	if (hint != NOTE_SUBMIT) {
    980  1.53  jmcneill 		mutex_exit(&sc->lock);
    981  1.53  jmcneill 	}
    982  1.53  jmcneill 	return rv;
    983  1.21  jdolecek }
    984  1.21  jdolecek 
    985  1.67      maya static const struct filterops sequencerwrite_filtops = {
    986  1.74   thorpej 	.f_flags = FILTEROP_ISFD,
    987  1.67      maya 	.f_attach = NULL,
    988  1.67      maya 	.f_detach = filt_sequencerwdetach,
    989  1.67      maya 	.f_event = filt_sequencerwrite,
    990  1.67      maya };
    991  1.21  jdolecek 
    992  1.32      chap static int
    993  1.21  jdolecek sequencerkqfilter(dev_t dev, struct knote *kn)
    994  1.21  jdolecek {
    995  1.56  christos 	struct sequencer_softc *sc;
    996  1.73   thorpej 	struct selinfo *sip;
    997  1.77  riastrad 
    998  1.56  christos 	if ((sc = sequencerget(SEQUENCERUNIT(dev))) == NULL)
    999  1.56  christos 		return ENXIO;
   1000  1.21  jdolecek 
   1001  1.21  jdolecek 	switch (kn->kn_filter) {
   1002  1.21  jdolecek 	case EVFILT_READ:
   1003  1.73   thorpej 		sip = &sc->rsel;
   1004  1.21  jdolecek 		kn->kn_fop = &sequencerread_filtops;
   1005  1.21  jdolecek 		break;
   1006  1.21  jdolecek 
   1007  1.21  jdolecek 	case EVFILT_WRITE:
   1008  1.73   thorpej 		sip = &sc->wsel;
   1009  1.21  jdolecek 		kn->kn_fop = &sequencerwrite_filtops;
   1010  1.21  jdolecek 		break;
   1011  1.21  jdolecek 
   1012  1.21  jdolecek 	default:
   1013  1.77  riastrad 		return EINVAL;
   1014  1.21  jdolecek 	}
   1015  1.21  jdolecek 
   1016  1.21  jdolecek 	kn->kn_hook = sc;
   1017  1.21  jdolecek 
   1018  1.53  jmcneill 	mutex_enter(&sc->lock);
   1019  1.73   thorpej 	selrecord_knote(sip, kn);
   1020  1.53  jmcneill 	mutex_exit(&sc->lock);
   1021  1.21  jdolecek 
   1022  1.77  riastrad 	return 0;
   1023   1.1  augustss }
   1024   1.1  augustss 
   1025  1.32      chap static void
   1026  1.32      chap seq_reset(struct sequencer_softc *sc)
   1027   1.1  augustss {
   1028   1.1  augustss 	int i, chn;
   1029   1.1  augustss 	struct midi_dev *md;
   1030   1.1  augustss 
   1031  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1032  1.53  jmcneill 
   1033  1.60       mrg 	if (!(sc->flags & FWRITE))
   1034  1.32      chap 	        return;
   1035   1.1  augustss 	for (i = 0; i < sc->nmidi; i++) {
   1036   1.1  augustss 		md = sc->devs[i];
   1037   1.3  augustss 		midiseq_reset(md);
   1038   1.1  augustss 		for (chn = 0; chn < MAXCHAN; chn++) {
   1039  1.32      chap 			midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE,
   1040  1.32      chap 			    .controller=MIDI_CTRL_NOTES_OFF));
   1041  1.32      chap 			midiseq_ctlchange(md, chn, &SEQ_MK_CHN(CTL_CHANGE,
   1042  1.32      chap 			    .controller=MIDI_CTRL_RESET));
   1043  1.32      chap 			midiseq_pitchbend(md, chn, &SEQ_MK_CHN(PITCH_BEND,
   1044  1.32      chap 			    .value=MIDI_BEND_NEUTRAL));
   1045   1.1  augustss 		}
   1046   1.1  augustss 	}
   1047   1.1  augustss }
   1048   1.1  augustss 
   1049  1.32      chap static int
   1050  1.32      chap seq_do_command(struct sequencer_softc *sc, seq_event_t *b)
   1051   1.1  augustss {
   1052   1.1  augustss 	int dev;
   1053   1.1  augustss 
   1054  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1055  1.53  jmcneill 
   1056  1.33  christos 	DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, b->timing.op));
   1057   1.1  augustss 
   1058  1.32      chap 	switch(b->tag) {
   1059   1.1  augustss 	case SEQ_LOCAL:
   1060   1.1  augustss 		return seq_do_local(sc, b);
   1061   1.1  augustss 	case SEQ_TIMING:
   1062   1.1  augustss 		return seq_do_timing(sc, b);
   1063   1.1  augustss 	case SEQ_CHN_VOICE:
   1064   1.1  augustss 		return seq_do_chnvoice(sc, b);
   1065   1.1  augustss 	case SEQ_CHN_COMMON:
   1066   1.1  augustss 		return seq_do_chncommon(sc, b);
   1067   1.4  augustss 	case SEQ_SYSEX:
   1068   1.4  augustss 		return seq_do_sysex(sc, b);
   1069   1.1  augustss 	/* COMPAT */
   1070   1.1  augustss 	case SEQOLD_MIDIPUTC:
   1071  1.32      chap 		dev = b->putc.device;
   1072   1.1  augustss 		if (dev < 0 || dev >= sc->nmidi)
   1073  1.77  riastrad 			return ENXIO;
   1074  1.32      chap 		return midiseq_out(sc->devs[dev], &b->putc.byte, 1, 0);
   1075   1.1  augustss 	default:
   1076  1.32      chap 		DPRINTFN(-1,("seq_do_command: unimpl command %02x\n", b->tag));
   1077  1.77  riastrad 		return EINVAL;
   1078   1.1  augustss 	}
   1079   1.1  augustss }
   1080   1.1  augustss 
   1081  1.32      chap static int
   1082  1.32      chap seq_do_chnvoice(struct sequencer_softc *sc, seq_event_t *b)
   1083   1.1  augustss {
   1084  1.32      chap 	int dev;
   1085   1.1  augustss 	int error;
   1086   1.1  augustss 	struct midi_dev *md;
   1087   1.1  augustss 
   1088  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1089  1.53  jmcneill 
   1090  1.32      chap 	dev = b->voice.device;
   1091  1.32      chap 	if (dev < 0 || dev >= sc->nmidi ||
   1092  1.32      chap 	    b->voice.channel > 15 ||
   1093  1.32      chap 	    b->voice.key >= SEQ_NOTE_MAX)
   1094   1.1  augustss 		return ENXIO;
   1095   1.1  augustss 	md = sc->devs[dev];
   1096  1.32      chap 	switch(b->voice.op) {
   1097  1.32      chap 	case MIDI_NOTEON: /* no need to special-case hidden noteoff here */
   1098  1.32      chap 		error = midiseq_noteon(md, b->voice.channel, b->voice.key, b);
   1099   1.1  augustss 		break;
   1100   1.1  augustss 	case MIDI_NOTEOFF:
   1101  1.32      chap 		error = midiseq_noteoff(md, b->voice.channel, b->voice.key, b);
   1102   1.1  augustss 		break;
   1103   1.1  augustss 	case MIDI_KEY_PRESSURE:
   1104  1.32      chap 		error = midiseq_keypressure(md,
   1105  1.32      chap 		    b->voice.channel, b->voice.key, b);
   1106   1.1  augustss 		break;
   1107   1.1  augustss 	default:
   1108  1.32      chap 		DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n",
   1109  1.32      chap 			b->voice.op));
   1110   1.1  augustss 		error = EINVAL;
   1111   1.1  augustss 		break;
   1112   1.1  augustss 	}
   1113   1.1  augustss 	return error;
   1114   1.1  augustss }
   1115   1.1  augustss 
   1116  1.32      chap static int
   1117  1.32      chap seq_do_chncommon(struct sequencer_softc *sc, seq_event_t *b)
   1118   1.1  augustss {
   1119  1.32      chap 	int dev;
   1120   1.1  augustss 	int error;
   1121   1.1  augustss 	struct midi_dev *md;
   1122   1.1  augustss 
   1123  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1124  1.53  jmcneill 
   1125  1.32      chap 	dev = b->common.device;
   1126  1.32      chap 	if (dev < 0 || dev >= sc->nmidi ||
   1127  1.32      chap 	    b->common.channel > 15)
   1128   1.1  augustss 		return ENXIO;
   1129   1.1  augustss 	md = sc->devs[dev];
   1130  1.32      chap 	DPRINTFN(2,("seq_do_chncommon: %02x\n", b->common.op));
   1131   1.1  augustss 
   1132   1.1  augustss 	error = 0;
   1133  1.32      chap 	switch(b->common.op) {
   1134   1.1  augustss 	case MIDI_PGM_CHANGE:
   1135  1.32      chap 		error = midiseq_pgmchange(md, b->common.channel, b);
   1136   1.1  augustss 		break;
   1137   1.1  augustss 	case MIDI_CTL_CHANGE:
   1138  1.32      chap 		error = midiseq_ctlchange(md, b->common.channel, b);
   1139   1.1  augustss 		break;
   1140   1.1  augustss 	case MIDI_PITCH_BEND:
   1141  1.32      chap 		error = midiseq_pitchbend(md, b->common.channel, b);
   1142   1.1  augustss 		break;
   1143   1.8  augustss 	case MIDI_CHN_PRESSURE:
   1144  1.32      chap 		error = midiseq_chnpressure(md, b->common.channel, b);
   1145   1.8  augustss 		break;
   1146   1.1  augustss 	default:
   1147  1.32      chap 		DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n",
   1148  1.32      chap 			b->common.op));
   1149   1.1  augustss 		error = EINVAL;
   1150   1.1  augustss 		break;
   1151   1.1  augustss 	}
   1152  1.32      chap 	return error;
   1153   1.1  augustss }
   1154   1.1  augustss 
   1155  1.32      chap static int
   1156  1.37  christos seq_do_local(struct sequencer_softc *sc, seq_event_t *b)
   1157   1.1  augustss {
   1158  1.53  jmcneill 
   1159  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1160  1.53  jmcneill 
   1161  1.77  riastrad 	return EINVAL;
   1162   1.4  augustss }
   1163   1.4  augustss 
   1164  1.32      chap static int
   1165  1.32      chap seq_do_sysex(struct sequencer_softc *sc, seq_event_t *b)
   1166   1.4  augustss {
   1167   1.4  augustss 	int dev, i;
   1168   1.4  augustss 	struct midi_dev *md;
   1169  1.32      chap 	uint8_t *bf = b->sysex.buffer;
   1170   1.4  augustss 
   1171  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1172  1.53  jmcneill 
   1173  1.32      chap 	dev = b->sysex.device;
   1174   1.4  augustss 	if (dev < 0 || dev >= sc->nmidi)
   1175  1.77  riastrad 		return ENXIO;
   1176  1.61       mrg 	DPRINTF(("%s: dev=%d\n", __func__, dev));
   1177   1.4  augustss 	md = sc->devs[dev];
   1178   1.4  augustss 
   1179  1.32      chap 	if (!md->doingsysex) {
   1180  1.32      chap 		midiseq_out(md, (uint8_t[]){MIDI_SYSEX_START}, 1, 0);
   1181  1.32      chap 		md->doingsysex = 1;
   1182   1.4  augustss 	}
   1183   1.4  augustss 
   1184  1.28  christos 	for (i = 0; i < 6 && bf[i] != 0xff; i++)
   1185   1.4  augustss 		;
   1186  1.28  christos 	midiseq_out(md, bf, i, 0);
   1187  1.28  christos 	if (i < 6 || (i > 0 && bf[i-1] == MIDI_SYSEX_END))
   1188  1.32      chap 		md->doingsysex = 0;
   1189  1.32      chap 	return 0;
   1190  1.32      chap }
   1191  1.32      chap 
   1192  1.32      chap static void
   1193  1.32      chap seq_timer_waitabs(struct sequencer_softc *sc, uint32_t divs)
   1194  1.32      chap {
   1195  1.32      chap 	struct timeval when;
   1196  1.32      chap 	long long usec;
   1197  1.32      chap 	struct syn_timer *t;
   1198  1.32      chap 	int ticks;
   1199  1.32      chap 
   1200  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1201  1.53  jmcneill 
   1202  1.32      chap 	t = &sc->timer;
   1203  1.32      chap 	t->divs_lastevent = divs;
   1204  1.32      chap 	divs -= t->divs_lastchange;
   1205  1.32      chap 	usec = (long long)divs * (long long)t->usperdiv; /* convert to usec */
   1206  1.32      chap 	when.tv_sec = usec / 1000000;
   1207  1.32      chap 	when.tv_usec = usec % 1000000;
   1208  1.51    cegger 	DPRINTFN(4, ("seq_timer_waitabs: adjdivs=%d, sleep when=%"PRId64".%06"PRId64,
   1209  1.51    cegger 	             divs, when.tv_sec, (uint64_t)when.tv_usec));
   1210  1.32      chap 	ADDTIMEVAL(&when, &t->reftime); /* abstime for end */
   1211  1.50  christos 	ticks = tvhzto(&when);
   1212  1.51    cegger 	DPRINTFN(4, (" when+start=%"PRId64".%06"PRId64", tick=%d\n",
   1213  1.51    cegger 		     when.tv_sec, (uint64_t)when.tv_usec, ticks));
   1214  1.32      chap 	if (ticks > 0) {
   1215  1.32      chap #ifdef DIAGNOSTIC
   1216  1.32      chap 		if (ticks > 20 * hz) {
   1217  1.32      chap 			/* Waiting more than 20s */
   1218  1.32      chap 			printf("seq_timer_waitabs: funny ticks=%d, "
   1219  1.32      chap 			       "usec=%lld\n", ticks, usec);
   1220  1.32      chap 		}
   1221  1.32      chap #endif
   1222  1.32      chap 		sc->timeout = 1;
   1223  1.32      chap 		callout_reset(&sc->sc_callout, ticks,
   1224  1.32      chap 		    seq_timeout, sc);
   1225  1.32      chap 	}
   1226  1.32      chap #ifdef SEQUENCER_DEBUG
   1227  1.83     oster 	else if (tick < 0)
   1228  1.61       mrg 		DPRINTF(("%s: ticks = %d\n", __func__, ticks));
   1229  1.32      chap #endif
   1230   1.1  augustss }
   1231   1.1  augustss 
   1232  1.32      chap static int
   1233  1.32      chap seq_do_timing(struct sequencer_softc *sc, seq_event_t *b)
   1234   1.1  augustss {
   1235   1.1  augustss 	struct syn_timer *t = &sc->timer;
   1236   1.1  augustss 	struct timeval when;
   1237   1.1  augustss 	int error;
   1238   1.1  augustss 
   1239  1.53  jmcneill 	KASSERT(mutex_owned(&sc->lock));
   1240  1.53  jmcneill 
   1241   1.1  augustss 	error = 0;
   1242  1.32      chap 	switch(b->timing.op) {
   1243   1.1  augustss 	case TMR_WAIT_REL:
   1244  1.32      chap 		seq_timer_waitabs(sc,
   1245  1.53  jmcneill 		    b->t_WAIT_REL.divisions + t->divs_lastevent);
   1246  1.32      chap 		break;
   1247   1.1  augustss 	case TMR_WAIT_ABS:
   1248  1.32      chap 		seq_timer_waitabs(sc, b->t_WAIT_ABS.divisions);
   1249   1.1  augustss 		break;
   1250   1.1  augustss 	case TMR_START:
   1251  1.32      chap 		microtime(&t->reftime);
   1252  1.32      chap 		t->divs_lastevent = t->divs_lastchange = 0;
   1253   1.1  augustss 		t->running = 1;
   1254   1.1  augustss 		break;
   1255   1.1  augustss 	case TMR_STOP:
   1256  1.32      chap 		microtime(&t->stoptime);
   1257   1.1  augustss 		t->running = 0;
   1258   1.1  augustss 		break;
   1259   1.1  augustss 	case TMR_CONTINUE:
   1260  1.32      chap 		if (t->running)
   1261  1.32      chap 			break;
   1262   1.1  augustss 		microtime(&when);
   1263  1.32      chap 		SUBTIMEVAL(&when, &t->stoptime);
   1264  1.32      chap 		ADDTIMEVAL(&t->reftime, &when);
   1265   1.1  augustss 		t->running = 1;
   1266   1.1  augustss 		break;
   1267   1.1  augustss 	case TMR_TEMPO:
   1268  1.32      chap 		/* bpm is unambiguously MIDI clocks per minute / 24 */
   1269  1.32      chap 		/* (24 MIDI clocks are usually but not always a quarter note) */
   1270  1.32      chap 		if (b->t_TEMPO.bpm < 8) /* where are these limits specified? */
   1271  1.32      chap 			t->tempo_beatpermin = 8;
   1272  1.32      chap 		else if (b->t_TEMPO.bpm > 360) /* ? */
   1273  1.32      chap 			t->tempo_beatpermin = 360;
   1274  1.32      chap 		else
   1275  1.32      chap 			t->tempo_beatpermin = b->t_TEMPO.bpm;
   1276  1.32      chap 		t->divs_lastchange = t->divs_lastevent;
   1277  1.32      chap 		microtime(&t->reftime);
   1278  1.32      chap 		RECALC_USPERDIV(t);
   1279   1.1  augustss 		break;
   1280   1.1  augustss 	case TMR_ECHO:
   1281   1.1  augustss 		error = seq_input_event(sc, b);
   1282   1.1  augustss 		break;
   1283   1.1  augustss 	case TMR_RESET:
   1284  1.32      chap 		t->divs_lastevent = t->divs_lastchange = 0;
   1285  1.32      chap 		microtime(&t->reftime);
   1286  1.32      chap 		break;
   1287  1.32      chap 	case TMR_SPP:
   1288  1.32      chap 	case TMR_TIMESIG:
   1289  1.61       mrg 		DPRINTF(("%s: unimplemented %02x\n", __func__, b->timing.op));
   1290  1.32      chap 		error = EINVAL; /* not quite accurate... */
   1291   1.1  augustss 		break;
   1292   1.1  augustss 	default:
   1293  1.61       mrg 		DPRINTF(("%s: unknown %02x\n", __func__, b->timing.op));
   1294   1.1  augustss 		error = EINVAL;
   1295   1.1  augustss 		break;
   1296   1.1  augustss 	}
   1297  1.77  riastrad 	return error;
   1298   1.1  augustss }
   1299   1.1  augustss 
   1300  1.32      chap static int
   1301  1.32      chap seq_do_fullsize(struct sequencer_softc *sc, seq_event_t *b, struct uio *uio)
   1302   1.1  augustss {
   1303   1.1  augustss 	struct sysex_info sysex;
   1304   1.1  augustss 	u_int dev;
   1305   1.1  augustss 
   1306  1.78  riastrad 	CTASSERT(sizeof(seq_event_rec) == SEQ_SYSEX_HDRSIZE);
   1307  1.71      maxv 	memcpy(&sysex, b, sizeof(*b));
   1308   1.1  augustss 	dev = sysex.device_no;
   1309  1.34  christos 	if (/* dev < 0 || */ dev >= sc->nmidi)
   1310  1.77  riastrad 		return ENXIO;
   1311   1.1  augustss 	DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
   1312   1.1  augustss 		     sysex.key, dev, sysex.len));
   1313  1.77  riastrad 	return midiseq_loadpatch(sc->devs[dev], &sysex, uio);
   1314   1.1  augustss }
   1315   1.1  augustss 
   1316  1.32      chap /*
   1317  1.32      chap  * Convert an old sequencer event to a new one.
   1318  1.32      chap  * NOTE: on entry, *ev may contain valid data only in the first 4 bytes.
   1319  1.32      chap  * That may be true even on exit (!) in the case of SEQOLD_MIDIPUTC; the
   1320  1.32      chap  * caller will only look at the first bytes in that case anyway. Ugly? Sure.
   1321  1.32      chap  */
   1322  1.32      chap static int
   1323  1.32      chap seq_to_new(seq_event_t *ev, struct uio *uio)
   1324   1.1  augustss {
   1325   1.1  augustss 	int cmd, chan, note, parm;
   1326  1.32      chap 	uint32_t tmp_delay;
   1327   1.1  augustss 	int error;
   1328  1.32      chap 	uint8_t *bfp;
   1329   1.1  augustss 
   1330  1.32      chap 	cmd = ev->tag;
   1331  1.32      chap 	bfp = ev->unknown.byte;
   1332  1.32      chap 	chan = *bfp++;
   1333  1.32      chap 	note = *bfp++;
   1334  1.32      chap 	parm = *bfp++;
   1335   1.1  augustss 	DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm));
   1336   1.1  augustss 
   1337   1.1  augustss 	if (cmd >= 0x80) {
   1338   1.1  augustss 		/* Fill the event record */
   1339   1.1  augustss 		if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) {
   1340  1.32      chap 			error = uiomove(bfp, sizeof *ev - SEQOLD_CMDSIZE, uio);
   1341   1.1  augustss 			if (error)
   1342   1.1  augustss 				return error;
   1343   1.1  augustss 		} else
   1344   1.1  augustss 			return EINVAL;
   1345   1.1  augustss 	}
   1346   1.1  augustss 
   1347   1.1  augustss 	switch(cmd) {
   1348   1.1  augustss 	case SEQOLD_NOTEOFF:
   1349  1.32      chap 		/*
   1350  1.32      chap 		 * What's with the SEQ_NOTE_XXX?  In OSS this seems to have
   1351  1.32      chap 		 * been undocumented magic for messing with the overall volume
   1352  1.32      chap 		 * of a 'voice', equated precariously with 'channel' and
   1353  1.32      chap 		 * pretty much unimplementable except by directly frobbing a
   1354  1.32      chap 		 * synth chip. For us, who treat everything as interfaced over
   1355  1.32      chap 		 * MIDI, this will just be unceremoniously discarded as
   1356  1.32      chap 		 * invalid in midiseq_noteoff, making the whole event an
   1357  1.32      chap 		 * elaborate no-op, and that doesn't seem to be any different
   1358  1.32      chap 		 * from what happens on linux with a MIDI-interfaced device,
   1359  1.32      chap 		 * by the way. The moral is ... use the new /dev/music API, ok?
   1360  1.32      chap 		 */
   1361  1.32      chap 		*ev = SEQ_MK_CHN(NOTEOFF, .device=0, .channel=chan,
   1362  1.32      chap 		    .key=SEQ_NOTE_XXX, .velocity=parm);
   1363  1.32      chap 		break;
   1364   1.1  augustss 	case SEQOLD_NOTEON:
   1365  1.32      chap 		*ev = SEQ_MK_CHN(NOTEON,
   1366  1.32      chap 		    .device=0, .channel=chan, .key=note, .velocity=parm);
   1367   1.1  augustss 		break;
   1368   1.1  augustss 	case SEQOLD_WAIT:
   1369  1.32      chap 		/*
   1370  1.32      chap 		 * This event cannot even /exist/ on non-littleendian machines,
   1371  1.32      chap 		 * and so help me, that's exactly the way OSS defined it.
   1372  1.32      chap 		 * Also, the OSS programmer's guide states (p. 74, v1.11)
   1373  1.32      chap 		 * that seqold time units are system clock ticks, unlike
   1374  1.32      chap 		 * the new 'divisions' which are determined by timebase. In
   1375  1.32      chap 		 * that case we would need to do scaling here - but no such
   1376  1.32      chap 		 * behavior is visible in linux either--which also treats this
   1377  1.32      chap 		 * value, surprisingly, as an absolute, not relative, time.
   1378  1.32      chap 		 * My guess is that this event has gone unused so long that
   1379  1.32      chap 		 * nobody could agree we got it wrong no matter what we do.
   1380  1.32      chap 		 */
   1381  1.32      chap 		tmp_delay = *(uint32_t *)ev >> 8;
   1382  1.32      chap 		*ev = SEQ_MK_TIMING(WAIT_ABS, .divisions=tmp_delay);
   1383   1.1  augustss 		break;
   1384   1.1  augustss 	case SEQOLD_SYNCTIMER:
   1385  1.32      chap 		/*
   1386  1.32      chap 		 * The TMR_RESET event is not defined in any OSS materials
   1387  1.32      chap 		 * I can find; it may have been invented here just to provide
   1388  1.32      chap 		 * an accurate _to_new translation of this event.
   1389  1.32      chap 		 */
   1390  1.32      chap 		*ev = SEQ_MK_TIMING(RESET);
   1391   1.1  augustss 		break;
   1392   1.1  augustss 	case SEQOLD_PGMCHANGE:
   1393  1.32      chap 		*ev = SEQ_MK_CHN(PGM_CHANGE,
   1394  1.32      chap 		    .device=0, .channel=chan, .program=note);
   1395   1.1  augustss 		break;
   1396   1.1  augustss 	case SEQOLD_MIDIPUTC:
   1397   1.1  augustss 		break;		/* interpret in normal mode */
   1398   1.1  augustss 	case SEQOLD_ECHO:
   1399   1.1  augustss 	case SEQOLD_PRIVATE:
   1400   1.1  augustss 	case SEQOLD_EXTENDED:
   1401   1.1  augustss 	default:
   1402  1.61       mrg 		DPRINTF(("%s: not impl 0x%02x\n", __func__, cmd));
   1403   1.1  augustss 		return EINVAL;
   1404  1.32      chap 	/* In case new-style events show up */
   1405   1.1  augustss 	case SEQ_TIMING:
   1406   1.1  augustss 	case SEQ_CHN_VOICE:
   1407   1.1  augustss 	case SEQ_CHN_COMMON:
   1408   1.1  augustss 	case SEQ_FULLSIZE:
   1409   1.1  augustss 		break;
   1410   1.1  augustss 	}
   1411   1.1  augustss 	return 0;
   1412   1.1  augustss }
   1413   1.1  augustss 
   1414   1.1  augustss /**********************************************/
   1415   1.1  augustss 
   1416   1.1  augustss void
   1417  1.37  christos midiseq_in(struct midi_dev *md, u_char *msg, int len)
   1418   1.1  augustss {
   1419  1.53  jmcneill 	struct sequencer_softc *sc;
   1420  1.53  jmcneill 	sequencer_pcqitem_t qi;
   1421   1.1  augustss 
   1422  1.27     perry 	DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
   1423   1.1  augustss 		     md, msg[0], msg[1], msg[2]));
   1424   1.1  augustss 
   1425  1.53  jmcneill 	sc = md->seq;
   1426  1.53  jmcneill 
   1427  1.53  jmcneill 	qi.qi_msg[0] = msg[0];
   1428  1.53  jmcneill 	qi.qi_msg[1] = msg[1];
   1429  1.53  jmcneill 	qi.qi_msg[2] = msg[2];
   1430  1.53  jmcneill 	qi.qi_msg[3] = md->unit | 0x80;	/* ensure non-zero value of qi_ptr */
   1431  1.53  jmcneill 	pcq_put(sc->pcq, qi.qi_ptr);
   1432  1.53  jmcneill 	softint_schedule(sc->sih);
   1433   1.1  augustss }
   1434   1.1  augustss 
   1435  1.32      chap static struct midi_dev *
   1436  1.32      chap midiseq_open(int unit, int flags)
   1437   1.1  augustss {
   1438   1.1  augustss 	int error;
   1439   1.1  augustss 	struct midi_dev *md;
   1440   1.1  augustss 	struct midi_softc *sc;
   1441   1.1  augustss 	struct midi_info mi;
   1442  1.41        ad 	int major;
   1443  1.41        ad 	dev_t dev;
   1444  1.53  jmcneill 	vnode_t *vp;
   1445  1.54       mrg 	int oflags;
   1446  1.77  riastrad 
   1447  1.46    plunky 	major = devsw_name2chr("midi", NULL, 0);
   1448  1.41        ad 	dev = makedev(major, unit);
   1449   1.1  augustss 
   1450  1.53  jmcneill 	DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags));
   1451  1.53  jmcneill 
   1452  1.53  jmcneill 	error = cdevvp(dev, &vp);
   1453  1.53  jmcneill 	if (error)
   1454  1.53  jmcneill 		return NULL;
   1455  1.53  jmcneill 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1456  1.53  jmcneill 	error = VOP_OPEN(vp, flags, kauth_cred_get());
   1457  1.53  jmcneill 	VOP_UNLOCK(vp);
   1458  1.53  jmcneill 	if (error) {
   1459  1.53  jmcneill 		vrele(vp);
   1460  1.53  jmcneill 		return NULL;
   1461  1.53  jmcneill 	}
   1462  1.53  jmcneill 
   1463  1.53  jmcneill 	/* Only after we have acquired reference via VOP_OPEN(). */
   1464  1.41        ad 	midi_getinfo(dev, &mi);
   1465  1.54       mrg 	oflags = flags;
   1466  1.53  jmcneill 	if ((mi.props & MIDI_PROP_CAN_INPUT) == 0)
   1467  1.32      chap 	        flags &= ~FREAD;
   1468  1.53  jmcneill 	if ((flags & (FREAD|FWRITE)) == 0) {
   1469  1.79  riastrad 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1470  1.54       mrg 		VOP_CLOSE(vp, oflags, kauth_cred_get());
   1471  1.79  riastrad 		VOP_UNLOCK(vp);
   1472  1.53  jmcneill 		vrele(vp);
   1473  1.53  jmcneill 	        return NULL;
   1474  1.53  jmcneill 	}
   1475  1.53  jmcneill 
   1476  1.49    cegger 	sc = device_lookup_private(&midi_cd, unit);
   1477  1.53  jmcneill 	md = kmem_zalloc(sizeof(*md), KM_SLEEP);
   1478   1.1  augustss 	md->unit = unit;
   1479   1.1  augustss 	md->name = mi.name;
   1480   1.1  augustss 	md->subtype = 0;
   1481   1.2  augustss 	md->nr_voices = 128;	/* XXX */
   1482   1.2  augustss 	md->instr_bank_size = 128; /* XXX */
   1483  1.53  jmcneill 	md->vp = vp;
   1484   1.1  augustss 	if (mi.props & MIDI_PROP_CAN_INPUT)
   1485   1.1  augustss 		md->capabilities |= SYNTH_CAP_INPUT;
   1486  1.53  jmcneill 	sc->seq_md = md;
   1487  1.77  riastrad 	return md;
   1488   1.1  augustss }
   1489   1.1  augustss 
   1490  1.32      chap static void
   1491  1.32      chap midiseq_close(struct midi_dev *md)
   1492   1.1  augustss {
   1493  1.77  riastrad 
   1494   1.3  augustss 	DPRINTFN(2, ("midiseq_close: %d\n", md->unit));
   1495  1.53  jmcneill 	(void)vn_close(md->vp, 0, kauth_cred_get());
   1496  1.53  jmcneill 	kmem_free(md, sizeof(*md));
   1497   1.1  augustss }
   1498   1.1  augustss 
   1499  1.32      chap static void
   1500  1.37  christos midiseq_reset(struct midi_dev *md)
   1501   1.1  augustss {
   1502   1.5  augustss 	/* XXX send GM reset? */
   1503   1.3  augustss 	DPRINTFN(3, ("midiseq_reset: %d\n", md->unit));
   1504   1.1  augustss }
   1505   1.1  augustss 
   1506  1.32      chap static int
   1507  1.37  christos midiseq_out(struct midi_dev *md, u_char *bf, u_int cc, int chk)
   1508   1.1  augustss {
   1509  1.60       mrg 	DPRINTFN(5, ("midiseq_out: md=%p, unit=%d, bf[0]=0x%02x, cc=%d\n",
   1510  1.60       mrg 		     md, md->unit, bf[0], cc));
   1511  1.10  augustss 
   1512  1.32      chap 	/* midi(4) does running status compression where appropriate. */
   1513  1.28  christos 	return midi_writebytes(md->unit, bf, cc);
   1514   1.1  augustss }
   1515   1.1  augustss 
   1516  1.32      chap /*
   1517  1.32      chap  * If the writing process hands us a hidden note-off in a note-on event,
   1518  1.32      chap  * we will simply write it that way; no need to special case it here,
   1519  1.32      chap  * as midi(4) will always canonicalize or compress as appropriate anyway.
   1520  1.32      chap  */
   1521  1.32      chap static int
   1522  1.32      chap midiseq_noteon(struct midi_dev *md, int chan, int key, seq_event_t *ev)
   1523   1.1  augustss {
   1524  1.77  riastrad 
   1525  1.32      chap 	return midiseq_out(md, (uint8_t[]){
   1526  1.32      chap 	    MIDI_NOTEON | chan, key, ev->c_NOTEON.velocity & 0x7f}, 3, 1);
   1527   1.1  augustss }
   1528   1.1  augustss 
   1529  1.32      chap static int
   1530  1.32      chap midiseq_noteoff(struct midi_dev *md, int chan, int key, seq_event_t *ev)
   1531   1.1  augustss {
   1532  1.77  riastrad 
   1533  1.32      chap 	return midiseq_out(md, (uint8_t[]){
   1534  1.32      chap 	    MIDI_NOTEOFF | chan, key, ev->c_NOTEOFF.velocity & 0x7f}, 3, 1);
   1535   1.1  augustss }
   1536   1.1  augustss 
   1537  1.32      chap static int
   1538  1.32      chap midiseq_keypressure(struct midi_dev *md, int chan, int key, seq_event_t *ev)
   1539   1.1  augustss {
   1540  1.77  riastrad 
   1541  1.32      chap 	return midiseq_out(md, (uint8_t[]){
   1542  1.32      chap 	    MIDI_KEY_PRESSURE | chan, key,
   1543  1.32      chap 	    ev->c_KEY_PRESSURE.pressure & 0x7f}, 3, 1);
   1544   1.1  augustss }
   1545   1.1  augustss 
   1546  1.32      chap static int
   1547  1.32      chap midiseq_pgmchange(struct midi_dev *md, int chan, seq_event_t *ev)
   1548   1.1  augustss {
   1549  1.77  riastrad 
   1550  1.32      chap 	if (ev->c_PGM_CHANGE.program > 127)
   1551   1.1  augustss 		return EINVAL;
   1552  1.32      chap 	return midiseq_out(md, (uint8_t[]){
   1553  1.32      chap 	    MIDI_PGM_CHANGE | chan, ev->c_PGM_CHANGE.program}, 2, 1);
   1554   1.8  augustss }
   1555   1.8  augustss 
   1556  1.32      chap static int
   1557  1.32      chap midiseq_chnpressure(struct midi_dev *md, int chan, seq_event_t *ev)
   1558   1.8  augustss {
   1559  1.77  riastrad 
   1560  1.32      chap 	if (ev->c_CHN_PRESSURE.pressure > 127)
   1561   1.8  augustss 		return EINVAL;
   1562  1.32      chap 	return midiseq_out(md, (uint8_t[]){
   1563  1.32      chap 	    MIDI_CHN_PRESSURE | chan, ev->c_CHN_PRESSURE.pressure}, 2, 1);
   1564   1.1  augustss }
   1565   1.1  augustss 
   1566  1.32      chap static int
   1567  1.32      chap midiseq_ctlchange(struct midi_dev *md, int chan, seq_event_t *ev)
   1568   1.1  augustss {
   1569  1.77  riastrad 
   1570  1.32      chap 	if (ev->c_CTL_CHANGE.controller > 127)
   1571   1.1  augustss 		return EINVAL;
   1572  1.32      chap 	return midiseq_out( md, (uint8_t[]){
   1573  1.32      chap 	    MIDI_CTL_CHANGE | chan, ev->c_CTL_CHANGE.controller,
   1574  1.32      chap 	    ev->c_CTL_CHANGE.value & 0x7f /* XXX this is SO wrong */
   1575  1.32      chap 	    }, 3, 1);
   1576   1.1  augustss }
   1577   1.1  augustss 
   1578  1.32      chap static int
   1579  1.32      chap midiseq_pitchbend(struct midi_dev *md, int chan, seq_event_t *ev)
   1580   1.1  augustss {
   1581  1.77  riastrad 
   1582  1.32      chap 	return midiseq_out(md, (uint8_t[]){
   1583  1.32      chap 	    MIDI_PITCH_BEND | chan,
   1584  1.32      chap 	    ev->c_PITCH_BEND.value & 0x7f,
   1585  1.32      chap 	    (ev->c_PITCH_BEND.value >> 7) & 0x7f}, 3, 1);
   1586   1.1  augustss }
   1587   1.1  augustss 
   1588  1.32      chap static int
   1589  1.32      chap midiseq_loadpatch(struct midi_dev *md,
   1590  1.32      chap                   struct sysex_info *sysex, struct uio *uio)
   1591   1.1  augustss {
   1592  1.53  jmcneill 	struct sequencer_softc *sc;
   1593  1.28  christos 	u_char c, bf[128];
   1594   1.1  augustss 	int i, cc, error;
   1595   1.1  augustss 
   1596   1.5  augustss 	if (sysex->key != SEQ_SYSEX_PATCH) {
   1597   1.5  augustss 		DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n",
   1598   1.5  augustss 			     sysex->key));
   1599  1.77  riastrad 		return EINVAL;
   1600   1.5  augustss 	}
   1601   1.1  augustss 	if (uio->uio_resid < sysex->len)
   1602   1.1  augustss 		/* adjust length, should be an error */
   1603   1.1  augustss 		sysex->len = uio->uio_resid;
   1604   1.1  augustss 
   1605   1.3  augustss 	DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len));
   1606   1.1  augustss 	if (sysex->len == 0)
   1607   1.1  augustss 		return EINVAL;
   1608   1.1  augustss 	error = uiomove(&c, 1, uio);
   1609   1.1  augustss 	if (error)
   1610   1.1  augustss 		return error;
   1611   1.1  augustss 	if (c != MIDI_SYSEX_START)		/* must start like this */
   1612   1.1  augustss 		return EINVAL;
   1613  1.53  jmcneill 	sc = md->seq;
   1614  1.53  jmcneill 	mutex_enter(&sc->lock);
   1615   1.7  augustss 	error = midiseq_out(md, &c, 1, 0);
   1616  1.53  jmcneill 	mutex_exit(&sc->lock);
   1617   1.1  augustss 	if (error)
   1618   1.1  augustss 		return error;
   1619   1.1  augustss 	--sysex->len;
   1620   1.1  augustss 	while (sysex->len > 0) {
   1621   1.1  augustss 		cc = sysex->len;
   1622  1.28  christos 		if (cc > sizeof bf)
   1623  1.28  christos 			cc = sizeof bf;
   1624  1.28  christos 		error = uiomove(bf, cc, uio);
   1625   1.1  augustss 		if (error)
   1626   1.1  augustss 			break;
   1627  1.28  christos 		for(i = 0; i < cc && !MIDI_IS_STATUS(bf[i]); i++)
   1628   1.1  augustss 			;
   1629  1.32      chap 		/*
   1630  1.38  christos 		 * XXX midi(4)'s buffer might not accommodate this, and the
   1631  1.32      chap 		 * function will not block us (though in this case we have
   1632  1.32      chap 		 * a process and could in principle block).
   1633  1.32      chap 		 */
   1634  1.53  jmcneill 		mutex_enter(&sc->lock);
   1635  1.28  christos 		error = midiseq_out(md, bf, i, 0);
   1636  1.53  jmcneill 		mutex_exit(&sc->lock);
   1637   1.1  augustss 		if (error)
   1638   1.1  augustss 			break;
   1639   1.1  augustss 		sysex->len -= i;
   1640   1.1  augustss 		if (i != cc)
   1641   1.1  augustss 			break;
   1642   1.1  augustss 	}
   1643  1.32      chap 	/*
   1644  1.32      chap 	 * Any leftover data in uio is rubbish;
   1645  1.27     perry 	 * the SYSEX should be one write ending in SYSEX_END.
   1646   1.1  augustss 	 */
   1647   1.1  augustss 	uio->uio_resid = 0;
   1648   1.1  augustss 	c = MIDI_SYSEX_END;
   1649  1.53  jmcneill 	mutex_enter(&sc->lock);
   1650  1.53  jmcneill 	error = midiseq_out(md, &c, 1, 0);
   1651  1.53  jmcneill 	mutex_exit(&sc->lock);
   1652  1.53  jmcneill 	return error;
   1653   1.1  augustss }
   1654   1.1  augustss 
   1655   1.9  augustss #if NMIDI == 0
   1656  1.32      chap static dev_type_open(midiopen);
   1657  1.32      chap static dev_type_close(midiclose);
   1658  1.20   gehenna 
   1659   1.9  augustss /*
   1660   1.9  augustss  * If someone has a sequencer, but no midi devices there will
   1661   1.9  augustss  * be unresolved references, so we provide little stubs.
   1662   1.9  augustss  */
   1663   1.9  augustss 
   1664   1.9  augustss int
   1665  1.52    cegger midi_unit_count(void)
   1666   1.9  augustss {
   1667  1.77  riastrad 	return 0;
   1668   1.9  augustss }
   1669   1.9  augustss 
   1670  1.32      chap static int
   1671  1.32      chap midiopen(dev_t dev, int flags, int ifmt, struct lwp *l)
   1672   1.9  augustss {
   1673  1.77  riastrad 	return ENXIO;
   1674   1.9  augustss }
   1675   1.9  augustss 
   1676   1.9  augustss void
   1677  1.32      chap midi_getinfo(dev_t dev, struct midi_info *mi)
   1678   1.9  augustss {
   1679  1.31      tron         mi->name = "Dummy MIDI device";
   1680  1.31      tron 	mi->props = 0;
   1681   1.9  augustss }
   1682   1.9  augustss 
   1683  1.32      chap static int
   1684  1.32      chap midiclose(dev_t dev, int flags, int ifmt, struct lwp *l)
   1685   1.9  augustss {
   1686  1.77  riastrad 	return ENXIO;
   1687   1.9  augustss }
   1688   1.9  augustss 
   1689   1.9  augustss int
   1690  1.32      chap midi_writebytes(int unit, u_char *bf, int cc)
   1691   1.9  augustss {
   1692  1.77  riastrad 	return ENXIO;
   1693   1.9  augustss }
   1694   1.9  augustss #endif /* NMIDI == 0 */
   1695