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