1148ed571SAdrian Chadd /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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> 305025b8d5SAdrian Chadd #include "opt_bwn.h" 315025b8d5SAdrian Chadd #include "opt_wlan.h" 325025b8d5SAdrian Chadd 33148ed571SAdrian Chadd #include <sys/param.h> 34148ed571SAdrian Chadd #include <sys/kernel.h> 35148ed571SAdrian Chadd #include <sys/bus.h> 36148ed571SAdrian Chadd #include <sys/module.h> 37148ed571SAdrian Chadd 38148ed571SAdrian Chadd #include <dev/pci/pcireg.h> 39148ed571SAdrian Chadd #include <dev/pci/pcivar.h> 40148ed571SAdrian Chadd 41148ed571SAdrian Chadd #include <dev/bhnd/bhndb/bhndb_pcivar.h> 42111d7cb2SLandon J. Fuller #include <dev/bhnd/bhndb/bhndb_hwdata.h> 43148ed571SAdrian Chadd #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h> 44148ed571SAdrian Chadd 45148ed571SAdrian Chadd #include <dev/bhnd/bhnd_ids.h> 46148ed571SAdrian Chadd 47148ed571SAdrian Chadd #include "bhndb_bus_if.h" 48148ed571SAdrian Chadd 49148ed571SAdrian Chadd #include "if_bwn_pcivar.h" 50148ed571SAdrian Chadd 51148ed571SAdrian Chadd /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */ 52148ed571SAdrian Chadd static int attach_untested = 0; 53148ed571SAdrian Chadd TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested); 54148ed571SAdrian Chadd 55148ed571SAdrian Chadd /* SIBA Devices */ 56148ed571SAdrian Chadd static const struct bwn_pci_device siba_devices[] = { 57148ed571SAdrian Chadd BWN_BCM_DEV(BCM4306_D11A, "BCM4306 802.11a", 58a225321fSLandon J. Fuller BWN_QUIRK_WLAN_DUALCORE|BWN_QUIRK_SOFTMODEM_UNPOPULATED), 59a225321fSLandon J. Fuller BWN_BCM_DEV(BCM4306_D11G, "BCM4306 802.11b/g", 60a225321fSLandon J. Fuller BWN_QUIRK_SOFTMODEM_UNPOPULATED), 61a225321fSLandon J. Fuller BWN_BCM_DEV(BCM4306_D11G_ID2, "BCM4306 802.11b/g", 62a225321fSLandon J. Fuller BWN_QUIRK_SOFTMODEM_UNPOPULATED), 63a225321fSLandon J. Fuller BWN_BCM_DEV(BCM4306_D11DUAL, "BCM4306 802.11a/b/g", 64a225321fSLandon J. Fuller BWN_QUIRK_SOFTMODEM_UNPOPULATED), 65148ed571SAdrian Chadd BWN_BCM_DEV(BCM4307, "BCM4307 802.11b", 0), 66148ed571SAdrian Chadd 67148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11G, "BCM4311 802.11b/g", 0), 68148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11DUAL, "BCM4311 802.11a/b/g", 0), 69148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11A, "BCM4311 802.11a", 70148ed571SAdrian Chadd BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE), 71148ed571SAdrian Chadd 72148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11G, "BCM4318 802.11b/g", 0), 73148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11DUAL, "BCM4318 802.11a/b/g", 0), 74148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11A, "BCM4318 802.11a", 75148ed571SAdrian Chadd BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE), 76148ed571SAdrian Chadd 777d616280SLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N, "BCM4321 802.11n Dual-Band", 787d616280SLandon J. Fuller BWN_QUIRK_USBH_UNPOPULATED), 797d616280SLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N2G, "BCM4321 802.11n 2GHz", 807d616280SLandon J. Fuller BWN_QUIRK_USBH_UNPOPULATED), 818d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N5G, "BCM4321 802.11n 5GHz", 827d616280SLandon J. Fuller BWN_QUIRK_UNTESTED|BWN_QUIRK_USBH_UNPOPULATED), 83148ed571SAdrian Chadd 84148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N, "BCM4322 802.11n Dual-Band", 0), 85148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N2G, "BCM4322 802.11n 2GHz", 86148ed571SAdrian Chadd BWN_QUIRK_UNTESTED), 87148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N5G, "BCM4322 802.11n 5GHz", 88148ed571SAdrian Chadd BWN_QUIRK_UNTESTED), 89148ed571SAdrian Chadd 90148ed571SAdrian Chadd BWN_BCM_DEV(BCM4328_D11G, "BCM4328/4312 802.11g", 0), 91148ed571SAdrian Chadd { 0, 0, NULL, 0 } 92148ed571SAdrian Chadd }; 93148ed571SAdrian Chadd 94148ed571SAdrian Chadd /** BCMA Devices */ 95148ed571SAdrian Chadd static const struct bwn_pci_device bcma_devices[] = { 96148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N, "BCM4331 802.11n Dual-Band", 0), 97148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N2G, "BCM4331 802.11n 2GHz", 0), 98148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N5G, "BCM4331 802.11n 5GHz", 0), 998d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N, "BCM43224 802.11n Dual-Band", 0), 1000bffd217SLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0), 101972459a6SAdrian Chadd BWN_BCM_DEV(BCM43225_D11N2G, "BCM43225 802.11n 2GHz", 0), 102148ed571SAdrian Chadd { 0, 0, NULL, 0} 103148ed571SAdrian Chadd }; 104148ed571SAdrian Chadd 105148ed571SAdrian Chadd /** Device configuration table */ 106148ed571SAdrian Chadd static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = { 107148ed571SAdrian Chadd /* SIBA devices */ 108148ed571SAdrian Chadd { 109148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_siba_generic_hwcfg, 110148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 111111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_siba_priority_table, 112148ed571SAdrian Chadd .devices = siba_devices 113148ed571SAdrian Chadd }, 114148ed571SAdrian Chadd /* BCMA devices */ 115148ed571SAdrian Chadd { 116148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_bcma_generic_hwcfg, 117148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 118111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_bcma_priority_table, 119148ed571SAdrian Chadd .devices = bcma_devices 120148ed571SAdrian Chadd }, 121148ed571SAdrian Chadd { NULL, NULL, NULL } 122148ed571SAdrian Chadd }; 123148ed571SAdrian Chadd 124148ed571SAdrian Chadd /** Search the device configuration table for an entry matching @p dev. */ 125148ed571SAdrian Chadd static int 126148ed571SAdrian Chadd bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg, 127148ed571SAdrian Chadd const struct bwn_pci_device **device) 128148ed571SAdrian Chadd { 129148ed571SAdrian Chadd const struct bwn_pci_devcfg *dvc; 130148ed571SAdrian Chadd const struct bwn_pci_device *dv; 131148ed571SAdrian Chadd 132148ed571SAdrian Chadd for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) { 133148ed571SAdrian Chadd for (dv = dvc->devices; dv->device != 0; dv++) { 134148ed571SAdrian Chadd if (pci_get_vendor(dev) == dv->vendor && 135148ed571SAdrian Chadd pci_get_device(dev) == dv->device) 136148ed571SAdrian Chadd { 137148ed571SAdrian Chadd if (cfg != NULL) 138148ed571SAdrian Chadd *cfg = dvc; 139148ed571SAdrian Chadd 140148ed571SAdrian Chadd if (device != NULL) 141148ed571SAdrian Chadd *device = dv; 142148ed571SAdrian Chadd 143148ed571SAdrian Chadd return (0); 144148ed571SAdrian Chadd } 145148ed571SAdrian Chadd } 146148ed571SAdrian Chadd } 147148ed571SAdrian Chadd 148148ed571SAdrian Chadd return (ENOENT); 149148ed571SAdrian Chadd } 150148ed571SAdrian Chadd 151148ed571SAdrian Chadd static int 152148ed571SAdrian Chadd bwn_pci_probe(device_t dev) 153148ed571SAdrian Chadd { 154148ed571SAdrian Chadd const struct bwn_pci_device *ident; 155148ed571SAdrian Chadd 156148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, NULL, &ident)) 157148ed571SAdrian Chadd return (ENXIO); 158148ed571SAdrian Chadd 159148ed571SAdrian Chadd /* Skip untested devices */ 160148ed571SAdrian Chadd if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested) 161148ed571SAdrian Chadd return (ENXIO); 162148ed571SAdrian Chadd 163148ed571SAdrian Chadd device_set_desc(dev, ident->desc); 164d177c199SLandon J. Fuller return (BUS_PROBE_DEFAULT); 165148ed571SAdrian Chadd } 166148ed571SAdrian Chadd 167148ed571SAdrian Chadd static int 168148ed571SAdrian Chadd bwn_pci_attach(device_t dev) 169148ed571SAdrian Chadd { 170148ed571SAdrian Chadd struct bwn_pci_softc *sc; 171148ed571SAdrian Chadd const struct bwn_pci_device *ident; 172148ed571SAdrian Chadd int error; 173148ed571SAdrian Chadd 174148ed571SAdrian Chadd sc = device_get_softc(dev); 175148ed571SAdrian Chadd sc->dev = dev; 176148ed571SAdrian Chadd 177148ed571SAdrian Chadd /* Find our hardware config */ 178148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident)) 179148ed571SAdrian Chadd return (ENXIO); 180148ed571SAdrian Chadd 181148ed571SAdrian Chadd /* Save quirk flags */ 182148ed571SAdrian Chadd sc->quirks = ident->quirks; 183148ed571SAdrian Chadd 184148ed571SAdrian Chadd /* Attach bridge device */ 185148ed571SAdrian Chadd if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1))) 186148ed571SAdrian Chadd return (ENXIO); 187148ed571SAdrian Chadd 188148ed571SAdrian Chadd /* Success */ 189148ed571SAdrian Chadd return (0); 190148ed571SAdrian Chadd } 191148ed571SAdrian Chadd 192148ed571SAdrian Chadd static void 193148ed571SAdrian Chadd bwn_pci_probe_nomatch(device_t dev, device_t child) 194148ed571SAdrian Chadd { 195148ed571SAdrian Chadd const char *name; 196148ed571SAdrian Chadd 197148ed571SAdrian Chadd name = device_get_name(child); 198148ed571SAdrian Chadd if (name == NULL) 199148ed571SAdrian Chadd name = "unknown device"; 200148ed571SAdrian Chadd 201148ed571SAdrian Chadd device_printf(dev, "<%s> (no driver attached)\n", name); 202148ed571SAdrian Chadd } 203148ed571SAdrian Chadd 204148ed571SAdrian Chadd static const struct bhndb_hwcfg * 205148ed571SAdrian Chadd bwn_pci_get_generic_hwcfg(device_t dev, device_t child) 206148ed571SAdrian Chadd { 207148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 208148ed571SAdrian Chadd return (sc->devcfg->bridge_hwcfg); 209148ed571SAdrian Chadd } 210148ed571SAdrian Chadd 211148ed571SAdrian Chadd static const struct bhndb_hw * 212148ed571SAdrian Chadd bwn_pci_get_bhndb_hwtable(device_t dev, device_t child) 213148ed571SAdrian Chadd { 214148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 215148ed571SAdrian Chadd return (sc->devcfg->bridge_hwtable); 216148ed571SAdrian Chadd } 217148ed571SAdrian Chadd 218111d7cb2SLandon J. Fuller static const struct bhndb_hw_priority * 219111d7cb2SLandon J. Fuller bwn_pci_get_bhndb_hwprio(device_t dev, device_t child) 220111d7cb2SLandon J. Fuller { 221111d7cb2SLandon J. Fuller struct bwn_pci_softc *sc = device_get_softc(dev); 222111d7cb2SLandon J. Fuller return (sc->devcfg->bridge_hwprio); 223111d7cb2SLandon J. Fuller } 224111d7cb2SLandon J. Fuller 225148ed571SAdrian Chadd static bool 226148ed571SAdrian Chadd bwn_pci_is_core_disabled(device_t dev, device_t child, 227148ed571SAdrian Chadd struct bhnd_core_info *core) 228148ed571SAdrian Chadd { 229148ed571SAdrian Chadd struct bwn_pci_softc *sc; 230148ed571SAdrian Chadd 231148ed571SAdrian Chadd sc = device_get_softc(dev); 232148ed571SAdrian Chadd 233148ed571SAdrian Chadd switch (bhnd_core_class(core)) { 234148ed571SAdrian Chadd case BHND_DEVCLASS_WLAN: 235148ed571SAdrian Chadd if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE)) 236148ed571SAdrian Chadd return (true); 237148ed571SAdrian Chadd 238148ed571SAdrian Chadd return (false); 239148ed571SAdrian Chadd 240148ed571SAdrian Chadd case BHND_DEVCLASS_ENET: 241148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_MAC: 242148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_PHY: 243148ed571SAdrian Chadd return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0); 244148ed571SAdrian Chadd 2457d616280SLandon J. Fuller case BHND_DEVCLASS_USB_HOST: 2467d616280SLandon J. Fuller return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0); 2477d616280SLandon J. Fuller 248a225321fSLandon J. Fuller case BHND_DEVCLASS_SOFTMODEM: 249a225321fSLandon J. Fuller return ((sc->quirks & BWN_QUIRK_SOFTMODEM_UNPOPULATED) != 0); 250a225321fSLandon J. Fuller 251148ed571SAdrian Chadd default: 252148ed571SAdrian Chadd return (false); 253148ed571SAdrian Chadd } 254148ed571SAdrian Chadd } 255148ed571SAdrian Chadd 256148ed571SAdrian Chadd static device_method_t bwn_pci_methods[] = { 257148ed571SAdrian Chadd /* Device interface */ 258148ed571SAdrian Chadd DEVMETHOD(device_probe, bwn_pci_probe), 259148ed571SAdrian Chadd DEVMETHOD(device_attach, bwn_pci_attach), 260*cf416f56SJohn Baldwin DEVMETHOD(device_detach, bus_generic_detach), 261148ed571SAdrian Chadd DEVMETHOD(device_shutdown, bus_generic_shutdown), 262148ed571SAdrian Chadd DEVMETHOD(device_suspend, bus_generic_suspend), 263148ed571SAdrian Chadd DEVMETHOD(device_resume, bus_generic_resume), 264148ed571SAdrian Chadd 265148ed571SAdrian Chadd /* Bus interface */ 266148ed571SAdrian Chadd DEVMETHOD(bus_probe_nomatch, bwn_pci_probe_nomatch), 267148ed571SAdrian Chadd 268148ed571SAdrian Chadd /* BHNDB_BUS Interface */ 269148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_generic_hwcfg, bwn_pci_get_generic_hwcfg), 270148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_hardware_table, bwn_pci_get_bhndb_hwtable), 271111d7cb2SLandon J. Fuller DEVMETHOD(bhndb_bus_get_hardware_prio, bwn_pci_get_bhndb_hwprio), 272148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_is_core_disabled, bwn_pci_is_core_disabled), 273148ed571SAdrian Chadd 274148ed571SAdrian Chadd DEVMETHOD_END 275148ed571SAdrian Chadd }; 276148ed571SAdrian Chadd 2778d14ca9cSLandon J. Fuller DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, 2788d14ca9cSLandon J. Fuller sizeof(struct bwn_pci_softc)); 279b338e3eaSJohn Baldwin DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, NULL, NULL, SI_ORDER_ANY); 28096b52361SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_siba, 281329e817fSWarner Losh siba_devices, nitems(siba_devices) - 1); 28296b52361SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_bcma, 283329e817fSWarner Losh bcma_devices, nitems(bcma_devices) - 1); 28405bdf34aSJohn Baldwin DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, NULL, NULL); 285148ed571SAdrian Chadd 286d177c199SLandon J. Fuller MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1); 2878d14ca9cSLandon J. Fuller MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1); 288148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1); 289148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1); 290148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1); 291148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1); 29219a63eb5SLandon J. Fuller MODULE_VERSION(bwn_pci, 1); 293