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