1148ed571SAdrian Chadd /*- 28d14ca9cSLandon J. Fuller * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38d14ca9cSLandon J. Fuller * 4111d7cb2SLandon J. Fuller * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org> 5148ed571SAdrian Chadd * All rights reserved. 6148ed571SAdrian Chadd * 7148ed571SAdrian Chadd * Redistribution and use in source and binary forms, with or without 8148ed571SAdrian Chadd * modification, are permitted provided that the following conditions 9148ed571SAdrian Chadd * are met: 10148ed571SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 118d14ca9cSLandon J. Fuller * notice, this list of conditions and the following disclaimer. 128d14ca9cSLandon J. Fuller * 2. Redistributions in binary form must reproduce the above copyright 138d14ca9cSLandon J. Fuller * notice, this list of conditions and the following disclaimer in the 148d14ca9cSLandon J. Fuller * documentation and/or other materials provided with the distribution. 15148ed571SAdrian Chadd * 168d14ca9cSLandon J. Fuller * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 178d14ca9cSLandon J. Fuller * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188d14ca9cSLandon J. Fuller * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198d14ca9cSLandon J. Fuller * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 208d14ca9cSLandon J. Fuller * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218d14ca9cSLandon J. Fuller * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228d14ca9cSLandon J. Fuller * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238d14ca9cSLandon J. Fuller * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248d14ca9cSLandon J. Fuller * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258d14ca9cSLandon J. Fuller * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268d14ca9cSLandon J. Fuller * SUCH DAMAGE. 27148ed571SAdrian Chadd */ 28148ed571SAdrian Chadd 29148ed571SAdrian Chadd #include <sys/cdefs.h> 30148ed571SAdrian Chadd __FBSDID("$FreeBSD$"); 31148ed571SAdrian Chadd 325025b8d5SAdrian Chadd #include "opt_bwn.h" 335025b8d5SAdrian Chadd #include "opt_wlan.h" 345025b8d5SAdrian Chadd 35148ed571SAdrian Chadd #include <sys/param.h> 36148ed571SAdrian Chadd #include <sys/kernel.h> 37148ed571SAdrian Chadd #include <sys/bus.h> 38148ed571SAdrian Chadd #include <sys/module.h> 39148ed571SAdrian Chadd 40148ed571SAdrian Chadd #include <dev/pci/pcireg.h> 41148ed571SAdrian Chadd #include <dev/pci/pcivar.h> 42148ed571SAdrian Chadd 43148ed571SAdrian Chadd #include <dev/bhnd/bhndb/bhndb_pcivar.h> 44111d7cb2SLandon J. Fuller #include <dev/bhnd/bhndb/bhndb_hwdata.h> 45148ed571SAdrian Chadd #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h> 46148ed571SAdrian Chadd 47148ed571SAdrian Chadd #include <dev/bhnd/bhnd_ids.h> 48148ed571SAdrian Chadd 49148ed571SAdrian Chadd #include "bhndb_bus_if.h" 50148ed571SAdrian Chadd 51148ed571SAdrian Chadd #include "if_bwn_pcivar.h" 52148ed571SAdrian Chadd 53148ed571SAdrian Chadd /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */ 54148ed571SAdrian Chadd static int attach_untested = 0; 55148ed571SAdrian Chadd TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested); 56148ed571SAdrian Chadd 57148ed571SAdrian Chadd /* SIBA Devices */ 58148ed571SAdrian Chadd static const struct bwn_pci_device siba_devices[] = { 59148ed571SAdrian Chadd BWN_BCM_DEV(BCM4306_D11A, "BCM4306 802.11a", 60a225321fSLandon J. Fuller BWN_QUIRK_WLAN_DUALCORE|BWN_QUIRK_SOFTMODEM_UNPOPULATED), 61a225321fSLandon J. Fuller BWN_BCM_DEV(BCM4306_D11G, "BCM4306 802.11b/g", 62a225321fSLandon J. Fuller BWN_QUIRK_SOFTMODEM_UNPOPULATED), 63a225321fSLandon J. Fuller BWN_BCM_DEV(BCM4306_D11G_ID2, "BCM4306 802.11b/g", 64a225321fSLandon J. Fuller BWN_QUIRK_SOFTMODEM_UNPOPULATED), 65a225321fSLandon J. Fuller BWN_BCM_DEV(BCM4306_D11DUAL, "BCM4306 802.11a/b/g", 66a225321fSLandon J. Fuller BWN_QUIRK_SOFTMODEM_UNPOPULATED), 67148ed571SAdrian Chadd BWN_BCM_DEV(BCM4307, "BCM4307 802.11b", 0), 68148ed571SAdrian Chadd 69148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11G, "BCM4311 802.11b/g", 0), 70148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11DUAL, "BCM4311 802.11a/b/g", 0), 71148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11A, "BCM4311 802.11a", 72148ed571SAdrian Chadd BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE), 73148ed571SAdrian Chadd 74148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11G, "BCM4318 802.11b/g", 0), 75148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11DUAL, "BCM4318 802.11a/b/g", 0), 76148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11A, "BCM4318 802.11a", 77148ed571SAdrian Chadd BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE), 78148ed571SAdrian Chadd 797d616280SLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N, "BCM4321 802.11n Dual-Band", 807d616280SLandon J. Fuller BWN_QUIRK_USBH_UNPOPULATED), 817d616280SLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N2G, "BCM4321 802.11n 2GHz", 827d616280SLandon J. Fuller BWN_QUIRK_USBH_UNPOPULATED), 838d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N5G, "BCM4321 802.11n 5GHz", 847d616280SLandon J. Fuller BWN_QUIRK_UNTESTED|BWN_QUIRK_USBH_UNPOPULATED), 85148ed571SAdrian Chadd 86148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N, "BCM4322 802.11n Dual-Band", 0), 87148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N2G, "BCM4322 802.11n 2GHz", 88148ed571SAdrian Chadd BWN_QUIRK_UNTESTED), 89148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N5G, "BCM4322 802.11n 5GHz", 90148ed571SAdrian Chadd BWN_QUIRK_UNTESTED), 91148ed571SAdrian Chadd 92148ed571SAdrian Chadd BWN_BCM_DEV(BCM4328_D11G, "BCM4328/4312 802.11g", 0), 93148ed571SAdrian Chadd { 0, 0, NULL, 0 } 94148ed571SAdrian Chadd }; 95148ed571SAdrian Chadd 96148ed571SAdrian Chadd /** BCMA Devices */ 97148ed571SAdrian Chadd static const struct bwn_pci_device bcma_devices[] = { 98148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N, "BCM4331 802.11n Dual-Band", 0), 99148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N2G, "BCM4331 802.11n 2GHz", 0), 100148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N5G, "BCM4331 802.11n 5GHz", 0), 1018d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N, "BCM43224 802.11n Dual-Band", 0), 1020bffd217SLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0), 103972459a6SAdrian Chadd BWN_BCM_DEV(BCM43225_D11N2G, "BCM43225 802.11n 2GHz", 0), 104148ed571SAdrian Chadd { 0, 0, NULL, 0} 105148ed571SAdrian Chadd }; 106148ed571SAdrian Chadd 107148ed571SAdrian Chadd /** Device configuration table */ 108148ed571SAdrian Chadd static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = { 109148ed571SAdrian Chadd /* SIBA devices */ 110148ed571SAdrian Chadd { 111148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_siba_generic_hwcfg, 112148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 113111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_siba_priority_table, 114148ed571SAdrian Chadd .devices = siba_devices 115148ed571SAdrian Chadd }, 116148ed571SAdrian Chadd /* BCMA devices */ 117148ed571SAdrian Chadd { 118148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_bcma_generic_hwcfg, 119148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 120111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_bcma_priority_table, 121148ed571SAdrian Chadd .devices = bcma_devices 122148ed571SAdrian Chadd }, 123148ed571SAdrian Chadd { NULL, NULL, NULL } 124148ed571SAdrian Chadd }; 125148ed571SAdrian Chadd 126148ed571SAdrian Chadd /** Search the device configuration table for an entry matching @p dev. */ 127148ed571SAdrian Chadd static int 128148ed571SAdrian Chadd bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg, 129148ed571SAdrian Chadd const struct bwn_pci_device **device) 130148ed571SAdrian Chadd { 131148ed571SAdrian Chadd const struct bwn_pci_devcfg *dvc; 132148ed571SAdrian Chadd const struct bwn_pci_device *dv; 133148ed571SAdrian Chadd 134148ed571SAdrian Chadd for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) { 135148ed571SAdrian Chadd for (dv = dvc->devices; dv->device != 0; dv++) { 136148ed571SAdrian Chadd if (pci_get_vendor(dev) == dv->vendor && 137148ed571SAdrian Chadd pci_get_device(dev) == dv->device) 138148ed571SAdrian Chadd { 139148ed571SAdrian Chadd if (cfg != NULL) 140148ed571SAdrian Chadd *cfg = dvc; 141148ed571SAdrian Chadd 142148ed571SAdrian Chadd if (device != NULL) 143148ed571SAdrian Chadd *device = dv; 144148ed571SAdrian Chadd 145148ed571SAdrian Chadd return (0); 146148ed571SAdrian Chadd } 147148ed571SAdrian Chadd } 148148ed571SAdrian Chadd } 149148ed571SAdrian Chadd 150148ed571SAdrian Chadd return (ENOENT); 151148ed571SAdrian Chadd } 152148ed571SAdrian Chadd 153148ed571SAdrian Chadd static int 154148ed571SAdrian Chadd bwn_pci_probe(device_t dev) 155148ed571SAdrian Chadd { 156148ed571SAdrian Chadd const struct bwn_pci_device *ident; 157148ed571SAdrian Chadd 158148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, NULL, &ident)) 159148ed571SAdrian Chadd return (ENXIO); 160148ed571SAdrian Chadd 161148ed571SAdrian Chadd /* Skip untested devices */ 162148ed571SAdrian Chadd if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested) 163148ed571SAdrian Chadd return (ENXIO); 164148ed571SAdrian Chadd 165148ed571SAdrian Chadd device_set_desc(dev, ident->desc); 166d177c199SLandon J. Fuller return (BUS_PROBE_DEFAULT); 167148ed571SAdrian Chadd } 168148ed571SAdrian Chadd 169148ed571SAdrian Chadd static int 170148ed571SAdrian Chadd bwn_pci_attach(device_t dev) 171148ed571SAdrian Chadd { 172148ed571SAdrian Chadd struct bwn_pci_softc *sc; 173148ed571SAdrian Chadd const struct bwn_pci_device *ident; 174148ed571SAdrian Chadd int error; 175148ed571SAdrian Chadd 176148ed571SAdrian Chadd sc = device_get_softc(dev); 177148ed571SAdrian Chadd sc->dev = dev; 178148ed571SAdrian Chadd 179148ed571SAdrian Chadd /* Find our hardware config */ 180148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident)) 181148ed571SAdrian Chadd return (ENXIO); 182148ed571SAdrian Chadd 183148ed571SAdrian Chadd /* Save quirk flags */ 184148ed571SAdrian Chadd sc->quirks = ident->quirks; 185148ed571SAdrian Chadd 186148ed571SAdrian Chadd /* Attach bridge device */ 187148ed571SAdrian Chadd if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1))) 188148ed571SAdrian Chadd return (ENXIO); 189148ed571SAdrian Chadd 190148ed571SAdrian Chadd /* Success */ 191148ed571SAdrian Chadd return (0); 192148ed571SAdrian Chadd } 193148ed571SAdrian Chadd 194148ed571SAdrian Chadd static int 195148ed571SAdrian Chadd bwn_pci_detach(device_t dev) 196148ed571SAdrian Chadd { 1978d14ca9cSLandon J. Fuller int error; 1988d14ca9cSLandon J. Fuller 1998d14ca9cSLandon J. Fuller if ((error = bus_generic_detach(dev))) 2008d14ca9cSLandon J. Fuller return (error); 2018d14ca9cSLandon J. Fuller 2028d14ca9cSLandon J. Fuller return (device_delete_children(dev)); 203148ed571SAdrian Chadd } 204148ed571SAdrian Chadd 205148ed571SAdrian Chadd static void 206148ed571SAdrian Chadd bwn_pci_probe_nomatch(device_t dev, device_t child) 207148ed571SAdrian Chadd { 208148ed571SAdrian Chadd const char *name; 209148ed571SAdrian Chadd 210148ed571SAdrian Chadd name = device_get_name(child); 211148ed571SAdrian Chadd if (name == NULL) 212148ed571SAdrian Chadd name = "unknown device"; 213148ed571SAdrian Chadd 214148ed571SAdrian Chadd device_printf(dev, "<%s> (no driver attached)\n", name); 215148ed571SAdrian Chadd } 216148ed571SAdrian Chadd 217148ed571SAdrian Chadd static const struct bhndb_hwcfg * 218148ed571SAdrian Chadd bwn_pci_get_generic_hwcfg(device_t dev, device_t child) 219148ed571SAdrian Chadd { 220148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 221148ed571SAdrian Chadd return (sc->devcfg->bridge_hwcfg); 222148ed571SAdrian Chadd } 223148ed571SAdrian Chadd 224148ed571SAdrian Chadd static const struct bhndb_hw * 225148ed571SAdrian Chadd bwn_pci_get_bhndb_hwtable(device_t dev, device_t child) 226148ed571SAdrian Chadd { 227148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 228148ed571SAdrian Chadd return (sc->devcfg->bridge_hwtable); 229148ed571SAdrian Chadd } 230148ed571SAdrian Chadd 231111d7cb2SLandon J. Fuller static const struct bhndb_hw_priority * 232111d7cb2SLandon J. Fuller bwn_pci_get_bhndb_hwprio(device_t dev, device_t child) 233111d7cb2SLandon J. Fuller { 234111d7cb2SLandon J. Fuller struct bwn_pci_softc *sc = device_get_softc(dev); 235111d7cb2SLandon J. Fuller return (sc->devcfg->bridge_hwprio); 236111d7cb2SLandon J. Fuller } 237111d7cb2SLandon J. Fuller 238148ed571SAdrian Chadd static bool 239148ed571SAdrian Chadd bwn_pci_is_core_disabled(device_t dev, device_t child, 240148ed571SAdrian Chadd struct bhnd_core_info *core) 241148ed571SAdrian Chadd { 242148ed571SAdrian Chadd struct bwn_pci_softc *sc; 243148ed571SAdrian Chadd 244148ed571SAdrian Chadd sc = device_get_softc(dev); 245148ed571SAdrian Chadd 246148ed571SAdrian Chadd switch (bhnd_core_class(core)) { 247148ed571SAdrian Chadd case BHND_DEVCLASS_WLAN: 248148ed571SAdrian Chadd if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE)) 249148ed571SAdrian Chadd return (true); 250148ed571SAdrian Chadd 251148ed571SAdrian Chadd return (false); 252148ed571SAdrian Chadd 253148ed571SAdrian Chadd case BHND_DEVCLASS_ENET: 254148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_MAC: 255148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_PHY: 256148ed571SAdrian Chadd return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0); 257148ed571SAdrian Chadd 2587d616280SLandon J. Fuller case BHND_DEVCLASS_USB_HOST: 2597d616280SLandon J. Fuller return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0); 2607d616280SLandon J. Fuller 261a225321fSLandon J. Fuller case BHND_DEVCLASS_SOFTMODEM: 262a225321fSLandon J. Fuller return ((sc->quirks & BWN_QUIRK_SOFTMODEM_UNPOPULATED) != 0); 263a225321fSLandon J. Fuller 264148ed571SAdrian Chadd default: 265148ed571SAdrian Chadd return (false); 266148ed571SAdrian Chadd } 267148ed571SAdrian Chadd } 268148ed571SAdrian Chadd 269148ed571SAdrian Chadd static device_method_t bwn_pci_methods[] = { 270148ed571SAdrian Chadd /* Device interface */ 271148ed571SAdrian Chadd DEVMETHOD(device_probe, bwn_pci_probe), 272148ed571SAdrian Chadd DEVMETHOD(device_attach, bwn_pci_attach), 273148ed571SAdrian Chadd DEVMETHOD(device_detach, bwn_pci_detach), 274148ed571SAdrian Chadd DEVMETHOD(device_shutdown, bus_generic_shutdown), 275148ed571SAdrian Chadd DEVMETHOD(device_suspend, bus_generic_suspend), 276148ed571SAdrian Chadd DEVMETHOD(device_resume, bus_generic_resume), 277148ed571SAdrian Chadd 278148ed571SAdrian Chadd /* Bus interface */ 279148ed571SAdrian Chadd DEVMETHOD(bus_probe_nomatch, bwn_pci_probe_nomatch), 280148ed571SAdrian Chadd 281148ed571SAdrian Chadd /* BHNDB_BUS Interface */ 282148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_generic_hwcfg, bwn_pci_get_generic_hwcfg), 283148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_hardware_table, bwn_pci_get_bhndb_hwtable), 284111d7cb2SLandon J. Fuller DEVMETHOD(bhndb_bus_get_hardware_prio, bwn_pci_get_bhndb_hwprio), 285148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_is_core_disabled, bwn_pci_is_core_disabled), 286148ed571SAdrian Chadd 287148ed571SAdrian Chadd DEVMETHOD_END 288148ed571SAdrian Chadd }; 289148ed571SAdrian Chadd 2908d14ca9cSLandon J. Fuller DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, 2918d14ca9cSLandon J. Fuller sizeof(struct bwn_pci_softc)); 292*b338e3eaSJohn Baldwin DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, NULL, NULL, SI_ORDER_ANY); 29396b52361SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_siba, 294329e817fSWarner Losh siba_devices, nitems(siba_devices) - 1); 29596b52361SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_bcma, 296329e817fSWarner Losh bcma_devices, nitems(bcma_devices) - 1); 29705bdf34aSJohn Baldwin DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, NULL, NULL); 298148ed571SAdrian Chadd 299d177c199SLandon J. Fuller MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1); 3008d14ca9cSLandon J. Fuller MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1); 301148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1); 302148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1); 303148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1); 304148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1); 30519a63eb5SLandon J. Fuller MODULE_VERSION(bwn_pci, 1); 306