proto_bus_pci.c revision 265055
1265055Smarcel/*- 2265055Smarcel * Copyright (c) 2014 Marcel Moolenaar 3265055Smarcel * All rights reserved. 4265055Smarcel * 5265055Smarcel * Redistribution and use in source and binary forms, with or without 6265055Smarcel * modification, are permitted provided that the following conditions 7265055Smarcel * are met: 8265055Smarcel * 1. Redistributions of source code must retain the above copyright 9265055Smarcel * notice, this list of conditions and the following disclaimer. 10265055Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11265055Smarcel * notice, this list of conditions and the following disclaimer in the 12265055Smarcel * documentation and/or other materials provided with the distribution. 13265055Smarcel * 14265055Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15265055Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16265055Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17265055Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18265055Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19265055Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20265055Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21265055Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22265055Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23265055Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24265055Smarcel */ 25265055Smarcel 26265055Smarcel#include <sys/cdefs.h> 27265055Smarcel__FBSDID("$FreeBSD: head/sys/dev/proto/proto_bus_pci.c 265055 2014-04-28 17:58:40Z marcel $"); 28265055Smarcel 29265055Smarcel#include <sys/param.h> 30265055Smarcel#include <sys/systm.h> 31265055Smarcel#include <sys/bus.h> 32265055Smarcel#include <sys/conf.h> 33265055Smarcel#include <sys/kernel.h> 34265055Smarcel#include <sys/module.h> 35265055Smarcel#include <machine/bus.h> 36265055Smarcel#include <sys/rman.h> 37265055Smarcel#include <machine/resource.h> 38265055Smarcel#include <sys/sbuf.h> 39265055Smarcel 40265055Smarcel#include <dev/pci/pcireg.h> 41265055Smarcel#include <dev/pci/pcivar.h> 42265055Smarcel 43265055Smarcel#include <dev/proto/proto.h> 44265055Smarcel 45265055Smarcelstatic int proto_pci_probe(device_t dev); 46265055Smarcelstatic int proto_pci_attach(device_t dev); 47265055Smarcel 48265055Smarcelstatic device_method_t proto_pci_methods[] = { 49265055Smarcel /* Device interface */ 50265055Smarcel DEVMETHOD(device_probe, proto_pci_probe), 51265055Smarcel DEVMETHOD(device_attach, proto_pci_attach), 52265055Smarcel DEVMETHOD(device_detach, proto_detach), 53265055Smarcel DEVMETHOD_END 54265055Smarcel}; 55265055Smarcel 56265055Smarcelstatic driver_t proto_pci_driver = { 57265055Smarcel proto_driver_name, 58265055Smarcel proto_pci_methods, 59265055Smarcel sizeof(struct proto_softc), 60265055Smarcel}; 61265055Smarcel 62265055Smarcelstatic int 63265055Smarcelproto_pci_probe(device_t dev) 64265055Smarcel{ 65265055Smarcel struct sbuf *sb; 66265055Smarcel 67265055Smarcel /* For now we only attach to function 0 devices. */ 68265055Smarcel if (pci_get_function(dev) != 0) 69265055Smarcel return (ENXIO); 70265055Smarcel 71265055Smarcel sb = sbuf_new_auto(); 72265055Smarcel sbuf_printf(sb, "pci%d:%d:%d:%d", pci_get_domain(dev), 73265055Smarcel pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)); 74265055Smarcel sbuf_finish(sb); 75265055Smarcel device_set_desc_copy(dev, sbuf_data(sb)); 76265055Smarcel sbuf_delete(sb); 77265055Smarcel return (BUS_PROBE_HOOVER); 78265055Smarcel} 79265055Smarcel 80265055Smarcelstatic int 81265055Smarcelproto_pci_attach(device_t dev) 82265055Smarcel{ 83265055Smarcel struct proto_softc *sc; 84265055Smarcel struct resource *res; 85265055Smarcel int bar, rid, type; 86265055Smarcel 87265055Smarcel sc = device_get_softc(dev); 88265055Smarcel 89265055Smarcel proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL); 90265055Smarcel 91265055Smarcel for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) { 92265055Smarcel rid = PCIR_BAR(bar); 93265055Smarcel type = SYS_RES_MEMORY; 94265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE); 95265055Smarcel if (res == NULL) { 96265055Smarcel type = SYS_RES_IOPORT; 97265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, 98265055Smarcel RF_ACTIVE); 99265055Smarcel } 100265055Smarcel if (res != NULL) 101265055Smarcel proto_add_resource(sc, type, rid, res); 102265055Smarcel } 103265055Smarcel 104265055Smarcel rid = 0; 105265055Smarcel type = SYS_RES_IRQ; 106265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE); 107265055Smarcel if (res != NULL) 108265055Smarcel proto_add_resource(sc, type, rid, res); 109265055Smarcel return (proto_attach(dev)); 110265055Smarcel} 111265055Smarcel 112265055SmarcelDRIVER_MODULE(proto, pci, proto_pci_driver, proto_devclass, NULL, NULL); 113