Home | History | Annotate | Line # | Download | only in dev
midi.c revision 1.12.2.3
      1  1.12.2.3    bouyer /*	$NetBSD: midi.c,v 1.12.2.3 2001/01/18 09:23:15 bouyer 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.8  augustss  * by Lennart Augustsson (augustss (at) netbsd.org).
      9       1.1  augustss  *
     10       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11       1.1  augustss  * modification, are permitted provided that the following conditions
     12       1.1  augustss  * are met:
     13       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18       1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19       1.1  augustss  *    must display the following acknowledgement:
     20       1.1  augustss  *        This product includes software developed by the NetBSD
     21       1.1  augustss  *        Foundation, Inc. and its contributors.
     22       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  augustss  *    contributors may be used to endorse or promote products derived
     24       1.1  augustss  *    from this software without specific prior written permission.
     25       1.1  augustss  *
     26       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  augustss  */
     38       1.1  augustss 
     39       1.1  augustss #include "midi.h"
     40       1.1  augustss #include "sequencer.h"
     41       1.1  augustss 
     42       1.1  augustss #include <sys/param.h>
     43       1.1  augustss #include <sys/ioctl.h>
     44       1.1  augustss #include <sys/fcntl.h>
     45       1.1  augustss #include <sys/vnode.h>
     46       1.1  augustss #include <sys/select.h>
     47       1.1  augustss #include <sys/poll.h>
     48       1.1  augustss #include <sys/malloc.h>
     49       1.1  augustss #include <sys/proc.h>
     50       1.1  augustss #include <sys/systm.h>
     51  1.12.2.2    bouyer #include <sys/callout.h>
     52       1.1  augustss #include <sys/syslog.h>
     53       1.1  augustss #include <sys/kernel.h>
     54       1.1  augustss #include <sys/signalvar.h>
     55       1.1  augustss #include <sys/conf.h>
     56       1.1  augustss #include <sys/audioio.h>
     57       1.1  augustss #include <sys/midiio.h>
     58       1.1  augustss #include <sys/device.h>
     59       1.1  augustss 
     60       1.1  augustss #include <dev/audio_if.h>
     61       1.4  augustss #include <dev/midi_if.h>
     62       1.1  augustss #include <dev/midivar.h>
     63       1.1  augustss 
     64      1.10  drochner #if NMIDI > 0
     65      1.10  drochner 
     66       1.1  augustss #ifdef AUDIO_DEBUG
     67       1.1  augustss #define DPRINTF(x)	if (mididebug) printf x
     68       1.1  augustss #define DPRINTFN(n,x)	if (mididebug >= (n)) printf x
     69       1.1  augustss int	mididebug = 0;
     70       1.1  augustss #else
     71       1.1  augustss #define DPRINTF(x)
     72       1.1  augustss #define DPRINTFN(n,x)
     73       1.1  augustss #endif
     74       1.1  augustss 
     75       1.1  augustss int midi_wait;
     76       1.1  augustss 
     77       1.2  augustss void	midi_in __P((void *, int));
     78       1.2  augustss void	midi_out __P((void *));
     79       1.2  augustss int	midi_start_output __P((struct midi_softc *, int));
     80       1.2  augustss int	midi_sleep_timo __P((int *, char *, int));
     81       1.2  augustss int	midi_sleep __P((int *, char *));
     82       1.2  augustss void	midi_wakeup __P((int *));
     83       1.2  augustss void	midi_initbuf __P((struct midi_buffer *));
     84       1.2  augustss void	midi_timeout __P((void *));
     85       1.1  augustss 
     86       1.1  augustss int	midiprobe __P((struct device *, struct cfdata *, void *));
     87       1.1  augustss void	midiattach __P((struct device *, struct device *, void *));
     88  1.12.2.3    bouyer int	mididetach __P((struct device *, int));
     89  1.12.2.3    bouyer int	midiactivate __P((struct device *, enum devact));
     90       1.1  augustss 
     91       1.1  augustss struct cfattach midi_ca = {
     92  1.12.2.3    bouyer 	sizeof(struct midi_softc), midiprobe, midiattach,
     93  1.12.2.3    bouyer 	mididetach, midiactivate
     94       1.1  augustss };
     95       1.1  augustss 
     96       1.4  augustss #ifdef MIDI_SAVE
     97       1.4  augustss #define MIDI_SAVE_SIZE 100000
     98       1.4  augustss int midicnt;
     99       1.4  augustss struct {
    100       1.4  augustss 	int cnt;
    101       1.4  augustss 	u_char buf[MIDI_SAVE_SIZE];
    102       1.4  augustss } midisave;
    103       1.4  augustss #define MIDI_GETSAVE		_IOWR('m', 100, int)
    104       1.4  augustss 
    105       1.4  augustss #endif
    106       1.4  augustss 
    107       1.1  augustss extern struct cfdriver midi_cd;
    108       1.1  augustss 
    109       1.1  augustss int
    110       1.1  augustss midiprobe(parent, match, aux)
    111       1.1  augustss 	struct device *parent;
    112       1.1  augustss 	struct cfdata *match;
    113       1.1  augustss 	void *aux;
    114       1.1  augustss {
    115       1.1  augustss 	struct audio_attach_args *sa = aux;
    116       1.1  augustss 
    117       1.1  augustss 	DPRINTFN(6,("midiprobe: type=%d sa=%p hw=%p\n",
    118       1.1  augustss 		 sa->type, sa, sa->hwif));
    119  1.12.2.2    bouyer 	return (sa->type == AUDIODEV_TYPE_MIDI);
    120       1.1  augustss }
    121       1.1  augustss 
    122       1.1  augustss void
    123       1.1  augustss midiattach(parent, self, aux)
    124       1.1  augustss 	struct device *parent, *self;
    125       1.1  augustss 	void *aux;
    126       1.1  augustss {
    127       1.1  augustss 	struct midi_softc *sc = (void *)self;
    128       1.1  augustss 	struct audio_attach_args *sa = aux;
    129       1.1  augustss 	struct midi_hw_if *hwp = sa->hwif;
    130       1.1  augustss 	void *hdlp = sa->hdl;
    131       1.1  augustss 
    132       1.1  augustss 	DPRINTFN(6, ("MIDI attach\n"));
    133       1.1  augustss 
    134       1.1  augustss #ifdef DIAGNOSTIC
    135       1.1  augustss 	if (hwp == 0 ||
    136       1.1  augustss 	    hwp->open == 0 ||
    137       1.1  augustss 	    hwp->close == 0 ||
    138       1.1  augustss 	    hwp->output == 0 ||
    139       1.1  augustss 	    hwp->getinfo == 0) {
    140       1.1  augustss 		printf("midi: missing method\n");
    141       1.1  augustss 		return;
    142       1.1  augustss 	}
    143       1.1  augustss #endif
    144  1.12.2.2    bouyer 
    145  1.12.2.2    bouyer 	callout_init(&sc->sc_callout);
    146  1.12.2.2    bouyer 
    147       1.1  augustss 	sc->hw_if = hwp;
    148       1.1  augustss 	sc->hw_hdl = hdlp;
    149  1.12.2.3    bouyer 	sc->dying = 0;
    150       1.2  augustss 	midi_attach(sc, parent);
    151       1.2  augustss }
    152       1.2  augustss 
    153  1.12.2.3    bouyer int
    154  1.12.2.3    bouyer midiactivate(self, act)
    155  1.12.2.3    bouyer 	struct device *self;
    156  1.12.2.3    bouyer 	enum devact act;
    157  1.12.2.3    bouyer {
    158  1.12.2.3    bouyer 	struct midi_softc *sc = (struct midi_softc *)self;
    159  1.12.2.3    bouyer 
    160  1.12.2.3    bouyer 	switch (act) {
    161  1.12.2.3    bouyer 	case DVACT_ACTIVATE:
    162  1.12.2.3    bouyer 		return (EOPNOTSUPP);
    163  1.12.2.3    bouyer 		break;
    164  1.12.2.3    bouyer 
    165  1.12.2.3    bouyer 	case DVACT_DEACTIVATE:
    166  1.12.2.3    bouyer 		sc->dying = 1;
    167  1.12.2.3    bouyer 		break;
    168  1.12.2.3    bouyer 	}
    169  1.12.2.3    bouyer 	return (0);
    170  1.12.2.3    bouyer }
    171  1.12.2.3    bouyer 
    172  1.12.2.3    bouyer int
    173  1.12.2.3    bouyer mididetach(self, flags)
    174  1.12.2.3    bouyer 	struct device *self;
    175  1.12.2.3    bouyer 	int flags;
    176  1.12.2.3    bouyer {
    177  1.12.2.3    bouyer 	struct midi_softc *sc = (struct midi_softc *)self;
    178  1.12.2.3    bouyer 	int maj, mn;
    179  1.12.2.3    bouyer 
    180  1.12.2.3    bouyer 	DPRINTF(("midi_detach: sc=%p flags=%d\n", sc, flags));
    181  1.12.2.3    bouyer 
    182  1.12.2.3    bouyer 	sc->dying = 1;
    183  1.12.2.3    bouyer 
    184  1.12.2.3    bouyer 	wakeup(&sc->wchan);
    185  1.12.2.3    bouyer 	wakeup(&sc->rchan);
    186  1.12.2.3    bouyer 
    187  1.12.2.3    bouyer 	/* locate the major number */
    188  1.12.2.3    bouyer 	for (maj = 0; maj < nchrdev; maj++)
    189  1.12.2.3    bouyer 		if (cdevsw[maj].d_open == midiopen)
    190  1.12.2.3    bouyer 			break;
    191  1.12.2.3    bouyer 
    192  1.12.2.3    bouyer 	/* Nuke the vnodes for any open instances (calls close). */
    193  1.12.2.3    bouyer 	mn = self->dv_unit;
    194  1.12.2.3    bouyer 	vdevgone(maj, mn, mn, VCHR);
    195  1.12.2.3    bouyer 
    196  1.12.2.3    bouyer 	return (0);
    197  1.12.2.3    bouyer }
    198  1.12.2.3    bouyer 
    199       1.2  augustss void
    200       1.2  augustss midi_attach(sc, parent)
    201       1.2  augustss 	struct midi_softc *sc;
    202       1.2  augustss 	struct device *parent;
    203       1.2  augustss {
    204       1.2  augustss 	struct midi_info mi;
    205       1.2  augustss 
    206       1.1  augustss 	sc->isopen = 0;
    207       1.1  augustss 
    208       1.1  augustss 	midi_wait = MIDI_WAIT * hz / 1000000;
    209       1.1  augustss 	if (midi_wait == 0)
    210       1.1  augustss 		midi_wait = 1;
    211       1.1  augustss 
    212       1.2  augustss 	sc->sc_dev = parent;
    213       1.2  augustss 	sc->hw_if->getinfo(sc->hw_hdl, &mi);
    214       1.1  augustss 	sc->props = mi.props;
    215  1.12.2.1   thorpej 	printf(": %s\n", mi.name);
    216       1.1  augustss }
    217       1.1  augustss 
    218       1.1  augustss int
    219       1.1  augustss midi_unit_count()
    220       1.1  augustss {
    221       1.1  augustss 	return midi_cd.cd_ndevs;
    222       1.1  augustss }
    223       1.1  augustss 
    224       1.1  augustss void
    225       1.1  augustss midi_initbuf(mb)
    226       1.1  augustss 	struct midi_buffer *mb;
    227       1.1  augustss {
    228       1.1  augustss 	mb->used = 0;
    229       1.1  augustss 	mb->usedhigh = MIDI_BUFSIZE;
    230       1.1  augustss 	mb->end = mb->start + mb->usedhigh;
    231       1.1  augustss 	mb->inp = mb->outp = mb->start;
    232       1.1  augustss }
    233       1.1  augustss 
    234       1.2  augustss int
    235       1.1  augustss midi_sleep_timo(chan, label, timo)
    236       1.1  augustss 	int *chan;
    237       1.1  augustss 	char *label;
    238       1.1  augustss 	int timo;
    239       1.1  augustss {
    240       1.1  augustss 	int st;
    241       1.1  augustss 
    242       1.1  augustss 	if (!label)
    243       1.1  augustss 		label = "midi";
    244       1.1  augustss 
    245       1.1  augustss 	DPRINTFN(5, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
    246       1.1  augustss 	*chan = 1;
    247       1.1  augustss 	st = tsleep(chan, PWAIT | PCATCH, label, timo);
    248       1.1  augustss 	*chan = 0;
    249       1.1  augustss #ifdef MIDI_DEBUG
    250       1.1  augustss 	if (st != 0)
    251       1.1  augustss 		printf("midi_sleep: %d\n", st);
    252       1.1  augustss #endif
    253       1.1  augustss 	return st;
    254       1.1  augustss }
    255       1.1  augustss 
    256       1.2  augustss int
    257       1.1  augustss midi_sleep(chan, label)
    258       1.1  augustss 	int *chan;
    259       1.1  augustss 	char *label;
    260       1.1  augustss {
    261       1.1  augustss 	return midi_sleep_timo(chan, label, 0);
    262       1.1  augustss }
    263       1.1  augustss 
    264       1.2  augustss void
    265       1.1  augustss midi_wakeup(chan)
    266       1.1  augustss 	int *chan;
    267       1.1  augustss {
    268       1.1  augustss 	if (*chan) {
    269       1.1  augustss 		DPRINTFN(5, ("midi_wakeup: %p\n", chan));
    270       1.1  augustss 		wakeup(chan);
    271       1.1  augustss 		*chan = 0;
    272       1.1  augustss 	}
    273       1.1  augustss }
    274       1.1  augustss 
    275       1.1  augustss static int midi_lengths[] = { 2,2,2,2,1,1,2,0 };
    276       1.1  augustss /* Number of bytes in a MIDI command */
    277       1.1  augustss #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
    278       1.1  augustss 
    279       1.1  augustss void
    280       1.1  augustss midi_in(addr, data)
    281       1.1  augustss 	void *addr;
    282       1.1  augustss 	int data;
    283       1.1  augustss {
    284       1.1  augustss 	struct midi_softc *sc = addr;
    285       1.1  augustss 	struct midi_buffer *mb = &sc->inbuf;
    286       1.1  augustss 	int i;
    287       1.1  augustss 
    288       1.3  augustss 	if (!sc->isopen)
    289       1.3  augustss 		return;
    290       1.1  augustss 	if (data == MIDI_ACK)
    291       1.1  augustss 		return;
    292  1.12.2.2    bouyer 
    293  1.12.2.2    bouyer 	DPRINTFN(3, ("midi_in: sc=%p data=0x%02x state=%d pos=%d\n",
    294  1.12.2.2    bouyer 		     sc, data, sc->in_state, sc->in_pos));
    295  1.12.2.2    bouyer 
    296       1.1  augustss 	if (!(sc->flags & FREAD))
    297       1.1  augustss 		return;		/* discard data if not reading */
    298       1.1  augustss 
    299       1.1  augustss 	switch(sc->in_state) {
    300       1.1  augustss 	case MIDI_IN_START:
    301       1.1  augustss 		if (MIDI_IS_STATUS(data)) {
    302       1.1  augustss 			switch(data) {
    303       1.1  augustss 			case 0xf0: /* Sysex */
    304       1.1  augustss 				sc->in_state = MIDI_IN_SYSEX;
    305       1.1  augustss 				break;
    306       1.1  augustss 			case 0xf1: /* MTC quarter frame */
    307       1.1  augustss 			case 0xf3: /* Song select */
    308       1.1  augustss 				sc->in_state = MIDI_IN_DATA;
    309       1.1  augustss 				sc->in_msg[0] = data;
    310       1.1  augustss 				sc->in_pos = 1;
    311       1.1  augustss 				sc->in_left = 1;
    312       1.1  augustss 				break;
    313       1.1  augustss 			case 0xf2: /* Song position pointer */
    314       1.1  augustss 				sc->in_state = MIDI_IN_DATA;
    315       1.1  augustss 				sc->in_msg[0] = data;
    316       1.1  augustss 				sc->in_pos = 1;
    317       1.1  augustss 				sc->in_left = 2;
    318       1.1  augustss 				break;
    319       1.1  augustss 			default:
    320       1.1  augustss 				if (MIDI_IS_COMMON(data)) {
    321       1.1  augustss 					sc->in_msg[0] = data;
    322       1.1  augustss 					sc->in_pos = 1;
    323       1.1  augustss 					goto deliver;
    324       1.1  augustss 				} else {
    325       1.1  augustss 					sc->in_state = MIDI_IN_DATA;
    326       1.1  augustss 					sc->in_msg[0] = sc->in_status = data;
    327       1.1  augustss 					sc->in_pos = 1;
    328  1.12.2.2    bouyer 					sc->in_left = MIDI_LENGTH(data);
    329       1.1  augustss 				}
    330       1.1  augustss 				break;
    331       1.1  augustss 			}
    332       1.1  augustss 		} else {
    333       1.1  augustss 			if (MIDI_IS_STATUS(sc->in_status)) {
    334       1.1  augustss 				sc->in_state = MIDI_IN_DATA;
    335       1.1  augustss 				sc->in_msg[0] = sc->in_status;
    336       1.1  augustss 				sc->in_msg[1] = data;
    337       1.1  augustss 				sc->in_pos = 2;
    338       1.1  augustss 				sc->in_left = MIDI_LENGTH(sc->in_status) - 1;
    339       1.1  augustss 			}
    340       1.1  augustss 		}
    341       1.1  augustss 		return;
    342       1.1  augustss 	case MIDI_IN_DATA:
    343       1.1  augustss 		sc->in_msg[sc->in_pos++] = data;
    344       1.1  augustss 		if (--sc->in_left <= 0)
    345       1.1  augustss 			break;	/* deliver data */
    346       1.1  augustss 		return;
    347       1.1  augustss 	case MIDI_IN_SYSEX:
    348       1.1  augustss 		if (data == MIDI_SYSEX_END)
    349       1.1  augustss 			sc->in_state = MIDI_IN_START;
    350       1.1  augustss 		return;
    351       1.1  augustss 	}
    352       1.1  augustss deliver:
    353       1.1  augustss 	sc->in_state = MIDI_IN_START;
    354       1.1  augustss #if NSEQUENCER > 0
    355       1.1  augustss 	if (sc->seqopen) {
    356       1.7  augustss 		extern void midiseq_in __P((struct midi_dev *,u_char *,int));
    357       1.7  augustss 		midiseq_in(sc->seq_md, sc->in_msg, sc->in_pos);
    358       1.1  augustss 		return;
    359       1.1  augustss 	}
    360       1.1  augustss #endif
    361       1.1  augustss 
    362       1.1  augustss 	if (mb->used + sc->in_pos > mb->usedhigh) {
    363       1.1  augustss 		DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
    364       1.1  augustss 			 sc->in_msg[0]));
    365       1.1  augustss 		return;
    366       1.1  augustss 	}
    367       1.1  augustss 	for (i = 0; i < sc->in_pos; i++) {
    368       1.1  augustss 		*mb->inp++ = sc->in_msg[i];
    369       1.1  augustss 		if (mb->inp >= mb->end)
    370       1.1  augustss 			mb->inp = mb->start;
    371       1.1  augustss 		mb->used++;
    372       1.1  augustss 	}
    373       1.1  augustss 	midi_wakeup(&sc->rchan);
    374       1.1  augustss 	selwakeup(&sc->rsel);
    375       1.1  augustss 	if (sc->async)
    376       1.1  augustss 		psignal(sc->async, SIGIO);
    377       1.1  augustss }
    378       1.1  augustss 
    379       1.1  augustss void
    380       1.1  augustss midi_out(addr)
    381       1.1  augustss 	void *addr;
    382       1.1  augustss {
    383       1.1  augustss 	struct midi_softc *sc = addr;
    384       1.3  augustss 
    385       1.3  augustss 	if (!sc->isopen)
    386       1.3  augustss 		return;
    387       1.1  augustss 	DPRINTFN(3, ("midi_out: %p\n", sc));
    388       1.1  augustss 	midi_start_output(sc, 1);
    389       1.1  augustss }
    390       1.1  augustss 
    391       1.1  augustss int
    392       1.1  augustss midiopen(dev, flags, ifmt, p)
    393       1.1  augustss 	dev_t dev;
    394       1.1  augustss 	int flags, ifmt;
    395       1.1  augustss 	struct proc *p;
    396       1.1  augustss {
    397       1.1  augustss 	struct midi_softc *sc;
    398       1.1  augustss 	struct midi_hw_if *hw;
    399       1.1  augustss 	int error;
    400       1.1  augustss 
    401  1.12.2.2    bouyer 	sc = device_lookup(&midi_cd, MIDIUNIT(dev));
    402  1.12.2.2    bouyer 	if (sc == NULL)
    403  1.12.2.2    bouyer 		return (ENXIO);
    404  1.12.2.3    bouyer 	if (sc->dying)
    405  1.12.2.3    bouyer 		return (EIO);
    406  1.12.2.2    bouyer 
    407       1.1  augustss 	DPRINTF(("midiopen %p\n", sc));
    408       1.1  augustss 
    409       1.1  augustss 	hw = sc->hw_if;
    410       1.1  augustss 	if (!hw)
    411       1.1  augustss 		return ENXIO;
    412       1.1  augustss 	if (sc->isopen)
    413       1.1  augustss 		return EBUSY;
    414       1.1  augustss 	sc->in_state = MIDI_IN_START;
    415       1.1  augustss 	sc->in_status = 0;
    416       1.1  augustss 	error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
    417       1.1  augustss 	if (error)
    418       1.1  augustss 		return error;
    419       1.1  augustss 	sc->isopen++;
    420       1.6   mycroft 	midi_initbuf(&sc->outbuf);
    421       1.6   mycroft 	midi_initbuf(&sc->inbuf);
    422       1.1  augustss 	sc->flags = flags;
    423       1.1  augustss 	sc->rchan = 0;
    424       1.1  augustss 	sc->wchan = 0;
    425       1.1  augustss 	sc->pbus = 0;
    426       1.1  augustss 	sc->async = 0;
    427       1.1  augustss 
    428       1.4  augustss #ifdef MIDI_SAVE
    429       1.4  augustss 	if (midicnt != 0) {
    430       1.4  augustss 		midisave.cnt = midicnt;
    431       1.4  augustss 		midicnt = 0;
    432       1.4  augustss 	}
    433       1.4  augustss #endif
    434       1.4  augustss 
    435       1.1  augustss 	return 0;
    436       1.1  augustss }
    437       1.1  augustss 
    438       1.1  augustss int
    439       1.1  augustss midiclose(dev, flags, ifmt, p)
    440       1.1  augustss 	dev_t dev;
    441       1.1  augustss 	int flags, ifmt;
    442       1.1  augustss 	struct proc *p;
    443       1.1  augustss {
    444       1.1  augustss 	int unit = MIDIUNIT(dev);
    445       1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    446       1.1  augustss 	struct midi_hw_if *hw = sc->hw_if;
    447       1.1  augustss 	int s, error;
    448       1.1  augustss 
    449       1.1  augustss 	DPRINTF(("midiclose %p\n", sc));
    450       1.1  augustss 
    451       1.1  augustss 	midi_start_output(sc, 0);
    452       1.1  augustss 	error = 0;
    453       1.1  augustss 	s = splaudio();
    454       1.1  augustss 	while (sc->outbuf.used > 0 && !error) {
    455       1.1  augustss 		DPRINTFN(2,("midiclose sleep used=%d\n", sc->outbuf.used));
    456       1.3  augustss 		error = midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz);
    457       1.1  augustss 	}
    458       1.6   mycroft 	splx(s);
    459       1.3  augustss 	sc->isopen = 0;
    460       1.1  augustss 	hw->close(sc->hw_hdl);
    461       1.1  augustss #if NSEQUENCER > 0
    462       1.1  augustss 	sc->seqopen = 0;
    463       1.7  augustss 	sc->seq_md = 0;
    464       1.1  augustss #endif
    465       1.1  augustss 	return 0;
    466       1.1  augustss }
    467       1.1  augustss 
    468       1.1  augustss int
    469       1.1  augustss midiread(dev, uio, ioflag)
    470       1.1  augustss 	dev_t dev;
    471       1.1  augustss 	struct uio *uio;
    472       1.1  augustss 	int ioflag;
    473       1.1  augustss {
    474       1.1  augustss 	int unit = MIDIUNIT(dev);
    475       1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    476       1.1  augustss 	struct midi_buffer *mb = &sc->inbuf;
    477       1.1  augustss 	int error;
    478       1.1  augustss 	u_char *outp;
    479       1.1  augustss 	int used, cc, n, resid;
    480       1.1  augustss 	int s;
    481       1.1  augustss 
    482      1.11   nathanw 	DPRINTF(("midiread: %p, count=%lu\n", sc,
    483      1.11   nathanw 		 (unsigned long)uio->uio_resid));
    484       1.1  augustss 
    485  1.12.2.3    bouyer 	if (sc->dying)
    486  1.12.2.3    bouyer 		return EIO;
    487  1.12.2.3    bouyer 
    488       1.1  augustss 	error = 0;
    489       1.1  augustss 	resid = uio->uio_resid;
    490       1.1  augustss 	while (uio->uio_resid == resid && !error) {
    491       1.1  augustss 		s = splaudio();
    492       1.1  augustss 		while (mb->used <= 0) {
    493       1.1  augustss 			if (ioflag & IO_NDELAY) {
    494       1.1  augustss 				splx(s);
    495       1.1  augustss 				return EWOULDBLOCK;
    496       1.1  augustss 			}
    497       1.1  augustss 			error = midi_sleep(&sc->rchan, "mid rd");
    498       1.1  augustss 			if (error) {
    499       1.1  augustss 				splx(s);
    500       1.1  augustss 				return error;
    501       1.1  augustss 			}
    502       1.1  augustss 		}
    503       1.1  augustss 		used = mb->used;
    504       1.1  augustss 		outp = mb->outp;
    505       1.1  augustss 		splx(s);
    506  1.12.2.3    bouyer 		if (sc->dying)
    507  1.12.2.3    bouyer 			return EIO;
    508       1.1  augustss 		cc = used;	/* maximum to read */
    509       1.1  augustss 		n = mb->end - outp;
    510       1.1  augustss 		if (n < cc)
    511       1.1  augustss 			cc = n;	/* don't read beyond end of buffer */
    512       1.1  augustss 		if (uio->uio_resid < cc)
    513       1.1  augustss 			cc = uio->uio_resid; /* and no more than we want */
    514       1.1  augustss 		DPRINTFN(3, ("midiread: uiomove cc=%d\n", cc));
    515       1.1  augustss 		error = uiomove(outp, cc, uio);
    516       1.1  augustss 		if (error)
    517       1.1  augustss 			break;
    518       1.1  augustss 		used -= cc;
    519       1.1  augustss 		outp += cc;
    520       1.1  augustss 		if (outp >= mb->end)
    521       1.1  augustss 			outp = mb->start;
    522       1.1  augustss 		s = splaudio();
    523       1.1  augustss 		mb->outp = outp;
    524       1.1  augustss 		mb->used = used;
    525       1.1  augustss 		splx(s);
    526       1.1  augustss 	}
    527       1.1  augustss 	return error;
    528       1.1  augustss }
    529       1.1  augustss 
    530       1.1  augustss void
    531       1.1  augustss midi_timeout(arg)
    532       1.1  augustss 	void *arg;
    533       1.1  augustss {
    534       1.1  augustss 	struct midi_softc *sc = arg;
    535       1.1  augustss 
    536       1.1  augustss 	DPRINTFN(3,("midi_timeout: %p\n", sc));
    537       1.1  augustss 	midi_start_output(sc, 1);
    538       1.1  augustss }
    539       1.1  augustss 
    540       1.1  augustss int
    541       1.1  augustss midi_start_output(sc, intr)
    542       1.1  augustss 	struct midi_softc *sc;
    543       1.1  augustss 	int intr;
    544       1.1  augustss {
    545       1.1  augustss 	struct midi_buffer *mb = &sc->outbuf;
    546       1.1  augustss 	u_char *outp;
    547       1.1  augustss 	int error;
    548       1.1  augustss 	int s;
    549       1.1  augustss 	int i, mmax;
    550       1.1  augustss 
    551       1.1  augustss 	error = 0;
    552       1.1  augustss 	mmax = sc->props & MIDI_PROP_OUT_INTR ? 1 : MIDI_MAX_WRITE;
    553  1.12.2.3    bouyer 
    554  1.12.2.3    bouyer 	if (sc->dying)
    555  1.12.2.3    bouyer 		return EIO;
    556  1.12.2.3    bouyer 
    557       1.1  augustss 	s = splaudio();
    558       1.1  augustss 	if (sc->pbus && !intr) {
    559       1.1  augustss 		DPRINTFN(4, ("midi_start_output: busy\n"));
    560       1.1  augustss 		splx(s);
    561       1.1  augustss 		return 0;
    562       1.1  augustss 	}
    563       1.1  augustss 	sc->pbus = 1;
    564       1.1  augustss 	for (i = 0; i < mmax && mb->used > 0 && !error; i++) {
    565       1.1  augustss 		outp = mb->outp;
    566       1.1  augustss 		splx(s);
    567       1.1  augustss 		DPRINTFN(4, ("midi_start_output: %p i=%d, data=0x%02x\n",
    568       1.1  augustss 			     sc, i, *outp));
    569       1.4  augustss #ifdef MIDI_SAVE
    570       1.4  augustss 		midisave.buf[midicnt] = *outp;
    571       1.4  augustss 		midicnt = (midicnt + 1) % MIDI_SAVE_SIZE;
    572       1.4  augustss #endif
    573       1.1  augustss 		error = sc->hw_if->output(sc->hw_hdl, *outp++);
    574       1.1  augustss 		if (outp >= mb->end)
    575       1.1  augustss 			outp = mb->start;
    576       1.1  augustss 		s = splaudio();
    577       1.1  augustss 		mb->outp = outp;
    578       1.1  augustss 		mb->used--;
    579       1.1  augustss 	}
    580       1.1  augustss 	midi_wakeup(&sc->wchan);
    581       1.1  augustss 	selwakeup(&sc->wsel);
    582       1.1  augustss 	if (sc->async)
    583       1.1  augustss 		psignal(sc->async, SIGIO);
    584       1.1  augustss 	if (mb->used > 0) {
    585       1.1  augustss 		if (!(sc->props & MIDI_PROP_OUT_INTR))
    586  1.12.2.2    bouyer 			callout_reset(&sc->sc_callout, midi_wait,
    587  1.12.2.2    bouyer 			    midi_timeout, sc);
    588       1.1  augustss 	} else
    589       1.1  augustss 		sc->pbus = 0;
    590       1.1  augustss 	splx(s);
    591       1.1  augustss 	return error;
    592       1.1  augustss }
    593       1.1  augustss 
    594       1.1  augustss int
    595       1.1  augustss midiwrite(dev, uio, ioflag)
    596       1.1  augustss 	dev_t dev;
    597       1.1  augustss 	struct uio *uio;
    598       1.1  augustss 	int ioflag;
    599       1.1  augustss {
    600       1.1  augustss 	int unit = MIDIUNIT(dev);
    601       1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    602       1.1  augustss 	struct midi_buffer *mb = &sc->outbuf;
    603       1.1  augustss 	int error;
    604       1.1  augustss 	u_char *inp;
    605       1.1  augustss 	int used, cc, n;
    606       1.1  augustss 	int s;
    607       1.1  augustss 
    608      1.11   nathanw 	DPRINTFN(2, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
    609      1.11   nathanw 		     (unsigned long)uio->uio_resid));
    610       1.1  augustss 
    611  1.12.2.3    bouyer 	if (sc->dying)
    612  1.12.2.3    bouyer 		return EIO;
    613  1.12.2.3    bouyer 
    614       1.1  augustss 	error = 0;
    615       1.1  augustss 	while (uio->uio_resid > 0 && !error) {
    616       1.1  augustss 		s = splaudio();
    617       1.1  augustss 		if (mb->used >= mb->usedhigh) {
    618       1.1  augustss 			DPRINTFN(3,("midi_write: sleep used=%d hiwat=%d\n",
    619       1.1  augustss 				 mb->used, mb->usedhigh));
    620       1.1  augustss 			if (ioflag & IO_NDELAY) {
    621       1.1  augustss 				splx(s);
    622       1.1  augustss 				return EWOULDBLOCK;
    623       1.1  augustss 			}
    624       1.1  augustss 			error = midi_sleep(&sc->wchan, "mid wr");
    625       1.1  augustss 			if (error) {
    626       1.1  augustss 				splx(s);
    627       1.1  augustss 				return error;
    628       1.1  augustss 			}
    629       1.1  augustss 		}
    630       1.1  augustss 		used = mb->used;
    631       1.1  augustss 		inp = mb->inp;
    632       1.1  augustss 		splx(s);
    633  1.12.2.3    bouyer 		if (sc->dying)
    634  1.12.2.3    bouyer 			return EIO;
    635       1.1  augustss 		cc = mb->usedhigh - used; 	/* maximum to write */
    636       1.1  augustss 		n = mb->end - inp;
    637       1.1  augustss 		if (n < cc)
    638       1.9  augustss 			cc = n;		/* don't write beyond end of buffer */
    639       1.1  augustss 		if (uio->uio_resid < cc)
    640       1.1  augustss 			cc = uio->uio_resid; 	/* and no more than we have */
    641       1.1  augustss 		error = uiomove(inp, cc, uio);
    642       1.1  augustss #ifdef MIDI_DEBUG
    643       1.1  augustss 		if (error)
    644       1.9  augustss 		        printf("midi_write:(1) uiomove failed %d; "
    645       1.9  augustss 			       "cc=%d inp=%p\n",
    646       1.1  augustss 			       error, cc, inp);
    647       1.1  augustss #endif
    648       1.1  augustss 		if (error)
    649       1.1  augustss 			break;
    650       1.1  augustss 		inp = mb->inp + cc;
    651       1.1  augustss 		if (inp >= mb->end)
    652       1.1  augustss 			inp = mb->start;
    653       1.1  augustss 		s = splaudio();
    654       1.1  augustss 		mb->inp = inp;
    655       1.1  augustss 		mb->used += cc;
    656       1.1  augustss 		splx(s);
    657       1.1  augustss 		error = midi_start_output(sc, 0);
    658       1.1  augustss 	}
    659       1.1  augustss 	return error;
    660       1.5  augustss }
    661       1.5  augustss 
    662       1.5  augustss /*
    663       1.9  augustss  * This write routine is only called from sequencer code and expects
    664       1.5  augustss  * a write that is smaller than the MIDI buffer.
    665       1.5  augustss  */
    666       1.5  augustss int
    667       1.5  augustss midi_writebytes(unit, buf, cc)
    668       1.5  augustss 	int unit;
    669       1.5  augustss 	u_char *buf;
    670       1.5  augustss 	int cc;
    671       1.5  augustss {
    672       1.5  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    673       1.5  augustss 	struct midi_buffer *mb = &sc->outbuf;
    674       1.5  augustss 	int n, s;
    675       1.5  augustss 
    676       1.5  augustss 	DPRINTFN(2, ("midi_writebytes: %p, unit=%d, cc=%d\n", sc, unit, cc));
    677       1.9  augustss 	DPRINTFN(3, ("midi_writebytes: %x %x %x\n",buf[0],buf[1],buf[2]));
    678       1.5  augustss 
    679  1.12.2.3    bouyer 	if (sc->dying)
    680  1.12.2.3    bouyer 		return EIO;
    681  1.12.2.3    bouyer 
    682       1.5  augustss 	s = splaudio();
    683       1.5  augustss 	if (mb->used + cc >= mb->usedhigh) {
    684       1.5  augustss 		splx(s);
    685       1.5  augustss 		return (EWOULDBLOCK);
    686       1.5  augustss 	}
    687       1.5  augustss 	n = mb->end - mb->inp;
    688       1.5  augustss 	if (cc < n)
    689       1.5  augustss 		n = cc;
    690       1.9  augustss 	mb->used += cc;
    691       1.9  augustss 	memcpy(mb->inp, buf, n);
    692       1.5  augustss 	mb->inp += n;
    693       1.5  augustss 	if (mb->inp >= mb->end) {
    694       1.5  augustss 		mb->inp = mb->start;
    695       1.5  augustss 		cc -= n;
    696       1.5  augustss 		if (cc > 0) {
    697       1.5  augustss 			memcpy(mb->inp, buf + n, cc);
    698       1.5  augustss 			mb->inp += cc;
    699       1.5  augustss 		}
    700       1.5  augustss 	}
    701       1.5  augustss 	splx(s);
    702       1.5  augustss 	return (midi_start_output(sc, 0));
    703       1.1  augustss }
    704       1.1  augustss 
    705       1.1  augustss int
    706       1.1  augustss midiioctl(dev, cmd, addr, flag, p)
    707       1.1  augustss 	dev_t dev;
    708       1.1  augustss 	u_long cmd;
    709       1.1  augustss 	caddr_t addr;
    710       1.1  augustss 	int flag;
    711       1.1  augustss 	struct proc *p;
    712       1.1  augustss {
    713       1.1  augustss 	int unit = MIDIUNIT(dev);
    714       1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    715       1.1  augustss 	struct midi_hw_if *hw = sc->hw_if;
    716       1.1  augustss 	int error;
    717       1.1  augustss 
    718       1.1  augustss 	DPRINTF(("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
    719  1.12.2.3    bouyer 
    720  1.12.2.3    bouyer 	if (sc->dying)
    721  1.12.2.3    bouyer 		return EIO;
    722  1.12.2.3    bouyer 
    723       1.1  augustss 	error = 0;
    724       1.1  augustss 	switch (cmd) {
    725       1.1  augustss 	case FIONBIO:
    726       1.1  augustss 		/* All handled in the upper FS layer. */
    727       1.1  augustss 		break;
    728       1.1  augustss 
    729       1.1  augustss 	case FIOASYNC:
    730       1.1  augustss 		if (*(int *)addr) {
    731       1.1  augustss 			if (sc->async)
    732       1.1  augustss 				return EBUSY;
    733       1.1  augustss 			sc->async = p;
    734       1.1  augustss 			DPRINTF(("midi_ioctl: FIOASYNC %p\n", p));
    735       1.1  augustss 		} else
    736       1.1  augustss 			sc->async = 0;
    737       1.1  augustss 		break;
    738       1.1  augustss 
    739       1.1  augustss #if 0
    740       1.1  augustss 	case MIDI_PRETIME:
    741       1.1  augustss 		/* XXX OSS
    742       1.1  augustss 		 * This should set up a read timeout, but that's
    743       1.1  augustss 		 * why we have poll(), so there's nothing yet. */
    744       1.1  augustss 		error = EINVAL;
    745       1.1  augustss 		break;
    746       1.1  augustss #endif
    747       1.1  augustss 
    748       1.4  augustss #ifdef MIDI_SAVE
    749       1.4  augustss 	case MIDI_GETSAVE:
    750       1.4  augustss 		error = copyout(&midisave, *(void **)addr, sizeof midisave);
    751       1.4  augustss   		break;
    752       1.4  augustss #endif
    753       1.4  augustss 
    754       1.1  augustss 	default:
    755       1.1  augustss 		if (hw->ioctl)
    756       1.2  augustss 			error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, p);
    757       1.1  augustss 		else
    758       1.1  augustss 			error = EINVAL;
    759       1.1  augustss 		break;
    760       1.1  augustss 	}
    761       1.1  augustss 	return error;
    762       1.1  augustss }
    763       1.1  augustss 
    764       1.1  augustss int
    765       1.1  augustss midipoll(dev, events, p)
    766       1.1  augustss 	dev_t dev;
    767       1.1  augustss 	int events;
    768       1.1  augustss 	struct proc *p;
    769       1.1  augustss {
    770       1.1  augustss 	int unit = MIDIUNIT(dev);
    771       1.1  augustss 	struct midi_softc *sc = midi_cd.cd_devs[unit];
    772       1.1  augustss 	int revents = 0;
    773       1.1  augustss 	int s = splaudio();
    774       1.1  augustss 
    775       1.1  augustss 	DPRINTF(("midipoll: %p events=0x%x\n", sc, events));
    776       1.1  augustss 
    777  1.12.2.3    bouyer 	if (sc->dying)
    778  1.12.2.3    bouyer 		return EIO;
    779  1.12.2.3    bouyer 
    780       1.1  augustss 	if (events & (POLLIN | POLLRDNORM))
    781       1.1  augustss 		if (sc->inbuf.used > 0)
    782       1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    783       1.1  augustss 
    784       1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    785       1.1  augustss 		if (sc->outbuf.used < sc->outbuf.usedhigh)
    786       1.1  augustss 			revents |= events & (POLLOUT | POLLWRNORM);
    787       1.1  augustss 
    788       1.1  augustss 	if (revents == 0) {
    789       1.1  augustss 		if (events & (POLLIN | POLLRDNORM))
    790       1.1  augustss 			selrecord(p, &sc->rsel);
    791       1.1  augustss 
    792       1.1  augustss 		if (events & (POLLOUT | POLLWRNORM))
    793       1.1  augustss 			selrecord(p, &sc->wsel);
    794       1.1  augustss 	}
    795       1.1  augustss 
    796       1.1  augustss 	splx(s);
    797       1.1  augustss 	return revents;
    798       1.1  augustss }
    799       1.1  augustss 
    800       1.1  augustss void
    801       1.1  augustss midi_getinfo(dev, mi)
    802       1.1  augustss 	dev_t dev;
    803       1.1  augustss 	struct midi_info *mi;
    804       1.1  augustss {
    805       1.1  augustss 	struct midi_softc *sc;
    806       1.1  augustss 
    807  1.12.2.2    bouyer 	sc = device_lookup(&midi_cd, MIDIUNIT(dev));
    808  1.12.2.2    bouyer 	if (sc == NULL)
    809  1.12.2.3    bouyer 		return;
    810  1.12.2.3    bouyer 	if (sc->dying)
    811       1.1  augustss 		return;
    812  1.12.2.2    bouyer 
    813       1.1  augustss 	sc->hw_if->getinfo(sc->hw_hdl, mi);
    814       1.4  augustss }
    815       1.4  augustss 
    816      1.10  drochner #endif /* NMIDI > 0 */
    817      1.10  drochner 
    818      1.10  drochner #if NMIDI > 0 || NMIDIBUS > 0
    819      1.10  drochner 
    820       1.4  augustss int	audioprint __P((void *, const char *));
    821       1.4  augustss 
    822      1.12  augustss struct device *
    823       1.4  augustss midi_attach_mi(mhwp, hdlp, dev)
    824       1.4  augustss 	struct midi_hw_if *mhwp;
    825       1.4  augustss 	void *hdlp;
    826       1.4  augustss 	struct device *dev;
    827       1.4  augustss {
    828       1.4  augustss 	struct audio_attach_args arg;
    829       1.4  augustss 
    830       1.4  augustss #ifdef DIAGNOSTIC
    831       1.4  augustss 	if (mhwp == NULL) {
    832       1.4  augustss 		printf("midi_attach_mi: NULL\n");
    833      1.12  augustss 		return (0);
    834       1.4  augustss 	}
    835       1.4  augustss #endif
    836       1.4  augustss 	arg.type = AUDIODEV_TYPE_MIDI;
    837       1.4  augustss 	arg.hwif = mhwp;
    838       1.4  augustss 	arg.hdl = hdlp;
    839      1.12  augustss 	return (config_found(dev, &arg, audioprint));
    840       1.1  augustss }
    841       1.1  augustss 
    842      1.10  drochner #endif /* NMIDI > 0 || NMIDIBUS > 0 */
    843