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 94148ed571SAdrian Chadd { 0, 0, NULL, 0 } 95148ed571SAdrian Chadd }; 96148ed571SAdrian Chadd 97148ed571SAdrian Chadd /** BCMA Devices */ 98148ed571SAdrian Chadd static const struct bwn_pci_device bcma_devices[] = { 99148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N, "BCM4331 802.11n Dual-Band", 0), 100148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N2G, "BCM4331 802.11n 2GHz", 0), 101148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N5G, "BCM4331 802.11n 5GHz", 0), 1028d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N, "BCM43224 802.11n Dual-Band", 0), 1030bffd217SLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0), 104972459a6SAdrian Chadd BWN_BCM_DEV(BCM43225_D11N2G, "BCM43225 802.11n 2GHz", 0), 105148ed571SAdrian Chadd 106148ed571SAdrian Chadd { 0, 0, NULL, 0} 107148ed571SAdrian Chadd }; 108148ed571SAdrian Chadd 109148ed571SAdrian Chadd /** Device configuration table */ 110148ed571SAdrian Chadd static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = { 111148ed571SAdrian Chadd /* SIBA devices */ 112148ed571SAdrian Chadd { 113148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_siba_generic_hwcfg, 114148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 115111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_siba_priority_table, 116148ed571SAdrian Chadd .devices = siba_devices 117148ed571SAdrian Chadd }, 118148ed571SAdrian Chadd /* BCMA devices */ 119148ed571SAdrian Chadd { 120148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_bcma_generic_hwcfg, 121148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 122111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_bcma_priority_table, 123148ed571SAdrian Chadd .devices = bcma_devices 124148ed571SAdrian Chadd }, 125148ed571SAdrian Chadd { NULL, NULL, NULL } 126148ed571SAdrian Chadd }; 127148ed571SAdrian Chadd 128148ed571SAdrian Chadd /** Search the device configuration table for an entry matching @p dev. */ 129148ed571SAdrian Chadd static int 130148ed571SAdrian Chadd bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg, 131148ed571SAdrian Chadd const struct bwn_pci_device **device) 132148ed571SAdrian Chadd { 133148ed571SAdrian Chadd const struct bwn_pci_devcfg *dvc; 134148ed571SAdrian Chadd const struct bwn_pci_device *dv; 135148ed571SAdrian Chadd 136148ed571SAdrian Chadd for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) { 137148ed571SAdrian Chadd for (dv = dvc->devices; dv->device != 0; dv++) { 138148ed571SAdrian Chadd if (pci_get_vendor(dev) == dv->vendor && 139148ed571SAdrian Chadd pci_get_device(dev) == dv->device) 140148ed571SAdrian Chadd { 141148ed571SAdrian Chadd if (cfg != NULL) 142148ed571SAdrian Chadd *cfg = dvc; 143148ed571SAdrian Chadd 144148ed571SAdrian Chadd if (device != NULL) 145148ed571SAdrian Chadd *device = dv; 146148ed571SAdrian Chadd 147148ed571SAdrian Chadd return (0); 148148ed571SAdrian Chadd } 149148ed571SAdrian Chadd } 150148ed571SAdrian Chadd } 151148ed571SAdrian Chadd 152148ed571SAdrian Chadd return (ENOENT); 153148ed571SAdrian Chadd } 154148ed571SAdrian Chadd 155148ed571SAdrian Chadd static int 156148ed571SAdrian Chadd bwn_pci_probe(device_t dev) 157148ed571SAdrian Chadd { 158148ed571SAdrian Chadd const struct bwn_pci_device *ident; 159148ed571SAdrian Chadd 160148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, NULL, &ident)) 161148ed571SAdrian Chadd return (ENXIO); 162148ed571SAdrian Chadd 163148ed571SAdrian Chadd /* Skip untested devices */ 164148ed571SAdrian Chadd if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested) 165148ed571SAdrian Chadd return (ENXIO); 166148ed571SAdrian Chadd 167148ed571SAdrian Chadd device_set_desc(dev, ident->desc); 168d177c199SLandon J. Fuller return (BUS_PROBE_DEFAULT); 169148ed571SAdrian Chadd } 170148ed571SAdrian Chadd 171148ed571SAdrian Chadd static int 172148ed571SAdrian Chadd bwn_pci_attach(device_t dev) 173148ed571SAdrian Chadd { 174148ed571SAdrian Chadd struct bwn_pci_softc *sc; 175148ed571SAdrian Chadd const struct bwn_pci_device *ident; 176148ed571SAdrian Chadd int error; 177148ed571SAdrian Chadd 178148ed571SAdrian Chadd sc = device_get_softc(dev); 179148ed571SAdrian Chadd sc->dev = dev; 180148ed571SAdrian Chadd 181148ed571SAdrian Chadd /* Find our hardware config */ 182148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident)) 183148ed571SAdrian Chadd return (ENXIO); 184148ed571SAdrian Chadd 185148ed571SAdrian Chadd /* Save quirk flags */ 186148ed571SAdrian Chadd sc->quirks = ident->quirks; 187148ed571SAdrian Chadd 188148ed571SAdrian Chadd /* Attach bridge device */ 189148ed571SAdrian Chadd if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1))) 190148ed571SAdrian Chadd return (ENXIO); 191148ed571SAdrian Chadd 192148ed571SAdrian Chadd /* Success */ 193148ed571SAdrian Chadd return (0); 194148ed571SAdrian Chadd } 195148ed571SAdrian Chadd 196148ed571SAdrian Chadd static int 197148ed571SAdrian Chadd bwn_pci_detach(device_t dev) 198148ed571SAdrian Chadd { 1998d14ca9cSLandon J. Fuller int error; 2008d14ca9cSLandon J. Fuller 2018d14ca9cSLandon J. Fuller if ((error = bus_generic_detach(dev))) 2028d14ca9cSLandon J. Fuller return (error); 2038d14ca9cSLandon J. Fuller 2048d14ca9cSLandon J. Fuller return (device_delete_children(dev)); 205148ed571SAdrian Chadd } 206148ed571SAdrian Chadd 207148ed571SAdrian Chadd static void 208148ed571SAdrian Chadd bwn_pci_probe_nomatch(device_t dev, device_t child) 209148ed571SAdrian Chadd { 210148ed571SAdrian Chadd const char *name; 211148ed571SAdrian Chadd 212148ed571SAdrian Chadd name = device_get_name(child); 213148ed571SAdrian Chadd if (name == NULL) 214148ed571SAdrian Chadd name = "unknown device"; 215148ed571SAdrian Chadd 216148ed571SAdrian Chadd device_printf(dev, "<%s> (no driver attached)\n", name); 217148ed571SAdrian Chadd } 218148ed571SAdrian Chadd 219148ed571SAdrian Chadd static const struct bhndb_hwcfg * 220148ed571SAdrian Chadd bwn_pci_get_generic_hwcfg(device_t dev, device_t child) 221148ed571SAdrian Chadd { 222148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 223148ed571SAdrian Chadd return (sc->devcfg->bridge_hwcfg); 224148ed571SAdrian Chadd } 225148ed571SAdrian Chadd 226148ed571SAdrian Chadd static const struct bhndb_hw * 227148ed571SAdrian Chadd bwn_pci_get_bhndb_hwtable(device_t dev, device_t child) 228148ed571SAdrian Chadd { 229148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 230148ed571SAdrian Chadd return (sc->devcfg->bridge_hwtable); 231148ed571SAdrian Chadd } 232148ed571SAdrian Chadd 233111d7cb2SLandon J. Fuller static const struct bhndb_hw_priority * 234111d7cb2SLandon J. Fuller bwn_pci_get_bhndb_hwprio(device_t dev, device_t child) 235111d7cb2SLandon J. Fuller { 236111d7cb2SLandon J. Fuller struct bwn_pci_softc *sc = device_get_softc(dev); 237111d7cb2SLandon J. Fuller return (sc->devcfg->bridge_hwprio); 238111d7cb2SLandon J. Fuller } 239111d7cb2SLandon J. Fuller 240148ed571SAdrian Chadd static bool 241148ed571SAdrian Chadd bwn_pci_is_core_disabled(device_t dev, device_t child, 242148ed571SAdrian Chadd struct bhnd_core_info *core) 243148ed571SAdrian Chadd { 244148ed571SAdrian Chadd struct bwn_pci_softc *sc; 245148ed571SAdrian Chadd 246148ed571SAdrian Chadd sc = device_get_softc(dev); 247148ed571SAdrian Chadd 248148ed571SAdrian Chadd switch (bhnd_core_class(core)) { 249148ed571SAdrian Chadd case BHND_DEVCLASS_WLAN: 250148ed571SAdrian Chadd if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE)) 251148ed571SAdrian Chadd return (true); 252148ed571SAdrian Chadd 253148ed571SAdrian Chadd return (false); 254148ed571SAdrian Chadd 255148ed571SAdrian Chadd case BHND_DEVCLASS_ENET: 256148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_MAC: 257148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_PHY: 258148ed571SAdrian Chadd return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0); 259148ed571SAdrian Chadd 2607d616280SLandon J. Fuller case BHND_DEVCLASS_USB_HOST: 2617d616280SLandon J. Fuller return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0); 2627d616280SLandon J. Fuller 263a225321fSLandon J. Fuller case BHND_DEVCLASS_SOFTMODEM: 264a225321fSLandon J. Fuller return ((sc->quirks & BWN_QUIRK_SOFTMODEM_UNPOPULATED) != 0); 265a225321fSLandon J. Fuller 266148ed571SAdrian Chadd default: 267148ed571SAdrian Chadd return (false); 268148ed571SAdrian Chadd } 269148ed571SAdrian Chadd } 270148ed571SAdrian Chadd 271148ed571SAdrian Chadd static device_method_t bwn_pci_methods[] = { 272148ed571SAdrian Chadd /* Device interface */ 273148ed571SAdrian Chadd DEVMETHOD(device_probe, bwn_pci_probe), 274148ed571SAdrian Chadd DEVMETHOD(device_attach, bwn_pci_attach), 275148ed571SAdrian Chadd DEVMETHOD(device_detach, bwn_pci_detach), 276148ed571SAdrian Chadd DEVMETHOD(device_shutdown, bus_generic_shutdown), 277148ed571SAdrian Chadd DEVMETHOD(device_suspend, bus_generic_suspend), 278148ed571SAdrian Chadd DEVMETHOD(device_resume, bus_generic_resume), 279148ed571SAdrian Chadd 280148ed571SAdrian Chadd /* Bus interface */ 281148ed571SAdrian Chadd DEVMETHOD(bus_probe_nomatch, bwn_pci_probe_nomatch), 282148ed571SAdrian Chadd 283148ed571SAdrian Chadd /* BHNDB_BUS Interface */ 284148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_generic_hwcfg, bwn_pci_get_generic_hwcfg), 285148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_hardware_table, bwn_pci_get_bhndb_hwtable), 286111d7cb2SLandon J. Fuller DEVMETHOD(bhndb_bus_get_hardware_prio, bwn_pci_get_bhndb_hwprio), 287148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_is_core_disabled, bwn_pci_is_core_disabled), 288148ed571SAdrian Chadd 289148ed571SAdrian Chadd DEVMETHOD_END 290148ed571SAdrian Chadd }; 291148ed571SAdrian Chadd 292148ed571SAdrian Chadd static devclass_t bwn_pci_devclass; 293148ed571SAdrian Chadd 2948d14ca9cSLandon J. Fuller DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, 2958d14ca9cSLandon J. Fuller sizeof(struct bwn_pci_softc)); 2968d14ca9cSLandon J. Fuller DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL, 2978d14ca9cSLandon J. Fuller NULL, SI_ORDER_ANY); 29896b52361SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_siba, 299*329e817fSWarner Losh siba_devices, nitems(siba_devices) - 1); 30096b52361SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_bcma, 301*329e817fSWarner Losh bcma_devices, nitems(bcma_devices) - 1); 302148ed571SAdrian Chadd DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL); 303148ed571SAdrian Chadd 304d177c199SLandon J. Fuller MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1); 3058d14ca9cSLandon J. Fuller MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1); 306148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1); 307148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1); 308148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1); 309148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1); 31019a63eb5SLandon J. Fuller MODULE_VERSION(bwn_pci, 1); 311