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