#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <sys/audioio.h>
#include <dev/midi_if.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>
#include <dev/ic/mpuvar.h>
int mpu_isa_match(struct device *, void *, void *);
void mpu_isa_attach(struct device *, struct device *, void *);
int mpu_test(bus_space_tag_t, int);
#ifdef AUDIO_DEBUG
#define DPRINTF(x) if (mpu_debug) printf x
int mpu_debug = 0;
#else
#define DPRINTF(x)
#endif
#define MPU_GETSTATUS(iot, ioh) (bus_space_read_1(iot, ioh, MPU_STATUS))
struct mpu_isa_softc {
struct device sc_dev;
struct mpu_softc sc_mpu;
};
const struct cfattach mpu_isa_ca = {
sizeof(struct mpu_isa_softc), mpu_isa_match, mpu_isa_attach
};
int
mpu_test(bus_space_tag_t iot, int iobase)
{
bus_space_handle_t ioh;
int i, rc;
rc = 0;
if (bus_space_map(iot, iobase, MPU401_NPORT, 0, &ioh)) {
DPRINTF(("mpu_test: can't map: %x/2\n", iobase));
return (0);
}
DPRINTF(("mpu_test: trying: %x\n", iobase));
if (MPU_GETSTATUS(iot, ioh) == 0xff)
goto done;
for (i = 0; i < MPU_MAXWAIT; i++) {
if (!(MPU_GETSTATUS(iot, ioh) & MPU_OUTPUT_BUSY)) {
rc = 1;
break;
}
delay (10);
}
if (rc == 1) {
bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
rc = 0;
for (i = 0; i < 2 * MPU_MAXWAIT; i++)
if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
rc = 1;
break;
}
}
done:
bus_space_unmap(iot, ioh, MPU401_NPORT);
return (rc);
}
int
mpu_isa_match(struct device *parent, void *match, void *aux)
{
struct isa_attach_args *ia = aux;
if (mpu_test(ia->ia_iot, ia->ia_iobase)) {
ia->ia_iosize = MPU401_NPORT;
return (1);
}
return (0);
}
void
mpu_isa_attach(struct device *parent, struct device *self, void *aux)
{
struct mpu_isa_softc *sc = (struct mpu_isa_softc *)self;
struct isa_attach_args *ia = aux;
sc->sc_mpu.iot = ia->ia_iot;
if (bus_space_map (ia->ia_iot, ia->ia_iobase, MPU401_NPORT,
0, &sc->sc_mpu.ioh)) {
printf(": can't map i/o space\n");
return;
}
if (!mpu_find(&sc->sc_mpu)) {
printf(": find failed\n");
return;
}
printf(": generic MPU-401 compatible\n");
midi_attach_mi(&mpu_midi_hw_if, &sc->sc_mpu, &sc->sc_dev);
}