Home | History | Annotate | Line # | Download | only in dev
midi.c revision 1.43.2.19
      1  1.43.2.19      chap /*	$NetBSD: midi.c,v 1.43.2.19 2006/05/31 03:17:06 chap Exp $	*/
      2        1.1  augustss 
      3        1.1  augustss /*
      4        1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5        1.1  augustss  * All rights reserved.
      6        1.1  augustss  *
      7        1.8  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.43.2.7      chap  * by Lennart Augustsson (augustss (at) NetBSD.org) and (MIDI FST and Active
      9  1.43.2.17      chap  * Sense handling) Chapman Flack (chap (at) NetBSD.org).
     10        1.1  augustss  *
     11        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12        1.1  augustss  * modification, are permitted provided that the following conditions
     13        1.1  augustss  * are met:
     14        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19        1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20        1.1  augustss  *    must display the following acknowledgement:
     21        1.1  augustss  *        This product includes software developed by the NetBSD
     22        1.1  augustss  *        Foundation, Inc. and its contributors.
     23        1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24        1.1  augustss  *    contributors may be used to endorse or promote products derived
     25        1.1  augustss  *    from this software without specific prior written permission.
     26        1.1  augustss  *
     27        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38        1.1  augustss  */
     39       1.23     lukem 
     40       1.23     lukem #include <sys/cdefs.h>
     41  1.43.2.19      chap __KERNEL_RCSID(0, "$NetBSD: midi.c,v 1.43.2.19 2006/05/31 03:17:06 chap Exp $");
     42        1.1  augustss 
     43        1.1  augustss #include "midi.h"
     44        1.1  augustss #include "sequencer.h"
     45        1.1  augustss 
     46        1.1  augustss #include <sys/param.h>
     47        1.1  augustss #include <sys/ioctl.h>
     48        1.1  augustss #include <sys/fcntl.h>
     49        1.1  augustss #include <sys/vnode.h>
     50        1.1  augustss #include <sys/select.h>
     51        1.1  augustss #include <sys/poll.h>
     52        1.1  augustss #include <sys/malloc.h>
     53        1.1  augustss #include <sys/proc.h>
     54        1.1  augustss #include <sys/systm.h>
     55       1.15   thorpej #include <sys/callout.h>
     56        1.1  augustss #include <sys/syslog.h>
     57        1.1  augustss #include <sys/kernel.h>
     58        1.1  augustss #include <sys/signalvar.h>
     59        1.1  augustss #include <sys/conf.h>
     60        1.1  augustss #include <sys/audioio.h>
     61        1.1  augustss #include <sys/midiio.h>
     62        1.1  augustss 
     63        1.1  augustss #include <dev/audio_if.h>
     64        1.4  augustss #include <dev/midi_if.h>
     65        1.1  augustss #include <dev/midivar.h>
     66        1.1  augustss 
     67       1.10  drochner #if NMIDI > 0
     68       1.10  drochner 
     69        1.1  augustss #ifdef AUDIO_DEBUG
     70        1.1  augustss #define DPRINTF(x)	if (mididebug) printf x
     71        1.1  augustss #define DPRINTFN(n,x)	if (mididebug >= (n)) printf x
     72        1.1  augustss int	mididebug = 0;
     73   1.43.2.2      chap /*
     74   1.43.2.2      chap  *      1: detected protocol errors and buffer overflows
     75   1.43.2.2      chap  *      2: probe, attach, detach
     76   1.43.2.2      chap  *      3: open, close
     77   1.43.2.2      chap  *      4: data received except realtime
     78   1.43.2.2      chap  *      5: ioctl
     79   1.43.2.2      chap  *      6: read, write, poll
     80   1.43.2.2      chap  *      7: data transmitted
     81   1.43.2.2      chap  *      8: uiomoves, synchronization
     82   1.43.2.2      chap  *      9: realtime data received
     83   1.43.2.2      chap  */
     84        1.1  augustss #else
     85        1.1  augustss #define DPRINTF(x)
     86        1.1  augustss #define DPRINTFN(n,x)
     87        1.1  augustss #endif
     88        1.1  augustss 
     89   1.43.2.8      chap static	struct simplelock hwif_register_lock = SIMPLELOCK_INITIALIZER;
     90   1.43.2.8      chap static	struct midi_softc *hwif_softc = NULL;
     91        1.1  augustss 
     92       1.22  augustss void	midi_in(void *, int);
     93       1.22  augustss void	midi_out(void *);
     94   1.43.2.7      chap int     midi_poll_out(struct midi_softc *);
     95   1.43.2.7      chap int     midi_intr_out(struct midi_softc *);
     96   1.43.2.8      chap int 	midi_msg_out(struct midi_softc *,
     97   1.43.2.8      chap                  u_char **, u_char **, u_char **, u_char **);
     98   1.43.2.7      chap int	midi_start_output(struct midi_softc *);
     99  1.43.2.16      chap int	midi_sleep_timo(int *, const char *, int, struct simplelock *);
    100  1.43.2.16      chap int	midi_sleep(int *, const char *, struct simplelock *);
    101       1.22  augustss void	midi_wakeup(int *);
    102       1.22  augustss void	midi_initbuf(struct midi_buffer *);
    103  1.43.2.11      chap void	midi_xmt_asense(void *);
    104  1.43.2.11      chap void	midi_rcv_asense(void *);
    105       1.22  augustss 
    106       1.22  augustss int	midiprobe(struct device *, struct cfdata *, void *);
    107       1.22  augustss void	midiattach(struct device *, struct device *, void *);
    108       1.22  augustss int	mididetach(struct device *, int);
    109       1.22  augustss int	midiactivate(struct device *, enum devact);
    110        1.1  augustss 
    111       1.25   gehenna dev_type_open(midiopen);
    112       1.25   gehenna dev_type_close(midiclose);
    113       1.25   gehenna dev_type_read(midiread);
    114       1.25   gehenna dev_type_write(midiwrite);
    115       1.25   gehenna dev_type_ioctl(midiioctl);
    116       1.25   gehenna dev_type_poll(midipoll);
    117       1.30  jdolecek dev_type_kqfilter(midikqfilter);
    118       1.25   gehenna 
    119       1.25   gehenna const struct cdevsw midi_cdevsw = {
    120       1.25   gehenna 	midiopen, midiclose, midiread, midiwrite, midiioctl,
    121       1.30  jdolecek 	nostop, notty, midipoll, nommap, midikqfilter,
    122       1.25   gehenna };
    123       1.25   gehenna 
    124       1.28   thorpej CFATTACH_DECL(midi, sizeof(struct midi_softc),
    125       1.29   thorpej     midiprobe, midiattach, mididetach, midiactivate);
    126        1.1  augustss 
    127  1.43.2.11      chap #define MIDI_XMT_ASENSE_PERIOD mstohz(275)
    128  1.43.2.11      chap #define MIDI_RCV_ASENSE_PERIOD mstohz(300)
    129        1.4  augustss 
    130        1.1  augustss extern struct cfdriver midi_cd;
    131        1.1  augustss 
    132        1.1  augustss int
    133       1.22  augustss midiprobe(struct device *parent, struct cfdata *match, void *aux)
    134        1.1  augustss {
    135        1.1  augustss 	struct audio_attach_args *sa = aux;
    136        1.1  augustss 
    137  1.43.2.16      chap 	DPRINTFN(2,("midiprobe: type=%d sa=%p hw=%p\n",
    138        1.1  augustss 		 sa->type, sa, sa->hwif));
    139       1.16  augustss 	return (sa->type == AUDIODEV_TYPE_MIDI);
    140        1.1  augustss }
    141        1.1  augustss 
    142        1.1  augustss void
    143       1.22  augustss midiattach(struct device *parent, struct device *self, void *aux)
    144        1.1  augustss {
    145        1.1  augustss 	struct midi_softc *sc = (void *)self;
    146        1.1  augustss 	struct audio_attach_args *sa = aux;
    147  1.43.2.16      chap 	const struct midi_hw_if *hwp = sa->hwif;
    148        1.1  augustss 	void *hdlp = sa->hdl;
    149        1.1  augustss 
    150   1.43.2.2      chap 	DPRINTFN(2, ("MIDI attach\n"));
    151        1.1  augustss 
    152        1.1  augustss #ifdef DIAGNOSTIC
    153        1.1  augustss 	if (hwp == 0 ||
    154        1.1  augustss 	    hwp->open == 0 ||
    155        1.1  augustss 	    hwp->close == 0 ||
    156        1.1  augustss 	    hwp->output == 0 ||
    157        1.1  augustss 	    hwp->getinfo == 0) {
    158        1.1  augustss 		printf("midi: missing method\n");
    159        1.1  augustss 		return;
    160        1.1  augustss 	}
    161        1.1  augustss #endif
    162       1.15   thorpej 
    163        1.1  augustss 	sc->hw_if = hwp;
    164        1.1  augustss 	sc->hw_hdl = hdlp;
    165        1.2  augustss 	midi_attach(sc, parent);
    166        1.2  augustss }
    167        1.2  augustss 
    168       1.18  tshiozak int
    169       1.22  augustss midiactivate(struct device *self, enum devact act)
    170       1.18  tshiozak {
    171       1.18  tshiozak 	struct midi_softc *sc = (struct midi_softc *)self;
    172       1.18  tshiozak 
    173       1.18  tshiozak 	switch (act) {
    174       1.18  tshiozak 	case DVACT_ACTIVATE:
    175       1.18  tshiozak 		return (EOPNOTSUPP);
    176       1.18  tshiozak 
    177       1.18  tshiozak 	case DVACT_DEACTIVATE:
    178       1.18  tshiozak 		sc->dying = 1;
    179       1.18  tshiozak 		break;
    180       1.18  tshiozak 	}
    181       1.18  tshiozak 	return (0);
    182       1.18  tshiozak }
    183       1.18  tshiozak 
    184       1.18  tshiozak int
    185       1.22  augustss mididetach(struct device *self, int flags)
    186       1.18  tshiozak {
    187       1.18  tshiozak 	struct midi_softc *sc = (struct midi_softc *)self;
    188       1.18  tshiozak 	int maj, mn;
    189       1.18  tshiozak 
    190   1.43.2.2      chap 	DPRINTFN(2,("midi_detach: sc=%p flags=%d\n", sc, flags));
    191       1.18  tshiozak 
    192       1.18  tshiozak 	sc->dying = 1;
    193       1.18  tshiozak 
    194       1.18  tshiozak 	wakeup(&sc->wchan);
    195       1.18  tshiozak 	wakeup(&sc->rchan);
    196       1.18  tshiozak 
    197       1.18  tshiozak 	/* locate the major number */
    198       1.25   gehenna 	maj = cdevsw_lookup_major(&midi_cdevsw);
    199       1.18  tshiozak 
    200       1.18  tshiozak 	/* Nuke the vnodes for any open instances (calls close). */
    201  1.43.2.16      chap 	mn = device_unit(self);
    202       1.18  tshiozak 	vdevgone(maj, mn, mn, VCHR);
    203   1.43.2.3      chap 
    204   1.43.2.6      chap 	evcnt_detach(&sc->xmt.bytesDiscarded);
    205   1.43.2.6      chap 	evcnt_detach(&sc->xmt.incompleteMessages);
    206   1.43.2.3      chap 	if ( sc->props & MIDI_PROP_CAN_INPUT ) {
    207   1.43.2.6      chap 		evcnt_detach(&sc->rcv.bytesDiscarded);
    208   1.43.2.6      chap 		evcnt_detach(&sc->rcv.incompleteMessages);
    209   1.43.2.3      chap 	}
    210       1.18  tshiozak 
    211       1.18  tshiozak 	return (0);
    212       1.18  tshiozak }
    213       1.18  tshiozak 
    214        1.2  augustss void
    215       1.22  augustss midi_attach(struct midi_softc *sc, struct device *parent)
    216        1.2  augustss {
    217        1.2  augustss 	struct midi_info mi;
    218   1.43.2.8      chap 	int s;
    219   1.43.2.5      chap 
    220  1.43.2.11      chap 	callout_init(&sc->xmt_asense_co);
    221  1.43.2.11      chap 	callout_init(&sc->rcv_asense_co);
    222  1.43.2.11      chap 	callout_setfunc(&sc->xmt_asense_co, midi_xmt_asense, sc);
    223  1.43.2.11      chap 	callout_setfunc(&sc->rcv_asense_co, midi_rcv_asense, sc);
    224   1.43.2.5      chap 	simple_lock_init(&sc->out_lock);
    225   1.43.2.7      chap 	simple_lock_init(&sc->in_lock);
    226   1.43.2.5      chap 	sc->dying = 0;
    227        1.1  augustss 	sc->isopen = 0;
    228        1.1  augustss 
    229        1.2  augustss 	sc->sc_dev = parent;
    230   1.43.2.8      chap 
    231   1.43.2.8      chap 	s = splaudio();
    232   1.43.2.8      chap 	simple_lock(&hwif_register_lock);
    233   1.43.2.8      chap 	hwif_softc = sc;
    234        1.2  augustss 	sc->hw_if->getinfo(sc->hw_hdl, &mi);
    235   1.43.2.8      chap 	hwif_softc = NULL;
    236   1.43.2.8      chap 	simple_unlock(&hwif_register_lock);
    237   1.43.2.8      chap 	splx(s);
    238   1.43.2.8      chap 
    239        1.1  augustss 	sc->props = mi.props;
    240   1.43.2.3      chap 
    241   1.43.2.6      chap 	evcnt_attach_dynamic(&sc->xmt.bytesDiscarded,
    242   1.43.2.6      chap 		EVCNT_TYPE_MISC, NULL,
    243   1.43.2.6      chap 		sc->dev.dv_xname, "xmt bytes discarded");
    244   1.43.2.6      chap 	evcnt_attach_dynamic(&sc->xmt.incompleteMessages,
    245   1.43.2.6      chap 		EVCNT_TYPE_MISC, NULL,
    246   1.43.2.6      chap 		sc->dev.dv_xname, "xmt incomplete msgs");
    247   1.43.2.3      chap 	if ( sc->props & MIDI_PROP_CAN_INPUT ) {
    248   1.43.2.6      chap 		evcnt_attach_dynamic(&sc->rcv.bytesDiscarded,
    249   1.43.2.3      chap 			EVCNT_TYPE_MISC, NULL,
    250   1.43.2.3      chap 			sc->dev.dv_xname, "rcv bytes discarded");
    251   1.43.2.6      chap 		evcnt_attach_dynamic(&sc->rcv.incompleteMessages,
    252   1.43.2.3      chap 			EVCNT_TYPE_MISC, NULL,
    253   1.43.2.3      chap 			sc->dev.dv_xname, "rcv incomplete msgs");
    254   1.43.2.3      chap 	}
    255   1.43.2.3      chap 
    256  1.43.2.19      chap 	printf(": %s%s\n", mi.name,
    257  1.43.2.19      chap 	    (sc->props & (MIDI_PROP_OUT_INTR|MIDI_PROP_NO_OUTPUT)) ?
    258  1.43.2.19      chap 	    "" : " (cpu-intensive output)");
    259        1.1  augustss }
    260        1.1  augustss 
    261   1.43.2.8      chap void midi_register_hw_if_ext(struct midi_hw_if_ext *exthw) {
    262  1.43.2.18      chap 	if ( hwif_softc != NULL ) /* ignore calls resulting from non-init */
    263  1.43.2.18      chap 		hwif_softc->hw_if_ext = exthw; /* uses of getinfo */
    264   1.43.2.8      chap }
    265   1.43.2.8      chap 
    266        1.1  augustss int
    267       1.22  augustss midi_unit_count(void)
    268        1.1  augustss {
    269   1.43.2.3      chap 	int i;
    270   1.43.2.3      chap 	for ( i = 0; i < midi_cd.cd_ndevs; ++i )
    271   1.43.2.3      chap 	        if ( NULL == midi_cd.cd_devs[i] )
    272   1.43.2.3      chap 		        break;
    273   1.43.2.3      chap         return i;
    274        1.1  augustss }
    275        1.1  augustss 
    276        1.1  augustss void
    277       1.22  augustss midi_initbuf(struct midi_buffer *mb)
    278        1.1  augustss {
    279   1.43.2.7      chap 	mb->idx_producerp = mb->idx_consumerp = mb->idx;
    280   1.43.2.7      chap 	mb->buf_producerp = mb->buf_consumerp = mb->buf;
    281        1.1  augustss }
    282   1.43.2.7      chap #define PACK_MB_IDX(cat,len) (((cat)<<4)|(len))
    283   1.43.2.7      chap #define MB_IDX_CAT(idx) ((idx)>>4)
    284   1.43.2.7      chap #define MB_IDX_LEN(idx) ((idx)&0xf)
    285        1.1  augustss 
    286        1.2  augustss int
    287  1.43.2.16      chap midi_sleep_timo(int *chan, const char *label, int timo, struct simplelock *lk)
    288        1.1  augustss {
    289        1.1  augustss 	int st;
    290        1.1  augustss 
    291        1.1  augustss 	if (!label)
    292        1.1  augustss 		label = "midi";
    293        1.1  augustss 
    294   1.43.2.2      chap 	DPRINTFN(8, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
    295        1.1  augustss 	*chan = 1;
    296   1.43.2.7      chap 	st = ltsleep(chan, PWAIT | PCATCH, label, timo, lk);
    297        1.1  augustss 	*chan = 0;
    298        1.1  augustss #ifdef MIDI_DEBUG
    299        1.1  augustss 	if (st != 0)
    300        1.1  augustss 		printf("midi_sleep: %d\n", st);
    301        1.1  augustss #endif
    302        1.1  augustss 	return st;
    303        1.1  augustss }
    304        1.1  augustss 
    305        1.2  augustss int
    306  1.43.2.16      chap midi_sleep(int *chan, const char *label, struct simplelock *lk)
    307        1.1  augustss {
    308   1.43.2.7      chap 	return midi_sleep_timo(chan, label, 0, lk);
    309        1.1  augustss }
    310        1.1  augustss 
    311        1.2  augustss void
    312       1.22  augustss midi_wakeup(int *chan)
    313        1.1  augustss {
    314        1.1  augustss 	if (*chan) {
    315   1.43.2.2      chap 		DPRINTFN(8, ("midi_wakeup: %p\n", chan));
    316        1.1  augustss 		wakeup(chan);
    317        1.1  augustss 		*chan = 0;
    318        1.1  augustss 	}
    319        1.1  augustss }
    320        1.1  augustss 
    321   1.43.2.2      chap /* in midivar.h:
    322   1.43.2.2      chap #define MIDI_CAT_DATA 0
    323   1.43.2.2      chap #define MIDI_CAT_STATUS1 1
    324   1.43.2.2      chap #define MIDI_CAT_STATUS2 2
    325   1.43.2.2      chap #define MIDI_CAT_COMMON 3
    326   1.43.2.2      chap */
    327   1.43.2.2      chap static char const midi_cats[] = "\0\0\0\0\0\0\0\0\2\2\2\2\1\1\2\3";
    328   1.43.2.2      chap #define MIDI_CAT(d) (midi_cats[((d)>>4)&15])
    329   1.43.2.7      chap #define FST_RETURN(offp,endp,ret) \
    330   1.43.2.6      chap 	return (s->pos=s->msg+(offp)), (s->end=s->msg+(endp)), (ret)
    331        1.1  augustss 
    332  1.43.2.18      chap enum fst_ret { FST_CHN, FST_CHV, FST_COM, FST_SYX, FST_RT, FST_MORE, FST_ERR,
    333  1.43.2.18      chap                FST_HUH, FST_SXP };
    334  1.43.2.18      chap enum fst_form { FST_CANON, FST_COMPR, FST_VCOMP };
    335  1.43.2.18      chap static struct {
    336  1.43.2.18      chap 	int off;
    337  1.43.2.18      chap 	enum fst_ret tag;
    338  1.43.2.18      chap } const midi_forms[] = {
    339  1.43.2.18      chap 	[FST_CANON] = { .off=0, .tag=FST_CHN },
    340  1.43.2.18      chap 	[FST_COMPR] = { .off=1, .tag=FST_CHN },
    341  1.43.2.18      chap 	[FST_VCOMP] = { .off=0, .tag=FST_CHV }
    342  1.43.2.18      chap };
    343  1.43.2.18      chap #define FST_CRETURN(endp) \
    344  1.43.2.18      chap 	FST_RETURN(midi_forms[form].off,endp,midi_forms[form].tag)
    345        1.1  augustss 
    346   1.43.2.6      chap /*
    347   1.43.2.7      chap  * A MIDI finite state transducer suitable for receiving or transmitting. It
    348   1.43.2.7      chap  * will accept correct MIDI input that uses, doesn't use, or sometimes uses the
    349   1.43.2.6      chap  * 'running status' compression technique, and transduce it to fully expanded
    350  1.43.2.18      chap  * (form=FST_CANON) or fully compressed (form=FST_COMPR or FST_VCOMP) form.
    351   1.43.2.6      chap  *
    352   1.43.2.7      chap  * Returns FST_MORE if a complete message has not been parsed yet (SysEx
    353   1.43.2.7      chap  * messages are the exception), FST_ERR or FST_HUH if the input does not
    354   1.43.2.7      chap  * conform to the protocol, or FST_CHN (channel messages), FST_COM (System
    355   1.43.2.7      chap  * Common messages), FST_RT (System Real-Time messages), or FST_SYX (System
    356   1.43.2.6      chap  * Exclusive) to broadly categorize the message parsed. s->pos and s->end
    357   1.43.2.6      chap  * locate the parsed message; while (s->pos<s->end) putchar(*(s->pos++));
    358   1.43.2.6      chap  * would output it.
    359   1.43.2.6      chap  *
    360   1.43.2.7      chap  * FST_HUH means the character c wasn't valid in the original state, but the
    361   1.43.2.6      chap  * state has now been reset to START and the caller should try again passing
    362   1.43.2.7      chap  * the same c. FST_ERR means c isn't valid in the start state; the caller
    363   1.43.2.6      chap  * should kiss it goodbye and continue to try successive characters from the
    364   1.43.2.7      chap  * input until something other than FST_ERR or FST_HUH is returned, at which
    365   1.43.2.6      chap  * point things are resynchronized.
    366   1.43.2.6      chap  *
    367   1.43.2.7      chap  * A FST_SYX return means that between pos and end are from 1 to 3
    368   1.43.2.6      chap  * bytes of a system exclusive message. A SysEx message will be delivered in
    369   1.43.2.6      chap  * one or more chunks of that form, where the first begins with 0xf0 and the
    370   1.43.2.6      chap  * last (which is the only one that might have length < 3) ends with 0xf7.
    371   1.43.2.6      chap  *
    372   1.43.2.6      chap  * Messages corrupted by a protocol error are discarded and won't be seen at
    373   1.43.2.6      chap  * all; again SysEx is the exception, as one or more chunks of it may already
    374   1.43.2.6      chap  * have been parsed.
    375   1.43.2.6      chap  *
    376   1.43.2.7      chap  * For FST_CHN messages, s->msg[0] always contains the status byte even if
    377  1.43.2.18      chap  * FST_COMPR form was requested (pos then points to msg[1]). That way, the
    378   1.43.2.6      chap  * caller can always identify the exact message if there is a need to do so.
    379   1.43.2.7      chap  * For all other message types except FST_SYX, the status byte is at *pos
    380   1.43.2.6      chap  * (which may not necessarily be msg[0]!). There is only one SysEx status
    381   1.43.2.7      chap  * byte, so the return value FST_SYX is sufficient to identify it.
    382  1.43.2.15      chap  *
    383  1.43.2.18      chap  * To simplify some use cases, compression can also be requested with
    384  1.43.2.18      chap  * form=FST_VCOMP. In this form a compressible channel message is indicated
    385  1.43.2.18      chap  * by returning a classification of FST_CHV instead of FST_CHN, and pos points
    386  1.43.2.18      chap  * to the status byte rather than being advanced past it. If the caller in this
    387  1.43.2.18      chap  * case saves the bytes from pos to end, it will have saved the entire message,
    388  1.43.2.18      chap  * and can act on the FST_CHV tag to drop the first byte later. In this form,
    389  1.43.2.18      chap  * unlike FST_CANON, hidden note-off (i.e. note-on with velocity 0) may occur.
    390  1.43.2.18      chap  *
    391  1.43.2.15      chap  * Two obscure points in the MIDI protocol complicate things further, both to
    392  1.43.2.15      chap  * do with the EndSysEx code, 0xf7. First, this code is permitted (and
    393  1.43.2.15      chap  * meaningless) outside of a System Exclusive message, anywhere a status byte
    394  1.43.2.15      chap  * could appear. Second, it is allowed to be absent at the end of a System
    395  1.43.2.15      chap  * Exclusive message (!) - any status byte at all (non-realtime) is allowed to
    396  1.43.2.15      chap  * terminate the message. Both require accomodation in the interface to
    397  1.43.2.15      chap  * midi_fst's caller. A stray 0xf7 should be ignored BUT should count as a
    398  1.43.2.15      chap  * message received for purposes of Active Sense timeout; the case is
    399  1.43.2.15      chap  * represented by a return of FST_COM with a length of zero (pos == end). A
    400  1.43.2.15      chap  * status byte other than 0xf7 during a system exclusive message will cause an
    401  1.43.2.15      chap  * FST_SXP (sysex plus) return; the bytes from pos to end are the end of the
    402  1.43.2.15      chap  * system exclusive message, and after handling those the caller should call
    403  1.43.2.18      chap  * midi_fst again with the same input byte.
    404  1.43.2.15      chap  *
    405  1.43.2.15      chap  * midi(4) will never produce either such form of rubbish.
    406   1.43.2.6      chap  */
    407   1.43.2.7      chap static enum fst_ret
    408  1.43.2.18      chap midi_fst(struct midi_state *s, u_char c, enum fst_form form)
    409   1.43.2.6      chap {
    410   1.43.2.6      chap 	int syxpos = 0;
    411       1.14  augustss 
    412   1.43.2.6      chap 	if ( c >= 0xf8 ) { /* All realtime messages bypass state machine */
    413   1.43.2.6      chap 	        if ( c == 0xf9  ||  c == 0xfd ) {
    414   1.43.2.7      chap 			DPRINTF( ("midi_fst: s=%p c=0x%02x undefined\n",
    415   1.43.2.6      chap 				  s, c));
    416   1.43.2.6      chap 			s->bytesDiscarded.ev_count++;
    417   1.43.2.7      chap 			return FST_ERR;
    418   1.43.2.2      chap 		}
    419   1.43.2.7      chap 		DPRINTFN(9, ("midi_fst: s=%p System Real-Time data=0x%02x\n",
    420   1.43.2.6      chap 			     s, c));
    421   1.43.2.6      chap 		s->msg[2] = c;
    422   1.43.2.7      chap 		FST_RETURN(2,3,FST_RT);
    423   1.43.2.2      chap 	}
    424   1.43.2.2      chap 
    425   1.43.2.7      chap 	DPRINTFN(4, ("midi_fst: s=%p data=0x%02x state=%d\n",
    426   1.43.2.6      chap 		     s, c, s->state));
    427   1.43.2.2      chap 
    428   1.43.2.7      chap         switch ( s->state   | MIDI_CAT(c) ) { /* break ==> return FST_MORE */
    429   1.43.2.2      chap 
    430   1.43.2.2      chap 	case MIDI_IN_START  | MIDI_CAT_COMMON:
    431   1.43.2.2      chap 	case MIDI_IN_RUN1_1 | MIDI_CAT_COMMON:
    432   1.43.2.2      chap 	case MIDI_IN_RUN2_2 | MIDI_CAT_COMMON:
    433  1.43.2.10      chap 	case MIDI_IN_RXX2_2 | MIDI_CAT_COMMON:
    434   1.43.2.6      chap 	        s->msg[0] = c;
    435   1.43.2.6      chap 	        switch ( c ) {
    436   1.43.2.6      chap 		case 0xf0: s->state = MIDI_IN_SYX1_3; break;
    437   1.43.2.6      chap 		case 0xf1: s->state = MIDI_IN_COM0_1; break;
    438   1.43.2.6      chap 		case 0xf2: s->state = MIDI_IN_COM0_2; break;
    439   1.43.2.6      chap 		case 0xf3: s->state = MIDI_IN_COM0_1; break;
    440   1.43.2.7      chap 		case 0xf6: s->state = MIDI_IN_START;  FST_RETURN(0,1,FST_COM);
    441  1.43.2.15      chap 		case 0xf7: s->state = MIDI_IN_START;  FST_RETURN(0,0,FST_COM);
    442   1.43.2.2      chap 		default: goto protocol_violation;
    443        1.1  augustss 		}
    444   1.43.2.2      chap 		break;
    445   1.43.2.2      chap 
    446   1.43.2.2      chap 	case MIDI_IN_RUN1_1 | MIDI_CAT_STATUS1:
    447   1.43.2.6      chap 		if ( c == s->msg[0] ) {
    448   1.43.2.6      chap 			s->state = MIDI_IN_RNX0_1;
    449   1.43.2.6      chap 			break;
    450   1.43.2.6      chap 		}
    451   1.43.2.6      chap 		/* FALLTHROUGH */
    452   1.43.2.2      chap 	case MIDI_IN_RUN2_2 | MIDI_CAT_STATUS1:
    453  1.43.2.10      chap 	case MIDI_IN_RXX2_2 | MIDI_CAT_STATUS1:
    454   1.43.2.6      chap 	case MIDI_IN_START  | MIDI_CAT_STATUS1:
    455   1.43.2.6      chap 	        s->state = MIDI_IN_RUN0_1;
    456   1.43.2.6      chap 	        s->msg[0] = c;
    457   1.43.2.6      chap 		break;
    458   1.43.2.2      chap 
    459   1.43.2.2      chap 	case MIDI_IN_RUN2_2 | MIDI_CAT_STATUS2:
    460  1.43.2.10      chap 	case MIDI_IN_RXX2_2 | MIDI_CAT_STATUS2:
    461   1.43.2.6      chap 		if ( c == s->msg[0] ) {
    462   1.43.2.6      chap 			s->state = MIDI_IN_RNX0_2;
    463   1.43.2.6      chap 			break;
    464   1.43.2.6      chap 		}
    465  1.43.2.10      chap 		if ( (c ^ s->msg[0]) == 0x10 && (c & 0xe0) == 0x80 ) {
    466  1.43.2.10      chap 			s->state = MIDI_IN_RXX0_2;
    467  1.43.2.10      chap 			s->msg[0] = c;
    468  1.43.2.10      chap 			break;
    469  1.43.2.10      chap 		}
    470   1.43.2.6      chap 		/* FALLTHROUGH */
    471   1.43.2.6      chap 	case MIDI_IN_RUN1_1 | MIDI_CAT_STATUS2:
    472   1.43.2.6      chap 	case MIDI_IN_START  | MIDI_CAT_STATUS2:
    473   1.43.2.6      chap 	        s->state = MIDI_IN_RUN0_2;
    474   1.43.2.6      chap 	        s->msg[0] = c;
    475   1.43.2.6      chap 		break;
    476   1.43.2.2      chap 
    477   1.43.2.2      chap         case MIDI_IN_COM0_1 | MIDI_CAT_DATA:
    478   1.43.2.6      chap 		s->state = MIDI_IN_START;
    479   1.43.2.6      chap 	        s->msg[1] = c;
    480   1.43.2.7      chap 		FST_RETURN(0,2,FST_COM);
    481   1.43.2.2      chap 
    482   1.43.2.2      chap         case MIDI_IN_COM0_2 | MIDI_CAT_DATA:
    483   1.43.2.6      chap 	        s->state = MIDI_IN_COM1_2;
    484   1.43.2.6      chap 	        s->msg[1] = c;
    485   1.43.2.6      chap 		break;
    486   1.43.2.2      chap 
    487   1.43.2.2      chap         case MIDI_IN_COM1_2 | MIDI_CAT_DATA:
    488   1.43.2.6      chap 		s->state = MIDI_IN_START;
    489   1.43.2.6      chap 	        s->msg[2] = c;
    490   1.43.2.7      chap 		FST_RETURN(0,3,FST_COM);
    491   1.43.2.2      chap 
    492   1.43.2.2      chap         case MIDI_IN_RUN0_1 | MIDI_CAT_DATA:
    493   1.43.2.6      chap 		s->state = MIDI_IN_RUN1_1;
    494   1.43.2.6      chap 	        s->msg[1] = c;
    495   1.43.2.7      chap 		FST_RETURN(0,2,FST_CHN);
    496   1.43.2.6      chap 
    497   1.43.2.2      chap         case MIDI_IN_RUN1_1 | MIDI_CAT_DATA:
    498   1.43.2.6      chap         case MIDI_IN_RNX0_1 | MIDI_CAT_DATA:
    499   1.43.2.6      chap 		s->state = MIDI_IN_RUN1_1;
    500   1.43.2.6      chap 	        s->msg[1] = c;
    501  1.43.2.18      chap 		FST_CRETURN(2);
    502   1.43.2.2      chap 
    503   1.43.2.2      chap         case MIDI_IN_RUN0_2 | MIDI_CAT_DATA:
    504   1.43.2.6      chap 	        s->state = MIDI_IN_RUN1_2;
    505   1.43.2.6      chap 	        s->msg[1] = c;
    506   1.43.2.6      chap 		break;
    507   1.43.2.2      chap 
    508   1.43.2.2      chap         case MIDI_IN_RUN1_2 | MIDI_CAT_DATA:
    509  1.43.2.18      chap 		if ( FST_CANON == form && 0 == c && (s->msg[0]&0xf0) == 0x90 ) {
    510  1.43.2.10      chap 			s->state = MIDI_IN_RXX2_2;
    511  1.43.2.10      chap 			s->msg[0] ^= 0x10;
    512  1.43.2.10      chap 			s->msg[2] = 64;
    513  1.43.2.10      chap 		} else {
    514  1.43.2.10      chap 			s->state = MIDI_IN_RUN2_2;
    515  1.43.2.10      chap 	        	s->msg[2] = c;
    516  1.43.2.10      chap 		}
    517   1.43.2.7      chap 		FST_RETURN(0,3,FST_CHN);
    518   1.43.2.6      chap 
    519   1.43.2.6      chap         case MIDI_IN_RUN2_2 | MIDI_CAT_DATA:
    520   1.43.2.6      chap 	        s->state = MIDI_IN_RNX1_2;
    521   1.43.2.6      chap 	        s->msg[1] = c;
    522   1.43.2.6      chap 		break;
    523   1.43.2.6      chap 
    524  1.43.2.10      chap         case MIDI_IN_RXX2_2 | MIDI_CAT_DATA:
    525  1.43.2.10      chap 	        s->state = MIDI_IN_RXX1_2;
    526  1.43.2.10      chap 		s->msg[0] ^= 0x10;
    527  1.43.2.10      chap 	        s->msg[1] = c;
    528  1.43.2.10      chap 		break;
    529  1.43.2.10      chap 
    530   1.43.2.6      chap         case MIDI_IN_RNX0_2 | MIDI_CAT_DATA:
    531   1.43.2.6      chap 	        s->state = MIDI_IN_RNY1_2;
    532   1.43.2.6      chap 	        s->msg[1] = c;
    533   1.43.2.6      chap 		break;
    534   1.43.2.6      chap 
    535  1.43.2.10      chap         case MIDI_IN_RXX0_2 | MIDI_CAT_DATA:
    536  1.43.2.10      chap 	        s->state = MIDI_IN_RXY1_2;
    537  1.43.2.10      chap 	        s->msg[1] = c;
    538  1.43.2.10      chap 		break;
    539  1.43.2.10      chap 
    540   1.43.2.6      chap         case MIDI_IN_RNX1_2 | MIDI_CAT_DATA:
    541   1.43.2.6      chap         case MIDI_IN_RNY1_2 | MIDI_CAT_DATA:
    542  1.43.2.18      chap 		if ( FST_CANON == form && 0 == c && (s->msg[0]&0xf0) == 0x90 ) {
    543  1.43.2.10      chap 			s->state = MIDI_IN_RXX2_2;
    544  1.43.2.10      chap 			s->msg[0] ^= 0x10;
    545  1.43.2.10      chap 			s->msg[2] = 64;
    546  1.43.2.10      chap 			FST_RETURN(0,3,FST_CHN);
    547  1.43.2.10      chap 		}
    548   1.43.2.6      chap 		s->state = MIDI_IN_RUN2_2;
    549   1.43.2.6      chap 	        s->msg[2] = c;
    550  1.43.2.18      chap 		FST_CRETURN(3);
    551   1.43.2.6      chap 
    552  1.43.2.10      chap         case MIDI_IN_RXX1_2 | MIDI_CAT_DATA:
    553  1.43.2.10      chap         case MIDI_IN_RXY1_2 | MIDI_CAT_DATA:
    554  1.43.2.18      chap 		if ( ( 0 == c && (s->msg[0]&0xf0) == 0x90)
    555  1.43.2.18      chap 		  || (64 == c && (s->msg[0]&0xf0) == 0x80
    556  1.43.2.18      chap 		      && FST_CANON != form) ) {
    557  1.43.2.10      chap 			s->state = MIDI_IN_RXX2_2;
    558  1.43.2.10      chap 			s->msg[0] ^= 0x10;
    559  1.43.2.10      chap 			s->msg[2] = 64 - c;
    560  1.43.2.18      chap 			FST_CRETURN(3);
    561  1.43.2.10      chap 		}
    562  1.43.2.10      chap 		s->state = MIDI_IN_RUN2_2;
    563  1.43.2.10      chap 	        s->msg[2] = c;
    564  1.43.2.10      chap 		FST_RETURN(0,3,FST_CHN);
    565  1.43.2.10      chap 
    566   1.43.2.6      chap         case MIDI_IN_SYX1_3 | MIDI_CAT_DATA:
    567   1.43.2.6      chap 		s->state = MIDI_IN_SYX2_3;
    568   1.43.2.6      chap 	        s->msg[1] = c;
    569   1.43.2.2      chap 		break;
    570   1.43.2.2      chap 
    571   1.43.2.6      chap         case MIDI_IN_SYX2_3 | MIDI_CAT_DATA:
    572   1.43.2.6      chap 		s->state = MIDI_IN_SYX0_3;
    573   1.43.2.6      chap 	        s->msg[2] = c;
    574   1.43.2.7      chap 		FST_RETURN(0,3,FST_SYX);
    575   1.43.2.6      chap 
    576   1.43.2.6      chap         case MIDI_IN_SYX0_3 | MIDI_CAT_DATA:
    577   1.43.2.6      chap 		s->state = MIDI_IN_SYX1_3;
    578   1.43.2.6      chap 	        s->msg[0] = c;
    579   1.43.2.6      chap 		break;
    580   1.43.2.6      chap 
    581   1.43.2.6      chap         case MIDI_IN_SYX2_3 | MIDI_CAT_COMMON:
    582  1.43.2.15      chap         case MIDI_IN_SYX2_3 | MIDI_CAT_STATUS1:
    583  1.43.2.15      chap         case MIDI_IN_SYX2_3 | MIDI_CAT_STATUS2:
    584   1.43.2.6      chap 		++ syxpos;
    585   1.43.2.6      chap 		/* FALLTHROUGH */
    586   1.43.2.6      chap         case MIDI_IN_SYX1_3 | MIDI_CAT_COMMON:
    587  1.43.2.15      chap         case MIDI_IN_SYX1_3 | MIDI_CAT_STATUS1:
    588  1.43.2.15      chap         case MIDI_IN_SYX1_3 | MIDI_CAT_STATUS2:
    589   1.43.2.6      chap 		++ syxpos;
    590   1.43.2.6      chap 		/* FALLTHROUGH */
    591   1.43.2.6      chap         case MIDI_IN_SYX0_3 | MIDI_CAT_COMMON:
    592  1.43.2.15      chap         case MIDI_IN_SYX0_3 | MIDI_CAT_STATUS1:
    593  1.43.2.15      chap         case MIDI_IN_SYX0_3 | MIDI_CAT_STATUS2:
    594  1.43.2.15      chap 		s->state = MIDI_IN_START;
    595   1.43.2.6      chap 	        if ( c == 0xf7 ) {
    596   1.43.2.6      chap 			s->msg[syxpos] = c;
    597   1.43.2.7      chap 		        FST_RETURN(0,1+syxpos,FST_SYX);
    598   1.43.2.2      chap 		}
    599  1.43.2.15      chap 		s->msg[syxpos] = 0xf7;
    600  1.43.2.15      chap 		FST_RETURN(0,1+syxpos,FST_SXP);
    601   1.43.2.2      chap 
    602   1.43.2.2      chap         default:
    603   1.43.2.2      chap protocol_violation:
    604   1.43.2.7      chap                 DPRINTF(("midi_fst: unexpected %#02x in state %u\n",
    605   1.43.2.6      chap 		        c, s->state));
    606   1.43.2.6      chap 		switch ( s->state ) {
    607   1.43.2.3      chap 		case MIDI_IN_RUN1_1: /* can only get here by seeing an */
    608   1.43.2.3      chap 		case MIDI_IN_RUN2_2: /* INVALID System Common message */
    609  1.43.2.10      chap 		case MIDI_IN_RXX2_2:
    610   1.43.2.6      chap 		        s->state = MIDI_IN_START;
    611   1.43.2.3      chap 			/* FALLTHROUGH */
    612   1.43.2.2      chap 		case MIDI_IN_START:
    613   1.43.2.6      chap 			s->bytesDiscarded.ev_count++;
    614   1.43.2.7      chap 			return FST_ERR;
    615   1.43.2.2      chap 		case MIDI_IN_COM1_2:
    616   1.43.2.2      chap 		case MIDI_IN_RUN1_2:
    617   1.43.2.6      chap 		case MIDI_IN_RNY1_2:
    618  1.43.2.10      chap 		case MIDI_IN_RXY1_2:
    619   1.43.2.6      chap 			s->bytesDiscarded.ev_count++;
    620   1.43.2.2      chap 			/* FALLTHROUGH */
    621   1.43.2.2      chap 		case MIDI_IN_COM0_1:
    622   1.43.2.2      chap 		case MIDI_IN_RUN0_1:
    623   1.43.2.6      chap 		case MIDI_IN_RNX0_1:
    624   1.43.2.2      chap 		case MIDI_IN_COM0_2:
    625   1.43.2.2      chap 		case MIDI_IN_RUN0_2:
    626   1.43.2.6      chap 		case MIDI_IN_RNX0_2:
    627  1.43.2.10      chap 		case MIDI_IN_RXX0_2:
    628   1.43.2.6      chap 		case MIDI_IN_RNX1_2:
    629  1.43.2.10      chap 		case MIDI_IN_RXX1_2:
    630   1.43.2.6      chap 			s->bytesDiscarded.ev_count++;
    631   1.43.2.6      chap 		        s->incompleteMessages.ev_count++;
    632   1.43.2.2      chap 			break;
    633   1.43.2.2      chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
    634   1.43.2.2      chap 		default:
    635   1.43.2.7      chap 		        printf("midi_fst: mishandled %#02x(%u) in state %u?!\n",
    636   1.43.2.6      chap 			      c, MIDI_CAT(c), s->state);
    637   1.43.2.2      chap #endif
    638   1.43.2.2      chap 		}
    639   1.43.2.6      chap 		s->state = MIDI_IN_START;
    640   1.43.2.7      chap 		return FST_HUH;
    641        1.1  augustss 	}
    642   1.43.2.7      chap 	return FST_MORE;
    643   1.43.2.6      chap }
    644   1.43.2.2      chap 
    645   1.43.2.6      chap void
    646   1.43.2.6      chap midi_in(void *addr, int data)
    647   1.43.2.6      chap {
    648   1.43.2.6      chap 	struct midi_softc *sc = addr;
    649   1.43.2.6      chap 	struct midi_buffer *mb = &sc->inbuf;
    650   1.43.2.6      chap 	int i;
    651   1.43.2.6      chap 	int count;
    652   1.43.2.7      chap 	enum fst_ret got;
    653   1.43.2.7      chap 	int s; /* hw may have various spls so impose our own */
    654   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
    655   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
    656   1.43.2.6      chap 
    657   1.43.2.6      chap 	if (!sc->isopen)
    658        1.1  augustss 		return;
    659   1.43.2.6      chap 
    660   1.43.2.6      chap 	if (!(sc->flags & FREAD))
    661   1.43.2.6      chap 		return;		/* discard data if not reading */
    662   1.43.2.6      chap 
    663  1.43.2.15      chap sxp_again:
    664   1.43.2.6      chap 	do
    665  1.43.2.18      chap 		got = midi_fst(&sc->rcv, data, FST_CANON);
    666   1.43.2.7      chap 	while ( got == FST_HUH );
    667   1.43.2.6      chap 
    668   1.43.2.6      chap 	switch ( got ) {
    669   1.43.2.7      chap 	case FST_MORE:
    670   1.43.2.7      chap 	case FST_ERR:
    671        1.1  augustss 		return;
    672   1.43.2.7      chap 	case FST_CHN:
    673   1.43.2.7      chap 	case FST_COM:
    674   1.43.2.7      chap 	case FST_RT:
    675   1.43.2.6      chap #if NSEQUENCER > 0
    676   1.43.2.6      chap 		if (sc->seqopen) {
    677   1.43.2.6      chap 			extern void midiseq_in(struct midi_dev *,u_char *,int);
    678   1.43.2.6      chap 			count = sc->rcv.end - sc->rcv.pos;
    679   1.43.2.6      chap 			midiseq_in(sc->seq_md, sc->rcv.pos, count);
    680   1.43.2.6      chap 			return;
    681   1.43.2.6      chap 		}
    682   1.43.2.6      chap #endif
    683   1.43.2.6      chap         	/*
    684   1.43.2.6      chap 		 * Pass Active Sense to the sequencer if it's open, but not to
    685   1.43.2.6      chap 		 * a raw reader. (Really should do something intelligent with
    686   1.43.2.6      chap 		 * it then, though....)
    687   1.43.2.6      chap 		 */
    688  1.43.2.11      chap 		if ( got == FST_RT && MIDI_ACK == sc->rcv.pos[0] ) {
    689  1.43.2.11      chap 			if ( !sc->rcv_expect_asense ) {
    690  1.43.2.11      chap 				sc->rcv_expect_asense = 1;
    691  1.43.2.11      chap 				callout_schedule(&sc->rcv_asense_co,
    692  1.43.2.11      chap 				                 MIDI_RCV_ASENSE_PERIOD);
    693  1.43.2.11      chap 			}
    694  1.43.2.11      chap 			sc->rcv_quiescent = 0;
    695  1.43.2.11      chap 			sc->rcv_eof = 0;
    696   1.43.2.6      chap 			return;
    697  1.43.2.11      chap 		}
    698   1.43.2.6      chap 		/* FALLTHROUGH */
    699   1.43.2.6      chap 	/*
    700   1.43.2.6      chap 	 * Ultimately SysEx msgs should be offered to the sequencer also; the
    701   1.43.2.6      chap 	 * sequencer API addresses them - but maybe our sequencer can't handle
    702   1.43.2.6      chap 	 * them yet, so offer only to raw reader. (Which means, ultimately,
    703   1.43.2.6      chap 	 * discard them if the sequencer's open, as it's not doing reads!)
    704  1.43.2.15      chap 	 * -> When SysEx support is added to the sequencer, be sure to handle
    705  1.43.2.15      chap 	 *    FST_SXP there too.
    706   1.43.2.6      chap 	 */
    707   1.43.2.7      chap 	case FST_SYX:
    708  1.43.2.15      chap 	case FST_SXP:
    709   1.43.2.6      chap 		count = sc->rcv.end - sc->rcv.pos;
    710   1.43.2.7      chap 		MIDI_IN_LOCK(sc,s);
    711  1.43.2.11      chap 		sc->rcv_quiescent = 0;
    712  1.43.2.11      chap 		sc->rcv_eof = 0;
    713  1.43.2.15      chap 		if ( 0 == count ) {
    714  1.43.2.15      chap 			MIDI_IN_UNLOCK(sc,s);
    715  1.43.2.15      chap 			break;
    716  1.43.2.15      chap 		}
    717   1.43.2.7      chap 		MIDI_BUF_PRODUCER_INIT(mb,idx);
    718   1.43.2.7      chap 		MIDI_BUF_PRODUCER_INIT(mb,buf);
    719   1.43.2.7      chap 		if (count > buf_lim - buf_cur
    720   1.43.2.7      chap 		     || 1 > idx_lim - idx_cur) {
    721   1.43.2.7      chap 			sc->rcv.bytesDiscarded.ev_count += count;
    722   1.43.2.7      chap 			MIDI_IN_UNLOCK(sc,s);
    723   1.43.2.6      chap 			DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
    724   1.43.2.6      chap 				 sc->rcv.pos[0]));
    725   1.43.2.6      chap 			return;
    726   1.43.2.6      chap 		}
    727   1.43.2.6      chap 		for (i = 0; i < count; i++) {
    728   1.43.2.7      chap 			*buf_cur++ = sc->rcv.pos[i];
    729   1.43.2.7      chap 			MIDI_BUF_WRAP(buf);
    730   1.43.2.6      chap 		}
    731   1.43.2.7      chap 		*idx_cur++ = PACK_MB_IDX(got,count);
    732   1.43.2.7      chap 		MIDI_BUF_WRAP(idx);
    733   1.43.2.7      chap 		MIDI_BUF_PRODUCER_WBACK(mb,buf);
    734   1.43.2.7      chap 		MIDI_BUF_PRODUCER_WBACK(mb,idx);
    735   1.43.2.6      chap 		midi_wakeup(&sc->rchan);
    736   1.43.2.6      chap 		if (sc->async)
    737   1.43.2.6      chap 			psignal(sc->async, SIGIO);
    738   1.43.2.7      chap 		MIDI_IN_UNLOCK(sc,s);
    739  1.43.2.11      chap 		selnotify(&sc->rsel, 0); /* filter will spin if locked */
    740   1.43.2.6      chap 		break;
    741   1.43.2.9      chap 	default: /* don't #ifdef this away, gcc will say FST_HUH not handled */
    742   1.43.2.7      chap 		printf("midi_in: midi_fst returned %d?!\n", got);
    743        1.1  augustss 	}
    744  1.43.2.15      chap 	if ( FST_SXP == got )
    745  1.43.2.15      chap 		goto sxp_again;
    746        1.1  augustss }
    747        1.1  augustss 
    748        1.1  augustss void
    749       1.22  augustss midi_out(void *addr)
    750        1.1  augustss {
    751        1.1  augustss 	struct midi_softc *sc = addr;
    752        1.3  augustss 
    753        1.3  augustss 	if (!sc->isopen)
    754        1.3  augustss 		return;
    755   1.43.2.2      chap 	DPRINTFN(8, ("midi_out: %p\n", sc));
    756   1.43.2.7      chap 	midi_intr_out(sc);
    757        1.1  augustss }
    758        1.1  augustss 
    759        1.1  augustss int
    760  1.43.2.16      chap midiopen(dev_t dev, int flags, int ifmt, struct lwp *l)
    761        1.1  augustss {
    762        1.1  augustss 	struct midi_softc *sc;
    763  1.43.2.16      chap 	const struct midi_hw_if *hw;
    764        1.1  augustss 	int error;
    765        1.1  augustss 
    766       1.17   thorpej 	sc = device_lookup(&midi_cd, MIDIUNIT(dev));
    767       1.17   thorpej 	if (sc == NULL)
    768       1.17   thorpej 		return (ENXIO);
    769       1.18  tshiozak 	if (sc->dying)
    770       1.18  tshiozak 		return (EIO);
    771       1.17   thorpej 
    772   1.43.2.2      chap 	DPRINTFN(3,("midiopen %p\n", sc));
    773        1.1  augustss 
    774        1.1  augustss 	hw = sc->hw_if;
    775        1.1  augustss 	if (!hw)
    776        1.1  augustss 		return ENXIO;
    777        1.1  augustss 	if (sc->isopen)
    778        1.1  augustss 		return EBUSY;
    779   1.43.2.7      chap 
    780   1.43.2.7      chap 	/* put both state machines into known states */
    781   1.43.2.6      chap 	sc->rcv.state = MIDI_IN_START;
    782   1.43.2.7      chap 	sc->rcv.pos = sc->rcv.msg;
    783   1.43.2.7      chap 	sc->rcv.end = sc->rcv.msg;
    784   1.43.2.6      chap 	sc->xmt.state = MIDI_IN_START;
    785   1.43.2.6      chap 	sc->xmt.pos = sc->xmt.msg;
    786   1.43.2.6      chap 	sc->xmt.end = sc->xmt.msg;
    787   1.43.2.7      chap 
    788   1.43.2.7      chap 	/* and the buffers */
    789   1.43.2.7      chap 	midi_initbuf(&sc->outbuf);
    790   1.43.2.7      chap 	midi_initbuf(&sc->inbuf);
    791  1.43.2.11      chap 
    792  1.43.2.11      chap 	/* and the receive flags */
    793  1.43.2.11      chap 	sc->rcv_expect_asense = 0;
    794  1.43.2.11      chap 	sc->rcv_quiescent = 0;
    795  1.43.2.11      chap 	sc->rcv_eof = 0;
    796   1.43.2.7      chap 
    797        1.1  augustss 	error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
    798        1.1  augustss 	if (error)
    799        1.1  augustss 		return error;
    800        1.1  augustss 	sc->isopen++;
    801        1.1  augustss 	sc->flags = flags;
    802        1.1  augustss 	sc->rchan = 0;
    803        1.1  augustss 	sc->wchan = 0;
    804        1.1  augustss 	sc->pbus = 0;
    805        1.1  augustss 	sc->async = 0;
    806        1.1  augustss 
    807        1.4  augustss #ifdef MIDI_SAVE
    808        1.4  augustss 	if (midicnt != 0) {
    809        1.4  augustss 		midisave.cnt = midicnt;
    810        1.4  augustss 		midicnt = 0;
    811        1.4  augustss 	}
    812        1.4  augustss #endif
    813        1.4  augustss 
    814        1.1  augustss 	return 0;
    815        1.1  augustss }
    816        1.1  augustss 
    817        1.1  augustss int
    818  1.43.2.16      chap midiclose(dev_t dev, int flags, int ifmt, struct lwp *l)
    819        1.1  augustss {
    820        1.1  augustss 	int unit = MIDIUNIT(dev);
    821        1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    822  1.43.2.16      chap 	const struct midi_hw_if *hw = sc->hw_if;
    823        1.1  augustss 	int s, error;
    824        1.1  augustss 
    825   1.43.2.3      chap 	DPRINTFN(3,("midiclose %p\n", sc));
    826        1.1  augustss 
    827   1.43.2.7      chap 	/* midi_start_output(sc); anything buffered => pbus already set! */
    828        1.1  augustss 	error = 0;
    829   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
    830   1.43.2.7      chap 	while (sc->pbus) {
    831   1.43.2.7      chap 		DPRINTFN(8,("midiclose sleep ...\n"));
    832   1.43.2.7      chap 		error =
    833   1.43.2.7      chap 		midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz, &sc->out_lock);
    834        1.1  augustss 	}
    835        1.3  augustss 	sc->isopen = 0;
    836  1.43.2.11      chap 	MIDI_OUT_UNLOCK(sc,s);
    837  1.43.2.11      chap 	callout_stop(&sc->xmt_asense_co); /* xxx fix this - sleep? */
    838  1.43.2.11      chap 	callout_stop(&sc->rcv_asense_co);
    839        1.1  augustss 	hw->close(sc->hw_hdl);
    840        1.1  augustss #if NSEQUENCER > 0
    841        1.1  augustss 	sc->seqopen = 0;
    842        1.7  augustss 	sc->seq_md = 0;
    843        1.1  augustss #endif
    844        1.1  augustss 	return 0;
    845        1.1  augustss }
    846        1.1  augustss 
    847        1.1  augustss int
    848       1.22  augustss midiread(dev_t dev, struct uio *uio, int ioflag)
    849        1.1  augustss {
    850        1.1  augustss 	int unit = MIDIUNIT(dev);
    851        1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    852        1.1  augustss 	struct midi_buffer *mb = &sc->inbuf;
    853        1.1  augustss 	int error;
    854        1.1  augustss 	int s;
    855   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
    856   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
    857   1.43.2.7      chap 	int appetite;
    858   1.43.2.7      chap 	int first = 1;
    859        1.1  augustss 
    860  1.43.2.16      chap 	DPRINTFN(6,("midiread: %p, count=%lu\n", sc,
    861       1.11   nathanw 		 (unsigned long)uio->uio_resid));
    862        1.1  augustss 
    863       1.18  tshiozak 	if (sc->dying)
    864       1.18  tshiozak 		return EIO;
    865   1.43.2.4      chap         if ( !(sc->props & MIDI_PROP_CAN_INPUT) )
    866   1.43.2.4      chap 	        return ENXIO;
    867       1.18  tshiozak 
    868   1.43.2.7      chap 	MIDI_IN_LOCK(sc,s);
    869   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(mb,idx);
    870   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(mb,buf);
    871   1.43.2.7      chap 	MIDI_IN_UNLOCK(sc,s);
    872   1.43.2.7      chap 
    873        1.1  augustss 	error = 0;
    874   1.43.2.7      chap 	for ( ;; ) {
    875   1.43.2.7      chap 		/*
    876   1.43.2.7      chap 		 * If the used portion of idx wraps around the end, just take
    877   1.43.2.7      chap 		 * the first part on this iteration, and we'll get the rest on
    878   1.43.2.7      chap 		 * the next.
    879   1.43.2.7      chap 		 */
    880   1.43.2.7      chap 		if ( idx_lim > idx_end )
    881   1.43.2.7      chap 			idx_lim = idx_end;
    882   1.43.2.7      chap 		/*
    883   1.43.2.7      chap 		 * Count bytes through the last complete message that will
    884   1.43.2.7      chap 		 * fit in the requested read.
    885   1.43.2.7      chap 		 */
    886   1.43.2.7      chap 		for (appetite = uio->uio_resid; idx_cur < idx_lim; ++idx_cur) {
    887   1.43.2.7      chap 			if ( appetite < MB_IDX_LEN(*idx_cur) )
    888   1.43.2.7      chap 				break;
    889   1.43.2.7      chap 			appetite -= MB_IDX_LEN(*idx_cur);
    890   1.43.2.7      chap 		}
    891   1.43.2.7      chap 		appetite = uio->uio_resid - appetite;
    892   1.43.2.7      chap 		/*
    893   1.43.2.7      chap 		 * Only if the read is too small to hold even the first
    894   1.43.2.7      chap 		 * complete message will we return a partial one (updating idx
    895   1.43.2.7      chap 		 * to reflect the remaining length of the message).
    896   1.43.2.7      chap 		 */
    897   1.43.2.7      chap 		if ( appetite == 0 && idx_cur < idx_lim ) {
    898   1.43.2.7      chap 			if ( !first )
    899   1.43.2.7      chap 				goto unlocked_exit; /* idx_cur not advanced */
    900   1.43.2.7      chap 			appetite = uio->uio_resid;
    901   1.43.2.7      chap 			*idx_cur = PACK_MB_IDX(MB_IDX_CAT(*idx_cur),
    902   1.43.2.7      chap 					       MB_IDX_LEN(*idx_cur) - appetite);
    903   1.43.2.7      chap 		}
    904   1.43.2.7      chap 		KASSERT(buf_cur + appetite <= buf_lim);
    905   1.43.2.7      chap 
    906   1.43.2.7      chap 		/* move the bytes */
    907   1.43.2.7      chap 		if ( appetite > 0 ) {
    908   1.43.2.7      chap 			first = 0;  /* we know we won't return empty-handed */
    909   1.43.2.7      chap 			/* do two uiomoves if data wrap around end of buf */
    910   1.43.2.7      chap 			if ( buf_cur + appetite > buf_end ) {
    911   1.43.2.7      chap 				DPRINTFN(8,
    912   1.43.2.7      chap 					("midiread: uiomove cc=%d (prewrap)\n",
    913   1.43.2.7      chap 					buf_end - buf_cur));
    914   1.43.2.7      chap 				error = uiomove(buf_cur, buf_end-buf_cur, uio);
    915   1.43.2.7      chap 				if ( error )
    916   1.43.2.7      chap 					goto unlocked_exit;
    917   1.43.2.7      chap 				appetite -= buf_end - buf_cur;
    918   1.43.2.7      chap 				buf_cur = mb->buf;
    919        1.1  augustss 			}
    920   1.43.2.7      chap 			DPRINTFN(8, ("midiread: uiomove cc=%d\n", appetite));
    921   1.43.2.7      chap 			error = uiomove(buf_cur, appetite, uio);
    922   1.43.2.7      chap 			if ( error )
    923   1.43.2.7      chap 				goto unlocked_exit;
    924   1.43.2.7      chap 			buf_cur += appetite;
    925   1.43.2.7      chap 		}
    926   1.43.2.7      chap 
    927   1.43.2.7      chap 		MIDI_BUF_WRAP(idx);
    928   1.43.2.7      chap 		MIDI_BUF_WRAP(buf);
    929   1.43.2.7      chap 
    930   1.43.2.7      chap 		MIDI_IN_LOCK(sc,s);
    931   1.43.2.7      chap 		MIDI_BUF_CONSUMER_WBACK(mb,idx);
    932   1.43.2.7      chap 		MIDI_BUF_CONSUMER_WBACK(mb,buf);
    933   1.43.2.7      chap 		if ( 0 == uio->uio_resid ) /* if read satisfied, we're done */
    934   1.43.2.7      chap 			break;
    935   1.43.2.7      chap 		MIDI_BUF_CONSUMER_REFRESH(mb,idx);
    936   1.43.2.7      chap 		if ( idx_cur == idx_lim ) { /* need to wait for data? */
    937  1.43.2.11      chap 			if ( !first || sc->rcv_eof ) /* never block reader if */
    938  1.43.2.11      chap 				break;            /* any data already in hand */
    939   1.43.2.7      chap 			if (ioflag & IO_NDELAY) {
    940   1.43.2.7      chap 				error = EWOULDBLOCK;
    941   1.43.2.7      chap 				break;
    942        1.1  augustss 			}
    943   1.43.2.7      chap 			error = midi_sleep(&sc->rchan, "mid rd", &sc->in_lock);
    944   1.43.2.7      chap 			if ( error )
    945   1.43.2.7      chap 				break;
    946   1.43.2.7      chap 			MIDI_BUF_CONSUMER_REFRESH(mb,idx); /* what'd we get? */
    947        1.1  augustss 		}
    948   1.43.2.7      chap 		MIDI_BUF_CONSUMER_REFRESH(mb,buf);
    949   1.43.2.7      chap 		MIDI_IN_UNLOCK(sc,s);
    950   1.43.2.7      chap 		if ( sc->dying )
    951       1.18  tshiozak 			return EIO;
    952        1.1  augustss 	}
    953   1.43.2.7      chap 	MIDI_IN_UNLOCK(sc,s);
    954   1.43.2.7      chap 
    955   1.43.2.7      chap unlocked_exit:
    956        1.1  augustss 	return error;
    957        1.1  augustss }
    958        1.1  augustss 
    959        1.1  augustss void
    960  1.43.2.11      chap midi_rcv_asense(void *arg)
    961  1.43.2.11      chap {
    962  1.43.2.11      chap 	struct midi_softc *sc = arg;
    963  1.43.2.11      chap 	int s;
    964  1.43.2.11      chap 
    965  1.43.2.11      chap 	if ( sc->dying || !sc->isopen )
    966  1.43.2.11      chap 		return;
    967  1.43.2.11      chap 
    968  1.43.2.11      chap 	if ( sc->rcv_quiescent ) {
    969  1.43.2.11      chap 		MIDI_IN_LOCK(sc,s);
    970  1.43.2.11      chap 		sc->rcv_eof = 1;
    971  1.43.2.11      chap 		sc->rcv_quiescent = 0;
    972  1.43.2.11      chap 		sc->rcv_expect_asense = 0;
    973  1.43.2.11      chap 		midi_wakeup(&sc->rchan);
    974  1.43.2.11      chap 		if (sc->async)
    975  1.43.2.11      chap 			psignal(sc->async, SIGIO);
    976  1.43.2.11      chap 		MIDI_IN_UNLOCK(sc,s);
    977  1.43.2.11      chap 		selnotify(&sc->rsel, 0); /* filter will spin if locked */
    978  1.43.2.11      chap 		return;
    979  1.43.2.11      chap 	}
    980  1.43.2.11      chap 
    981  1.43.2.11      chap 	sc->rcv_quiescent = 1;
    982  1.43.2.11      chap 	callout_schedule(&sc->rcv_asense_co, MIDI_RCV_ASENSE_PERIOD);
    983  1.43.2.11      chap }
    984  1.43.2.11      chap 
    985  1.43.2.11      chap void
    986  1.43.2.11      chap midi_xmt_asense(void *arg)
    987        1.1  augustss {
    988        1.1  augustss 	struct midi_softc *sc = arg;
    989   1.43.2.7      chap 	int s;
    990   1.43.2.7      chap 	int error;
    991   1.43.2.7      chap 	int armed;
    992  1.43.2.11      chap 
    993  1.43.2.11      chap 	if ( sc->dying || !sc->isopen )
    994  1.43.2.11      chap 		return;
    995        1.1  augustss 
    996   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
    997  1.43.2.11      chap 	if ( sc->pbus || sc->dying || !sc->isopen ) {
    998   1.43.2.7      chap 		MIDI_OUT_UNLOCK(sc,s);
    999   1.43.2.7      chap 		return;
   1000   1.43.2.7      chap 	}
   1001   1.43.2.7      chap 	sc->pbus = 1;
   1002  1.43.2.11      chap 	DPRINTFN(8,("midi_xmt_asense: %p\n", sc));
   1003   1.43.2.7      chap 
   1004   1.43.2.7      chap 	if ( sc->props & MIDI_PROP_OUT_INTR ) {
   1005   1.43.2.7      chap 		error = sc->hw_if->output(sc->hw_hdl, MIDI_ACK);
   1006   1.43.2.7      chap 		armed = (error == 0);
   1007   1.43.2.7      chap 	} else { /* polled output, do with interrupts unmasked */
   1008   1.43.2.7      chap 		MIDI_OUT_UNLOCK(sc,s);
   1009  1.43.2.11      chap 		/* running from softclock, so top half won't sneak in here */
   1010   1.43.2.7      chap 		error = sc->hw_if->output(sc->hw_hdl, MIDI_ACK);
   1011   1.43.2.7      chap 		MIDI_OUT_LOCK(sc,s);
   1012   1.43.2.7      chap 		armed = 0;
   1013   1.43.2.7      chap 	}
   1014   1.43.2.7      chap 
   1015   1.43.2.7      chap 	if ( !armed ) {
   1016   1.43.2.7      chap 		sc->pbus = 0;
   1017  1.43.2.11      chap 		callout_schedule(&sc->xmt_asense_co, MIDI_XMT_ASENSE_PERIOD);
   1018   1.43.2.7      chap 	}
   1019   1.43.2.7      chap 
   1020   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1021        1.1  augustss }
   1022        1.1  augustss 
   1023   1.43.2.5      chap /*
   1024   1.43.2.8      chap  * The way this function was hacked up to plug into poll_out and intr_out
   1025   1.43.2.8      chap  * after they were written won't win it any beauty contests, but it'll work
   1026   1.43.2.8      chap  * (code in haste, refactor at leisure). This may be called with the lock
   1027   1.43.2.8      chap  * (by intr_out) or without the lock (by poll_out) so it only does what could
   1028   1.43.2.8      chap  * be safe either way.
   1029   1.43.2.8      chap  */
   1030   1.43.2.8      chap int midi_msg_out(struct midi_softc *sc,
   1031   1.43.2.8      chap                  u_char **idx, u_char **idxl, u_char **buf, u_char **bufl) {
   1032   1.43.2.8      chap 	MIDI_BUF_DECLARE(idx);
   1033   1.43.2.8      chap 	MIDI_BUF_DECLARE(buf);
   1034   1.43.2.8      chap 	MIDI_BUF_EXTENT_INIT(&sc->outbuf,idx);
   1035   1.43.2.8      chap 	MIDI_BUF_EXTENT_INIT(&sc->outbuf,buf);
   1036   1.43.2.8      chap 	int length;
   1037   1.43.2.8      chap 	int error;
   1038   1.43.2.8      chap 	u_char contig[3];
   1039  1.43.2.18      chap 	u_char *cp;
   1040  1.43.2.18      chap 	u_char *ep;
   1041   1.43.2.8      chap 
   1042   1.43.2.8      chap 	idx_cur = *idx;
   1043   1.43.2.8      chap 	idx_lim = *idxl;
   1044   1.43.2.8      chap 	buf_cur = *buf;
   1045   1.43.2.8      chap 	buf_lim = *bufl;
   1046   1.43.2.8      chap 
   1047   1.43.2.8      chap 	length = MB_IDX_LEN(*idx_cur);
   1048   1.43.2.8      chap 
   1049  1.43.2.18      chap 	for ( cp = contig, ep = cp + length; cp < ep; ) {
   1050   1.43.2.8      chap 		*cp++ = *buf_cur++;
   1051   1.43.2.8      chap 		MIDI_BUF_WRAP(buf);
   1052   1.43.2.8      chap 	}
   1053  1.43.2.18      chap 	cp = contig;
   1054  1.43.2.18      chap 
   1055   1.43.2.8      chap 	switch ( MB_IDX_CAT(*idx_cur) ) {
   1056  1.43.2.18      chap 	case FST_CHV: /* chnmsg to be compressed (for device that wants it) */
   1057  1.43.2.18      chap 		++ cp;
   1058  1.43.2.18      chap 		-- length;
   1059  1.43.2.18      chap 		/* FALLTHROUGH */
   1060   1.43.2.8      chap 	case FST_CHN:
   1061   1.43.2.8      chap 		error = sc->hw_if_ext->channel(sc->hw_hdl,
   1062   1.43.2.8      chap 		                               MIDI_GET_STATUS(contig[0]),
   1063   1.43.2.8      chap 					       MIDI_GET_CHAN(contig[0]),
   1064  1.43.2.18      chap 					       cp, length);
   1065   1.43.2.8      chap 		break;
   1066   1.43.2.8      chap 	case FST_COM:
   1067   1.43.2.8      chap 		error = sc->hw_if_ext->common(sc->hw_hdl,
   1068   1.43.2.8      chap 		                              MIDI_GET_STATUS(contig[0]),
   1069  1.43.2.18      chap 					      cp, length);
   1070   1.43.2.8      chap 		break;
   1071   1.43.2.8      chap 	case FST_SYX:
   1072  1.43.2.15      chap 	case FST_SXP:
   1073   1.43.2.8      chap 		error = sc->hw_if_ext->sysex(sc->hw_hdl,
   1074  1.43.2.18      chap 					     cp, length);
   1075   1.43.2.8      chap 		break;
   1076   1.43.2.8      chap 	case FST_RT:
   1077  1.43.2.18      chap 		error = sc->hw_if->output(sc->hw_hdl, *cp);
   1078   1.43.2.8      chap 		break;
   1079   1.43.2.8      chap 	default:
   1080   1.43.2.8      chap 		error = EIO;
   1081   1.43.2.8      chap 	}
   1082   1.43.2.8      chap 
   1083   1.43.2.8      chap 	if ( !error ) {
   1084   1.43.2.8      chap 		++ idx_cur;
   1085   1.43.2.8      chap 		MIDI_BUF_WRAP(idx);
   1086   1.43.2.8      chap 		*idx  = idx_cur;
   1087   1.43.2.8      chap 		*idxl = idx_lim;
   1088   1.43.2.8      chap 		*buf  = buf_cur;
   1089   1.43.2.8      chap 		*bufl = buf_lim;
   1090   1.43.2.8      chap 	}
   1091   1.43.2.8      chap 
   1092   1.43.2.8      chap 	return error;
   1093   1.43.2.8      chap }
   1094   1.43.2.8      chap 
   1095   1.43.2.8      chap /*
   1096   1.43.2.7      chap  * midi_poll_out is intended for the midi hw (the vast majority of MIDI UARTs
   1097   1.43.2.7      chap  * on sound cards, apparently) that _do not have transmit-ready interrupts_.
   1098   1.43.2.7      chap  * Every call to hw_if->output for one of these may busy-wait to output the
   1099   1.43.2.7      chap  * byte; at the standard midi data rate that'll be 320us per byte. The
   1100   1.43.2.7      chap  * technique of writing only MIDI_MAX_WRITE bytes in a row and then waiting
   1101   1.43.2.7      chap  * for MIDI_WAIT does not reduce the total time spent busy-waiting, and it
   1102   1.43.2.7      chap  * adds arbitrary delays in transmission (and, since MIDI_WAIT is roughly the
   1103   1.43.2.7      chap  * same as the time to send MIDI_MAX_WRITE bytes, it effectively halves the
   1104   1.43.2.7      chap  * data rate). Here, a somewhat bolder approach is taken. Since midi traffic
   1105   1.43.2.7      chap  * is bursty but time-sensitive--most of the time there will be none at all,
   1106   1.43.2.7      chap  * but when there is it should go out ASAP--the strategy is to just get it
   1107   1.43.2.7      chap  * over with, and empty the buffer in one go. The effect this can have on
   1108   1.43.2.7      chap  * the rest of the system will be limited by the size of the buffer and the
   1109   1.43.2.7      chap  * sparseness of the traffic. But some precautions are in order. Interrupts
   1110   1.43.2.7      chap  * should all be unmasked when this is called, and midiwrite should not fill
   1111   1.43.2.7      chap  * the buffer more than once (when MIDI_PROP_CAN_INTR is false) without a
   1112   1.43.2.7      chap  * yield() so some other process can get scheduled. If the write is nonblocking,
   1113   1.43.2.7      chap  * midiwrite should return a short count rather than yield.
   1114   1.43.2.5      chap  *
   1115   1.43.2.7      chap  * Someday when there is fine-grained MP support, this should be reworked to
   1116   1.43.2.7      chap  * run in a callout so the writing process really could proceed concurrently.
   1117   1.43.2.7      chap  * But obviously where performance is a concern, interrupt-driven hardware
   1118  1.43.2.15      chap  * such as USB midi or (apparently) clcs will always be preferable. And it
   1119  1.43.2.15      chap  * seems (kern/32651) that many of the devices currently working in poll mode
   1120  1.43.2.15      chap  * may really have tx interrupt capability and want only implementation; that
   1121  1.43.2.15      chap  * ought to happen.
   1122   1.43.2.5      chap  */
   1123        1.1  augustss int
   1124   1.43.2.7      chap midi_poll_out(struct midi_softc *sc)
   1125        1.1  augustss {
   1126        1.1  augustss 	struct midi_buffer *mb = &sc->outbuf;
   1127        1.1  augustss 	int error;
   1128   1.43.2.7      chap 	int msglen;
   1129        1.1  augustss 	int s;
   1130   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
   1131   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1132        1.1  augustss 
   1133        1.1  augustss 	error = 0;
   1134       1.18  tshiozak 
   1135   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
   1136   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(mb,idx);
   1137   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(mb,buf);
   1138   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1139   1.43.2.7      chap 
   1140   1.43.2.7      chap 	for ( ;; ) {
   1141   1.43.2.7      chap 		while ( idx_cur != idx_lim ) {
   1142   1.43.2.8      chap 			if ( sc->hw_if_ext ) {
   1143   1.43.2.8      chap 				error = midi_msg_out(sc, &idx_cur, &idx_lim,
   1144   1.43.2.8      chap 				                         &buf_cur, &buf_lim);
   1145   1.43.2.8      chap 				if ( error )
   1146   1.43.2.8      chap 					goto ioerror;
   1147   1.43.2.8      chap 				continue;
   1148   1.43.2.8      chap 			}
   1149   1.43.2.8      chap 			/* or, lacking hw_if_ext ... */
   1150   1.43.2.7      chap 			msglen = MB_IDX_LEN(*idx_cur);
   1151  1.43.2.10      chap 			DPRINTFN(7,("midi_poll_out: %p <- %#02x\n",
   1152  1.43.2.10      chap 				   sc->hw_hdl, *buf_cur));
   1153   1.43.2.7      chap 			error = sc->hw_if->output(sc->hw_hdl, *buf_cur);
   1154   1.43.2.7      chap 			if ( error )
   1155   1.43.2.7      chap 				goto ioerror;
   1156   1.43.2.7      chap 			++ buf_cur;
   1157   1.43.2.7      chap 			MIDI_BUF_WRAP(buf);
   1158   1.43.2.7      chap 			-- msglen;
   1159   1.43.2.7      chap 			if ( msglen )
   1160   1.43.2.7      chap 				*idx_cur = PACK_MB_IDX(MB_IDX_CAT(*idx_cur),
   1161   1.43.2.7      chap 				                       msglen);
   1162   1.43.2.7      chap 			else {
   1163   1.43.2.7      chap 				++ idx_cur;
   1164   1.43.2.7      chap 				MIDI_BUF_WRAP(idx);
   1165   1.43.2.5      chap 			}
   1166   1.43.2.5      chap 		}
   1167   1.43.2.7      chap 		KASSERT(buf_cur == buf_lim);
   1168   1.43.2.7      chap 		MIDI_OUT_LOCK(sc,s);
   1169   1.43.2.7      chap 		MIDI_BUF_CONSUMER_WBACK(mb,idx);
   1170   1.43.2.7      chap 		MIDI_BUF_CONSUMER_WBACK(mb,buf);
   1171   1.43.2.7      chap 		MIDI_BUF_CONSUMER_REFRESH(mb,idx); /* any more to transmit? */
   1172   1.43.2.7      chap 		MIDI_BUF_CONSUMER_REFRESH(mb,buf);
   1173   1.43.2.7      chap 		if ( idx_lim == idx_cur )
   1174   1.43.2.7      chap 			break; /* still holding lock */
   1175   1.43.2.7      chap 		MIDI_OUT_UNLOCK(sc,s);
   1176   1.43.2.5      chap 	}
   1177   1.43.2.7      chap 	goto disarm; /* lock held */
   1178   1.43.2.7      chap 
   1179   1.43.2.7      chap ioerror:
   1180   1.43.2.7      chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
   1181   1.43.2.7      chap 	printf("%s: midi_poll_output error %d\n",
   1182   1.43.2.7      chap 	      sc->dev.dv_xname, error);
   1183   1.43.2.7      chap #endif
   1184   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
   1185   1.43.2.7      chap 	MIDI_BUF_CONSUMER_WBACK(mb,idx);
   1186   1.43.2.7      chap 	MIDI_BUF_CONSUMER_WBACK(mb,buf);
   1187   1.43.2.7      chap 
   1188   1.43.2.7      chap disarm:
   1189   1.43.2.7      chap 	sc->pbus = 0;
   1190  1.43.2.11      chap 	callout_schedule(&sc->xmt_asense_co, MIDI_XMT_ASENSE_PERIOD);
   1191   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1192   1.43.2.7      chap 	return error;
   1193   1.43.2.7      chap }
   1194   1.43.2.7      chap 
   1195   1.43.2.7      chap /*
   1196   1.43.2.7      chap  * The interrupt flavor acquires spl and lock once and releases at the end,
   1197   1.43.2.7      chap  * as it expects to write only one byte or message. The interface convention
   1198   1.43.2.7      chap  * is that if hw_if->output returns 0, it has initiated transmission and the
   1199   1.43.2.7      chap  * completion interrupt WILL be forthcoming; if it has not returned 0, NO
   1200   1.43.2.7      chap  * interrupt will be forthcoming, and if it returns EINPROGRESS it wants
   1201   1.43.2.7      chap  * another byte right away.
   1202   1.43.2.7      chap  */
   1203   1.43.2.7      chap int
   1204   1.43.2.7      chap midi_intr_out(struct midi_softc *sc)
   1205   1.43.2.7      chap {
   1206   1.43.2.7      chap 	struct midi_buffer *mb = &sc->outbuf;
   1207   1.43.2.7      chap 	int error;
   1208   1.43.2.7      chap 	int msglen;
   1209   1.43.2.7      chap 	int s;
   1210   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
   1211   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1212   1.43.2.7      chap 	int armed = 0;
   1213   1.43.2.7      chap 
   1214   1.43.2.7      chap 	error = 0;
   1215   1.43.2.7      chap 
   1216   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
   1217   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(mb,idx);
   1218   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(mb,buf);
   1219   1.43.2.5      chap 
   1220   1.43.2.7      chap 	while ( idx_cur != idx_lim ) {
   1221   1.43.2.8      chap 		if ( sc->hw_if_ext ) {
   1222   1.43.2.8      chap 			error = midi_msg_out(sc, &idx_cur, &idx_lim,
   1223   1.43.2.8      chap 				                 &buf_cur, &buf_lim);
   1224   1.43.2.8      chap 			if ( !error ) /* no EINPROGRESS from extended hw_if */
   1225   1.43.2.8      chap 				armed = 1;
   1226   1.43.2.8      chap 			break;
   1227   1.43.2.8      chap 		}
   1228   1.43.2.8      chap 		/* or, lacking hw_if_ext ... */
   1229   1.43.2.7      chap 		msglen = MB_IDX_LEN(*idx_cur);
   1230   1.43.2.7      chap 		error = sc->hw_if->output(sc->hw_hdl, *buf_cur);
   1231   1.43.2.7      chap 		if ( error  &&  error != EINPROGRESS )
   1232   1.43.2.5      chap 			break;
   1233   1.43.2.7      chap 		++ buf_cur;
   1234   1.43.2.7      chap 		MIDI_BUF_WRAP(buf);
   1235   1.43.2.7      chap 		-- msglen;
   1236   1.43.2.7      chap 		if ( msglen )
   1237   1.43.2.7      chap 			*idx_cur = PACK_MB_IDX(MB_IDX_CAT(*idx_cur),msglen);
   1238   1.43.2.7      chap 		else {
   1239   1.43.2.7      chap 			++ idx_cur;
   1240   1.43.2.7      chap 			MIDI_BUF_WRAP(idx);
   1241   1.43.2.5      chap 		}
   1242   1.43.2.7      chap 		if ( !error ) {
   1243   1.43.2.7      chap 			armed = 1;
   1244   1.43.2.7      chap 			break;
   1245   1.43.2.5      chap 		}
   1246        1.1  augustss 	}
   1247   1.43.2.7      chap 	MIDI_BUF_CONSUMER_WBACK(mb,idx);
   1248   1.43.2.7      chap 	MIDI_BUF_CONSUMER_WBACK(mb,buf);
   1249   1.43.2.7      chap 	if ( !armed ) {
   1250   1.43.2.7      chap 		sc->pbus = 0;
   1251  1.43.2.11      chap 		callout_schedule(&sc->xmt_asense_co, MIDI_XMT_ASENSE_PERIOD);
   1252   1.43.2.7      chap 	}
   1253   1.43.2.7      chap 	midi_wakeup(&sc->wchan);
   1254   1.43.2.7      chap 	if ( sc->async )
   1255   1.43.2.7      chap 		psignal(sc->async, SIGIO);
   1256   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1257  1.43.2.11      chap 	selnotify(&sc->wsel, 0); /* filter will spin if locked */
   1258   1.43.2.5      chap 
   1259   1.43.2.5      chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
   1260   1.43.2.7      chap 	if ( error )
   1261   1.43.2.7      chap 		printf("%s: midi_intr_output error %d\n",
   1262   1.43.2.7      chap 	               sc->dev.dv_xname, error);
   1263   1.43.2.5      chap #endif
   1264   1.43.2.7      chap 	return error;
   1265   1.43.2.7      chap }
   1266   1.43.2.5      chap 
   1267   1.43.2.7      chap int
   1268   1.43.2.7      chap midi_start_output(struct midi_softc *sc)
   1269   1.43.2.7      chap {
   1270   1.43.2.7      chap 	if (sc->dying)
   1271   1.43.2.7      chap 		return EIO;
   1272       1.20  tshiozak 
   1273   1.43.2.7      chap 	if ( sc->props & MIDI_PROP_OUT_INTR )
   1274   1.43.2.7      chap 		return midi_intr_out(sc);
   1275   1.43.2.7      chap 	return midi_poll_out(sc);
   1276   1.43.2.7      chap }
   1277   1.43.2.7      chap 
   1278   1.43.2.7      chap static int
   1279  1.43.2.15      chap real_writebytes(struct midi_softc *sc, u_char *ibuf, int cc)
   1280   1.43.2.7      chap {
   1281  1.43.2.15      chap 	u_char *iend = ibuf + cc;
   1282   1.43.2.7      chap 	struct midi_buffer *mb = &sc->outbuf;
   1283   1.43.2.7      chap 	int arming = 0;
   1284   1.43.2.7      chap 	int count;
   1285   1.43.2.7      chap 	int s;
   1286   1.43.2.7      chap 	int got;
   1287  1.43.2.18      chap 	enum fst_form form;
   1288   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
   1289   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1290  1.43.2.18      chap 
   1291  1.43.2.18      chap 	/*
   1292  1.43.2.18      chap 	 * If the hardware uses the extended hw_if, pass it canonicalized
   1293  1.43.2.18      chap 	 * messages (or compressed ones if it specifically requests, using
   1294  1.43.2.18      chap 	 * VCOMP form so the bottom half can still pass the op and chan along);
   1295  1.43.2.18      chap 	 * if it does not, send it compressed messages (using COMPR form as
   1296  1.43.2.18      chap 	 * there is no need to preserve the status for the bottom half).
   1297  1.43.2.18      chap 	 */
   1298  1.43.2.18      chap 	if ( NULL == sc->hw_if_ext )
   1299  1.43.2.18      chap 		form = FST_COMPR;
   1300  1.43.2.18      chap 	else if ( sc->hw_if_ext->compress )
   1301  1.43.2.18      chap 		form = FST_VCOMP;
   1302  1.43.2.18      chap 	else
   1303  1.43.2.18      chap 		form = FST_CANON;
   1304   1.43.2.7      chap 
   1305   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
   1306   1.43.2.7      chap 	MIDI_BUF_PRODUCER_INIT(mb,idx);
   1307   1.43.2.7      chap 	MIDI_BUF_PRODUCER_INIT(mb,buf);
   1308   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1309   1.43.2.7      chap 
   1310   1.43.2.7      chap 	if (sc->dying)
   1311   1.43.2.7      chap 		return EIO;
   1312   1.43.2.7      chap 
   1313  1.43.2.15      chap 	while ( ibuf < iend ) {
   1314  1.43.2.10      chap 		do {
   1315  1.43.2.18      chap 			got = midi_fst(&sc->xmt, *ibuf, form);
   1316  1.43.2.10      chap 		} while ( got == FST_HUH );
   1317  1.43.2.15      chap 		++ ibuf;
   1318   1.43.2.7      chap 		switch ( got ) {
   1319   1.43.2.7      chap 		case FST_MORE:
   1320   1.43.2.7      chap 			continue;
   1321   1.43.2.7      chap 		case FST_ERR:
   1322   1.43.2.7      chap #if defined(EPROTO) /* most appropriate SUSv3 errno, but not in errno.h yet */
   1323   1.43.2.7      chap 			return EPROTO;
   1324   1.43.2.7      chap #else
   1325   1.43.2.7      chap 			return EIO;
   1326   1.43.2.7      chap #endif
   1327   1.43.2.7      chap 		case FST_CHN:
   1328  1.43.2.18      chap 		case FST_CHV: /* only occurs in VCOMP form */
   1329   1.43.2.7      chap 		case FST_COM:
   1330   1.43.2.7      chap 		case FST_RT:
   1331   1.43.2.7      chap 		case FST_SYX:
   1332  1.43.2.15      chap 		case FST_SXP:
   1333   1.43.2.7      chap 			break; /* go add to buffer */
   1334   1.43.2.7      chap #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
   1335   1.43.2.7      chap 		default:
   1336   1.43.2.7      chap 			printf("midi_wr: midi_fst returned %d?!\n", got);
   1337   1.43.2.7      chap #endif
   1338   1.43.2.7      chap 		}
   1339   1.43.2.7      chap 		count = sc->xmt.end - sc->xmt.pos;
   1340  1.43.2.15      chap 		if ( 0 == count ) /* can happen with stray 0xf7; see midi_fst */
   1341  1.43.2.15      chap 			continue;
   1342   1.43.2.7      chap 		/*
   1343   1.43.2.7      chap 		 * return EWOULDBLOCK if the data passed will not fit in
   1344   1.43.2.7      chap 		 * the buffer; the caller should have taken steps to avoid that.
   1345  1.43.2.15      chap 		 * If got==FST_SXP we lose the new status byte, but we're losing
   1346  1.43.2.15      chap 		 * anyway, so c'est la vie.
   1347   1.43.2.7      chap 		 */
   1348   1.43.2.7      chap 		if ( idx_cur == idx_lim || count > buf_lim - buf_cur ) {
   1349   1.43.2.7      chap 			MIDI_OUT_LOCK(sc,s);
   1350   1.43.2.7      chap 			MIDI_BUF_PRODUCER_REFRESH(mb,idx); /* get the most */
   1351   1.43.2.7      chap 			MIDI_BUF_PRODUCER_REFRESH(mb,buf); /*  current facts */
   1352   1.43.2.7      chap 			MIDI_OUT_UNLOCK(sc,s);
   1353   1.43.2.7      chap 			if ( idx_cur == idx_lim || count > buf_lim - buf_cur )
   1354   1.43.2.7      chap 				return EWOULDBLOCK; /* caller's problem */
   1355   1.43.2.7      chap 		}
   1356   1.43.2.7      chap 		*idx_cur++ = PACK_MB_IDX(got,count);
   1357   1.43.2.7      chap 		MIDI_BUF_WRAP(idx);
   1358   1.43.2.7      chap 		while ( count ) {
   1359   1.43.2.7      chap 			*buf_cur++ = *(sc->xmt.pos)++;
   1360   1.43.2.7      chap 			MIDI_BUF_WRAP(buf);
   1361   1.43.2.7      chap 			-- count;
   1362   1.43.2.7      chap 		}
   1363  1.43.2.15      chap 		if ( FST_SXP == got )
   1364  1.43.2.15      chap 			-- ibuf; /* again with same status byte */
   1365   1.43.2.7      chap 	}
   1366   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
   1367   1.43.2.7      chap 	MIDI_BUF_PRODUCER_WBACK(mb,buf);
   1368   1.43.2.7      chap 	MIDI_BUF_PRODUCER_WBACK(mb,idx);
   1369   1.43.2.7      chap 	/*
   1370   1.43.2.7      chap 	 * If the output transfer is not already busy, and there is a message
   1371   1.43.2.7      chap 	 * buffered, mark it busy, stop the Active Sense callout (what if we're
   1372   1.43.2.7      chap 	 * too late and it's expired already? No big deal, an extra Active Sense
   1373   1.43.2.7      chap 	 * never hurt anybody) and start the output transfer once we're out of
   1374   1.43.2.7      chap 	 * the critical section (pbus==1 will stop anyone else doing the same).
   1375   1.43.2.7      chap 	 */
   1376  1.43.2.14      chap 	MIDI_BUF_CONSUMER_INIT(mb,idx); /* check what consumer's got to read */
   1377  1.43.2.14      chap 	if ( !sc->pbus && idx_cur < idx_lim ) {
   1378   1.43.2.7      chap 		sc->pbus = 1;
   1379  1.43.2.11      chap 		callout_stop(&sc->xmt_asense_co);
   1380   1.43.2.7      chap 		arming = 1;
   1381   1.43.2.7      chap 	}
   1382   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1383   1.43.2.7      chap 	return arming ? midi_start_output(sc) : 0;
   1384        1.1  augustss }
   1385        1.1  augustss 
   1386        1.1  augustss int
   1387       1.22  augustss midiwrite(dev_t dev, struct uio *uio, int ioflag)
   1388        1.1  augustss {
   1389        1.1  augustss 	int unit = MIDIUNIT(dev);
   1390        1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
   1391        1.1  augustss 	struct midi_buffer *mb = &sc->outbuf;
   1392        1.1  augustss 	int error;
   1393   1.43.2.7      chap 	u_char inp[256];
   1394        1.1  augustss 	int s;
   1395   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
   1396   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1397   1.43.2.7      chap 	size_t idxspace;
   1398   1.43.2.7      chap 	size_t bufspace;
   1399   1.43.2.7      chap 	size_t xfrcount;
   1400   1.43.2.7      chap 	int pollout = 0;
   1401        1.1  augustss 
   1402  1.43.2.16      chap 	DPRINTFN(6, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
   1403       1.11   nathanw 		     (unsigned long)uio->uio_resid));
   1404        1.1  augustss 
   1405       1.18  tshiozak 	if (sc->dying)
   1406       1.18  tshiozak 		return EIO;
   1407       1.18  tshiozak 
   1408        1.1  augustss 	error = 0;
   1409        1.1  augustss 	while (uio->uio_resid > 0 && !error) {
   1410   1.43.2.7      chap 
   1411   1.43.2.7      chap 		/*
   1412   1.43.2.7      chap 		 * block if necessary for the minimum buffer space to guarantee
   1413   1.43.2.7      chap 		 * we can write something.
   1414   1.43.2.7      chap 		 */
   1415   1.43.2.7      chap 		MIDI_OUT_LOCK(sc,s);
   1416   1.43.2.7      chap 		MIDI_BUF_PRODUCER_INIT(mb,idx); /* init can't go above loop; */
   1417   1.43.2.7      chap 		MIDI_BUF_PRODUCER_INIT(mb,buf); /* real_writebytes moves cur */
   1418   1.43.2.7      chap 		for ( ;; ) {
   1419   1.43.2.7      chap 			idxspace = MIDI_BUF_PRODUCER_REFRESH(mb,idx) - idx_cur;
   1420   1.43.2.7      chap 			bufspace = MIDI_BUF_PRODUCER_REFRESH(mb,buf) - buf_cur;
   1421   1.43.2.7      chap 			if ( idxspace >= 1  &&  bufspace >= 3  && !pollout )
   1422   1.43.2.7      chap 				break;
   1423   1.43.2.7      chap 			DPRINTFN(8,("midi_write: sleep idx=%d buf=%d\n",
   1424   1.43.2.7      chap 				 idxspace, bufspace));
   1425        1.1  augustss 			if (ioflag & IO_NDELAY) {
   1426   1.43.2.7      chap 				error = EWOULDBLOCK;
   1427   1.43.2.7      chap 				/*
   1428   1.43.2.7      chap 				 * If some amount has already been transferred,
   1429   1.43.2.7      chap 				 * the common syscall code will automagically
   1430   1.43.2.7      chap 				 * convert this to success with a short count.
   1431   1.43.2.7      chap 				 */
   1432   1.43.2.7      chap 				goto locked_exit;
   1433        1.1  augustss 			}
   1434   1.43.2.7      chap 			if ( pollout ) {
   1435   1.43.2.7      chap 				preempt(0); /* see midi_poll_output */
   1436   1.43.2.7      chap 				pollout = 0;
   1437   1.43.2.7      chap 			} else
   1438   1.43.2.7      chap 				error = midi_sleep(&sc->wchan, "mid wr",
   1439   1.43.2.7      chap 				                   &sc->out_lock);
   1440   1.43.2.7      chap 			if (error)
   1441   1.43.2.7      chap 				/*
   1442   1.43.2.7      chap 				 * Similarly, the common code will handle
   1443   1.43.2.7      chap 				 * EINTR and ERESTART properly here, changing to
   1444   1.43.2.7      chap 				 * a short count if something transferred.
   1445   1.43.2.7      chap 				 */
   1446   1.43.2.7      chap 				goto locked_exit;
   1447   1.43.2.7      chap 		}
   1448   1.43.2.7      chap 		MIDI_OUT_UNLOCK(sc,s);
   1449   1.43.2.7      chap 
   1450   1.43.2.7      chap 		/*
   1451   1.43.2.7      chap 		 * The number of bytes we can safely extract from the uio
   1452   1.43.2.7      chap 		 * depends on the available idx and buf space. Worst case,
   1453   1.43.2.7      chap 		 * every byte is a message so 1 idx is required per byte.
   1454   1.43.2.7      chap 		 * Worst case, the first byte completes a 3-byte msg in prior
   1455   1.43.2.7      chap 		 * state, and every subsequent byte is a Program Change or
   1456   1.43.2.7      chap 		 * Channel Pressure msg with running status and expands to 2
   1457   1.43.2.7      chap 		 * bytes, so the buf space reqd is 3+2(n-1) or 2n+1. So limit
   1458   1.43.2.7      chap 		 * the transfer to the min of idxspace and (bufspace-1)>>1.
   1459   1.43.2.7      chap 		 */
   1460   1.43.2.7      chap 		xfrcount = (bufspace - 1) >> 1;
   1461   1.43.2.7      chap 		if ( xfrcount > idxspace )
   1462   1.43.2.7      chap 			xfrcount = idxspace;
   1463   1.43.2.7      chap 		if ( xfrcount > sizeof inp )
   1464   1.43.2.7      chap 			xfrcount = sizeof inp;
   1465   1.43.2.7      chap 		if ( xfrcount > uio->uio_resid )
   1466   1.43.2.7      chap 			xfrcount = uio->uio_resid;
   1467   1.43.2.7      chap 
   1468   1.43.2.7      chap 		error = uiomove(inp, xfrcount, uio);
   1469        1.1  augustss #ifdef MIDI_DEBUG
   1470        1.1  augustss 		if (error)
   1471        1.9  augustss 		        printf("midi_write:(1) uiomove failed %d; "
   1472   1.43.2.7      chap 			       "xfrcount=%d inp=%p\n",
   1473   1.43.2.7      chap 			       error, xfrcount, inp);
   1474        1.1  augustss #endif
   1475   1.43.2.7      chap 		if ( error )
   1476   1.43.2.7      chap 			break;
   1477   1.43.2.7      chap 
   1478   1.43.2.7      chap 		/*
   1479   1.43.2.7      chap 		 * The number of bytes we extracted being calculated to
   1480   1.43.2.7      chap 		 * definitely fit in the buffer even with canonicalization,
   1481   1.43.2.7      chap 		 * there is no excuse for real_writebytes to return EWOULDBLOCK.
   1482   1.43.2.7      chap 		 */
   1483   1.43.2.7      chap 		error = real_writebytes(sc, inp, xfrcount);
   1484   1.43.2.7      chap 		KASSERT(error != EWOULDBLOCK);
   1485   1.43.2.7      chap 
   1486   1.43.2.7      chap 		if ( error )
   1487        1.1  augustss 			break;
   1488   1.43.2.7      chap 		/*
   1489   1.43.2.7      chap 		 * If this is a polling device and we just sent a buffer, let's
   1490   1.43.2.7      chap 		 * not send another without giving some other process a chance.
   1491   1.43.2.7      chap 		 */
   1492   1.43.2.7      chap 		if ( ! (sc->props & MIDI_PROP_OUT_INTR) )
   1493   1.43.2.7      chap 			pollout = 1;
   1494   1.43.2.7      chap 		DPRINTFN(8,("midiwrite: uio_resid now %u, props=%d\n",
   1495   1.43.2.7      chap                         uio->uio_resid, sc->props));
   1496        1.1  augustss 	}
   1497        1.1  augustss 	return error;
   1498   1.43.2.7      chap 
   1499   1.43.2.7      chap locked_exit:
   1500   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1501   1.43.2.7      chap 	return error;
   1502        1.5  augustss }
   1503        1.5  augustss 
   1504        1.5  augustss /*
   1505        1.9  augustss  * This write routine is only called from sequencer code and expects
   1506        1.5  augustss  * a write that is smaller than the MIDI buffer.
   1507        1.5  augustss  */
   1508        1.5  augustss int
   1509  1.43.2.16      chap midi_writebytes(int unit, u_char *bf, int cc)
   1510        1.5  augustss {
   1511        1.5  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
   1512        1.5  augustss 
   1513   1.43.2.2      chap 	DPRINTFN(7, ("midi_writebytes: %p, unit=%d, cc=%d %#02x %#02x %#02x\n",
   1514  1.43.2.17      chap                     sc, unit, cc, bf[0], bf[1], bf[2]));
   1515  1.43.2.17      chap 	return real_writebytes(sc, bf, cc);
   1516        1.1  augustss }
   1517        1.1  augustss 
   1518        1.1  augustss int
   1519  1.43.2.16      chap midiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct lwp *l)
   1520        1.1  augustss {
   1521        1.1  augustss 	int unit = MIDIUNIT(dev);
   1522        1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
   1523  1.43.2.16      chap 	const struct midi_hw_if *hw = sc->hw_if;
   1524        1.1  augustss 	int error;
   1525   1.43.2.7      chap 	int s;
   1526   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1527        1.1  augustss 
   1528   1.43.2.2      chap 	DPRINTFN(5,("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
   1529       1.18  tshiozak 
   1530       1.18  tshiozak 	if (sc->dying)
   1531       1.18  tshiozak 		return EIO;
   1532       1.18  tshiozak 
   1533        1.1  augustss 	error = 0;
   1534        1.1  augustss 	switch (cmd) {
   1535        1.1  augustss 	case FIONBIO:
   1536        1.1  augustss 		/* All handled in the upper FS layer. */
   1537        1.1  augustss 		break;
   1538   1.43.2.7      chap 
   1539   1.43.2.7      chap 	case FIONREAD:
   1540   1.43.2.7      chap 		/*
   1541   1.43.2.7      chap 		 * This code relies on the current implementation of midi_in
   1542   1.43.2.7      chap 		 * always updating buf and idx together in a critical section,
   1543   1.43.2.7      chap 		 * so buf always ends at a message boundary. Document this
   1544   1.43.2.7      chap 		 * ioctl as always returning a value such that the last message
   1545   1.43.2.7      chap 		 * included is complete (SysEx the only exception), and then
   1546   1.43.2.7      chap 		 * make sure the implementation doesn't regress.  NB that
   1547   1.43.2.7      chap 		 * means if this ioctl returns n and the proc then issues a
   1548   1.43.2.7      chap 		 * read of n, n bytes will be read, but if the proc issues a
   1549   1.43.2.7      chap 		 * read of m < n, fewer than m bytes may be read to ensure the
   1550   1.43.2.7      chap 		 * read ends at a message boundary.
   1551   1.43.2.7      chap 		 */
   1552   1.43.2.7      chap 		MIDI_IN_LOCK(sc,s);
   1553   1.43.2.7      chap 		MIDI_BUF_CONSUMER_INIT(&sc->inbuf,buf);
   1554   1.43.2.7      chap 		MIDI_IN_UNLOCK(sc,s);
   1555   1.43.2.7      chap 		*(int *)addr = buf_lim - buf_cur;
   1556   1.43.2.7      chap 		break;
   1557        1.1  augustss 
   1558        1.1  augustss 	case FIOASYNC:
   1559        1.1  augustss 		if (*(int *)addr) {
   1560        1.1  augustss 			if (sc->async)
   1561        1.1  augustss 				return EBUSY;
   1562  1.43.2.16      chap 			sc->async = l->l_proc;
   1563  1.43.2.16      chap 			DPRINTFN(5,("midi_ioctl: FIOASYNC %p\n", l->l_proc));
   1564        1.1  augustss 		} else
   1565        1.1  augustss 			sc->async = 0;
   1566        1.1  augustss 		break;
   1567        1.1  augustss 
   1568        1.1  augustss #if 0
   1569        1.1  augustss 	case MIDI_PRETIME:
   1570        1.1  augustss 		/* XXX OSS
   1571        1.1  augustss 		 * This should set up a read timeout, but that's
   1572        1.1  augustss 		 * why we have poll(), so there's nothing yet. */
   1573        1.1  augustss 		error = EINVAL;
   1574        1.1  augustss 		break;
   1575        1.1  augustss #endif
   1576        1.1  augustss 
   1577        1.4  augustss #ifdef MIDI_SAVE
   1578        1.4  augustss 	case MIDI_GETSAVE:
   1579        1.4  augustss 		error = copyout(&midisave, *(void **)addr, sizeof midisave);
   1580        1.4  augustss   		break;
   1581        1.4  augustss #endif
   1582        1.4  augustss 
   1583        1.1  augustss 	default:
   1584        1.1  augustss 		if (hw->ioctl)
   1585  1.43.2.16      chap 			error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, l);
   1586        1.1  augustss 		else
   1587        1.1  augustss 			error = EINVAL;
   1588        1.1  augustss 		break;
   1589        1.1  augustss 	}
   1590        1.1  augustss 	return error;
   1591        1.1  augustss }
   1592        1.1  augustss 
   1593        1.1  augustss int
   1594  1.43.2.16      chap midipoll(dev_t dev, int events, struct lwp *l)
   1595        1.1  augustss {
   1596        1.1  augustss 	int unit = MIDIUNIT(dev);
   1597        1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
   1598        1.1  augustss 	int revents = 0;
   1599       1.24      gson 	int s;
   1600   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
   1601   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1602        1.1  augustss 
   1603   1.43.2.2      chap 	DPRINTFN(6,("midipoll: %p events=0x%x\n", sc, events));
   1604        1.1  augustss 
   1605       1.18  tshiozak 	if (sc->dying)
   1606  1.43.2.16      chap 		return POLLHUP;
   1607       1.24      gson 
   1608       1.24      gson 	s = splaudio();
   1609       1.18  tshiozak 
   1610   1.43.2.7      chap 	if ((sc->flags&FREAD) && (events & (POLLIN | POLLRDNORM))) {
   1611   1.43.2.7      chap 		simple_lock(&sc->in_lock);
   1612   1.43.2.7      chap 		MIDI_BUF_CONSUMER_INIT(&sc->inbuf,idx);
   1613   1.43.2.7      chap 		if (idx_cur < idx_lim)
   1614        1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
   1615   1.43.2.7      chap 		else
   1616  1.43.2.16      chap 			selrecord(l, &sc->rsel);
   1617   1.43.2.7      chap 		simple_unlock(&sc->in_lock);
   1618   1.43.2.7      chap 	}
   1619        1.1  augustss 
   1620   1.43.2.7      chap 	if ((sc->flags&FWRITE) && (events & (POLLOUT | POLLWRNORM))) {
   1621   1.43.2.7      chap 		simple_lock(&sc->out_lock);
   1622   1.43.2.7      chap 		MIDI_BUF_PRODUCER_INIT(&sc->outbuf,idx);
   1623   1.43.2.7      chap 		MIDI_BUF_PRODUCER_INIT(&sc->outbuf,buf);
   1624   1.43.2.7      chap 		if ( idx_lim - idx_cur >= 1  &&  buf_lim - buf_cur >= 3 )
   1625   1.43.2.7      chap 			revents |= events & (POLLOUT | POLLWRNORM);
   1626   1.43.2.7      chap 		else
   1627  1.43.2.16      chap 			selrecord(l, &sc->wsel);
   1628   1.43.2.7      chap 		simple_unlock(&sc->out_lock);
   1629        1.1  augustss 	}
   1630        1.1  augustss 
   1631        1.1  augustss 	splx(s);
   1632        1.1  augustss 	return revents;
   1633       1.30  jdolecek }
   1634       1.30  jdolecek 
   1635       1.30  jdolecek static void
   1636       1.30  jdolecek filt_midirdetach(struct knote *kn)
   1637       1.30  jdolecek {
   1638       1.30  jdolecek 	struct midi_softc *sc = kn->kn_hook;
   1639       1.30  jdolecek 	int s;
   1640       1.30  jdolecek 
   1641       1.30  jdolecek 	s = splaudio();
   1642       1.31  christos 	SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext);
   1643       1.30  jdolecek 	splx(s);
   1644       1.30  jdolecek }
   1645       1.30  jdolecek 
   1646       1.30  jdolecek static int
   1647       1.30  jdolecek filt_midiread(struct knote *kn, long hint)
   1648       1.30  jdolecek {
   1649       1.30  jdolecek 	struct midi_softc *sc = kn->kn_hook;
   1650   1.43.2.7      chap 	int s;
   1651   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1652       1.30  jdolecek 
   1653       1.30  jdolecek 	/* XXXLUKEM (thorpej): please make sure this is correct. */
   1654       1.30  jdolecek 
   1655   1.43.2.7      chap 	MIDI_IN_LOCK(sc,s);
   1656   1.43.2.7      chap 	MIDI_BUF_CONSUMER_INIT(&sc->inbuf,buf);
   1657   1.43.2.7      chap 	kn->kn_data = buf_lim - buf_cur;
   1658   1.43.2.7      chap 	MIDI_IN_UNLOCK(sc,s);
   1659       1.30  jdolecek 	return (kn->kn_data > 0);
   1660       1.30  jdolecek }
   1661       1.30  jdolecek 
   1662       1.30  jdolecek static const struct filterops midiread_filtops =
   1663       1.30  jdolecek 	{ 1, NULL, filt_midirdetach, filt_midiread };
   1664       1.30  jdolecek 
   1665       1.30  jdolecek static void
   1666       1.30  jdolecek filt_midiwdetach(struct knote *kn)
   1667       1.30  jdolecek {
   1668       1.30  jdolecek 	struct midi_softc *sc = kn->kn_hook;
   1669       1.30  jdolecek 	int s;
   1670       1.30  jdolecek 
   1671       1.30  jdolecek 	s = splaudio();
   1672       1.31  christos 	SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext);
   1673       1.30  jdolecek 	splx(s);
   1674       1.30  jdolecek }
   1675       1.30  jdolecek 
   1676       1.30  jdolecek static int
   1677       1.30  jdolecek filt_midiwrite(struct knote *kn, long hint)
   1678       1.30  jdolecek {
   1679       1.30  jdolecek 	struct midi_softc *sc = kn->kn_hook;
   1680   1.43.2.7      chap 	int s;
   1681   1.43.2.7      chap 	MIDI_BUF_DECLARE(idx);
   1682   1.43.2.7      chap 	MIDI_BUF_DECLARE(buf);
   1683       1.30  jdolecek 
   1684       1.30  jdolecek 	/* XXXLUKEM (thorpej): please make sure this is correct. */
   1685       1.30  jdolecek 
   1686   1.43.2.7      chap 	MIDI_OUT_LOCK(sc,s);
   1687   1.43.2.7      chap 	MIDI_BUF_PRODUCER_INIT(&sc->outbuf,idx);
   1688   1.43.2.7      chap 	MIDI_BUF_PRODUCER_INIT(&sc->outbuf,buf);
   1689   1.43.2.7      chap 	kn->kn_data = ((buf_lim - buf_cur)-1)>>1;
   1690   1.43.2.7      chap 	if ( kn->kn_data > idx_lim - idx_cur )
   1691   1.43.2.7      chap 		kn->kn_data = idx_lim - idx_cur;
   1692   1.43.2.7      chap 	MIDI_OUT_UNLOCK(sc,s);
   1693       1.30  jdolecek 	return (kn->kn_data > 0);
   1694       1.30  jdolecek }
   1695       1.30  jdolecek 
   1696       1.30  jdolecek static const struct filterops midiwrite_filtops =
   1697       1.30  jdolecek 	{ 1, NULL, filt_midiwdetach, filt_midiwrite };
   1698       1.30  jdolecek 
   1699       1.30  jdolecek int
   1700       1.30  jdolecek midikqfilter(dev_t dev, struct knote *kn)
   1701       1.30  jdolecek {
   1702       1.30  jdolecek 	int unit = MIDIUNIT(dev);
   1703       1.30  jdolecek 	struct midi_softc *sc = midi_cd.cd_devs[unit];
   1704       1.30  jdolecek 	struct klist *klist;
   1705       1.30  jdolecek 	int s;
   1706       1.30  jdolecek 
   1707       1.30  jdolecek 	switch (kn->kn_filter) {
   1708       1.30  jdolecek 	case EVFILT_READ:
   1709       1.31  christos 		klist = &sc->rsel.sel_klist;
   1710       1.30  jdolecek 		kn->kn_fop = &midiread_filtops;
   1711       1.30  jdolecek 		break;
   1712       1.30  jdolecek 
   1713       1.30  jdolecek 	case EVFILT_WRITE:
   1714       1.31  christos 		klist = &sc->wsel.sel_klist;
   1715       1.30  jdolecek 		kn->kn_fop = &midiwrite_filtops;
   1716       1.30  jdolecek 		break;
   1717       1.30  jdolecek 
   1718       1.30  jdolecek 	default:
   1719       1.30  jdolecek 		return (1);
   1720       1.30  jdolecek 	}
   1721       1.30  jdolecek 
   1722       1.30  jdolecek 	kn->kn_hook = sc;
   1723       1.30  jdolecek 
   1724       1.30  jdolecek 	s = splaudio();
   1725       1.30  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1726       1.30  jdolecek 	splx(s);
   1727       1.30  jdolecek 
   1728       1.30  jdolecek 	return (0);
   1729        1.1  augustss }
   1730        1.1  augustss 
   1731        1.1  augustss void
   1732       1.22  augustss midi_getinfo(dev_t dev, struct midi_info *mi)
   1733        1.1  augustss {
   1734        1.1  augustss 	struct midi_softc *sc;
   1735        1.1  augustss 
   1736       1.17   thorpej 	sc = device_lookup(&midi_cd, MIDIUNIT(dev));
   1737       1.17   thorpej 	if (sc == NULL)
   1738       1.18  tshiozak 		return;
   1739       1.18  tshiozak 	if (sc->dying)
   1740        1.1  augustss 		return;
   1741       1.17   thorpej 
   1742        1.1  augustss 	sc->hw_if->getinfo(sc->hw_hdl, mi);
   1743        1.4  augustss }
   1744        1.4  augustss 
   1745       1.10  drochner #endif /* NMIDI > 0 */
   1746       1.10  drochner 
   1747       1.10  drochner #if NMIDI > 0 || NMIDIBUS > 0
   1748       1.10  drochner 
   1749       1.22  augustss int	audioprint(void *, const char *);
   1750        1.4  augustss 
   1751       1.12  augustss struct device *
   1752  1.43.2.16      chap midi_attach_mi(const struct midi_hw_if *mhwp, void *hdlp, struct device *dev)
   1753        1.4  augustss {
   1754        1.4  augustss 	struct audio_attach_args arg;
   1755        1.4  augustss 
   1756        1.4  augustss #ifdef DIAGNOSTIC
   1757        1.4  augustss 	if (mhwp == NULL) {
   1758       1.32   thorpej 		aprint_error("midi_attach_mi: NULL\n");
   1759       1.12  augustss 		return (0);
   1760        1.4  augustss 	}
   1761        1.4  augustss #endif
   1762        1.4  augustss 	arg.type = AUDIODEV_TYPE_MIDI;
   1763        1.4  augustss 	arg.hwif = mhwp;
   1764        1.4  augustss 	arg.hdl = hdlp;
   1765       1.12  augustss 	return (config_found(dev, &arg, audioprint));
   1766        1.1  augustss }
   1767        1.1  augustss 
   1768       1.10  drochner #endif /* NMIDI > 0 || NMIDIBUS > 0 */
   1769