#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: joy_pci.c,v 1.21 2014/05/07 19:30:09 joerg Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/ic/joyvar.h>
struct joy_pci_softc {
struct joy_softc sc_joy;
kmutex_t sc_lock;
};
static int bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg);
static int
joy_pci_match(device_t parent, cfdata_t match, void *aux)
{
struct pci_attach_args *pa = aux;
if (PCI_CLASS(pa->pa_class) == PCI_CLASS_INPUT &&
PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_INPUT_GAMEPORT &&
PCI_INTERFACE(pa->pa_class) == 0x10)
return 1;
if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CREATIVELABS &&
(PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY ||
PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY2))
return 1;
return 0;
}
static int
bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
pcireg_t address, mask;
address = pci_conf_read(pc, tag, reg);
pci_conf_write(pc, tag, reg, 0xffffffff);
mask = pci_conf_read(pc, tag, reg);
pci_conf_write(pc, tag, reg, address);
return (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO &&
PCI_MAPREG_IO_SIZE(mask) > 0);
}
static void
joy_pci_attach(device_t parent, device_t self, void *aux)
{
struct joy_pci_softc *psc = device_private(self);
struct joy_softc *sc = &psc->sc_joy;
struct pci_attach_args *pa = aux;
bus_size_t mapsize;
int reg;
pci_aprint_devinfo(pa, NULL);
for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END;
reg += sizeof(pcireg_t))
if (bar_is_io(pa->pa_pc, pa->pa_tag, reg))
break;
if (reg >= PCI_MAPREG_END) {
aprint_error_dev(self,
"violates PCI spec, no IO region found\n");
return;
}
if (pci_mapreg_map(pa, reg, PCI_MAPREG_TYPE_IO, 0,
&sc->sc_iot, &sc->sc_ioh, NULL, &mapsize)) {
aprint_error_dev(self, "could not map IO space\n");
return;
}
if (mapsize != 2) {
if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 1, 1,
&sc->sc_ioh)) {
aprint_error_dev(self, "error mapping subregion\n");
return;
}
}
mutex_init(&psc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
sc->sc_dev = self;
sc->sc_lock = &psc->sc_lock;
joyattach(sc);
}
CFATTACH_DECL_NEW(joy_pci, sizeof(struct joy_pci_softc),
joy_pci_match, joy_pci_attach, NULL, NULL);