#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/proto/proto.h>
static int proto_pci_probe(device_t dev);
static int proto_pci_attach(device_t dev);
static device_method_t proto_pci_methods[] = {
DEVMETHOD(device_probe, proto_pci_probe),
DEVMETHOD(device_attach, proto_pci_attach),
DEVMETHOD(device_detach, proto_detach),
DEVMETHOD_END
};
static driver_t proto_pci_driver = {
proto_driver_name,
proto_pci_methods,
sizeof(struct proto_softc),
};
static char proto_pci_prefix[] = "pci";
static char **proto_pci_devnames;
static int
proto_pci_probe(device_t dev)
{
if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
return (ENXIO);
device_set_descf(dev, "%s%d:%d:%d:%d", proto_pci_prefix,
pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
pci_get_function(dev));
return (proto_probe(dev, proto_pci_prefix, &proto_pci_devnames));
}
static int
proto_pci_attach(device_t dev)
{
struct proto_softc *sc;
struct resource *res;
uint32_t val;
int bar, rid, type;
sc = device_get_softc(dev);
proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
rid = PCIR_BAR(bar);
val = pci_read_config(dev, rid, 4);
type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY;
res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
if (res == NULL)
continue;
proto_add_resource(sc, type, rid, res);
if (type == SYS_RES_IOPORT)
continue;
if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64)
bar++;
}
rid = 0;
type = SYS_RES_IRQ;
res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
if (res != NULL)
proto_add_resource(sc, type, rid, res);
return (proto_attach(dev));
}
DRIVER_MODULE(proto, pci, proto_pci_driver, NULL, NULL);