xref: /freebsd/sys/dev/bwn/if_bwn_pci.c (revision d34048812292b714a0bf99967270d18fe3097c62)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_bwn.h"
33 #include "opt_wlan.h"
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/module.h>
39 
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 
43 #include <dev/bhnd/bhndb/bhndb_pcivar.h>
44 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
45 #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h>
46 
47 #include <dev/bhnd/bhnd_ids.h>
48 
49 #include "bhndb_bus_if.h"
50 
51 #include "if_bwn_pcivar.h"
52 
53 /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */
54 static int attach_untested = 0;
55 TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested);
56 
57 /* SIBA Devices */
58 static const struct bwn_pci_device siba_devices[] = {
59 	BWN_BCM_DEV(BCM4306_D11A,	"BCM4306 802.11a",
60 	    BWN_QUIRK_WLAN_DUALCORE|BWN_QUIRK_SOFTMODEM_UNPOPULATED),
61 	BWN_BCM_DEV(BCM4306_D11G,	"BCM4306 802.11b/g",
62 	    BWN_QUIRK_SOFTMODEM_UNPOPULATED),
63 	BWN_BCM_DEV(BCM4306_D11G_ID2,	"BCM4306 802.11b/g",
64 	    BWN_QUIRK_SOFTMODEM_UNPOPULATED),
65 	BWN_BCM_DEV(BCM4306_D11DUAL,	"BCM4306 802.11a/b/g",
66 	    BWN_QUIRK_SOFTMODEM_UNPOPULATED),
67 	BWN_BCM_DEV(BCM4307,		"BCM4307 802.11b",		0),
68 
69 	BWN_BCM_DEV(BCM4311_D11G,	"BCM4311 802.11b/g",		0),
70 	BWN_BCM_DEV(BCM4311_D11DUAL,	"BCM4311 802.11a/b/g",		0),
71 	BWN_BCM_DEV(BCM4311_D11A,	"BCM4311 802.11a",
72 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
73 
74 	BWN_BCM_DEV(BCM4318_D11G,	"BCM4318 802.11b/g",		0),
75 	BWN_BCM_DEV(BCM4318_D11DUAL,	"BCM4318 802.11a/b/g",		0),
76 	BWN_BCM_DEV(BCM4318_D11A,	"BCM4318 802.11a",
77 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
78 
79 	BWN_BCM_DEV(BCM4321_D11N,	"BCM4321 802.11n Dual-Band",
80 	    BWN_QUIRK_USBH_UNPOPULATED),
81 	BWN_BCM_DEV(BCM4321_D11N2G,	"BCM4321 802.11n 2GHz",
82 	    BWN_QUIRK_USBH_UNPOPULATED),
83 	BWN_BCM_DEV(BCM4321_D11N5G,	"BCM4321 802.11n 5GHz",
84 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_USBH_UNPOPULATED),
85 
86 	BWN_BCM_DEV(BCM4322_D11N,	"BCM4322 802.11n Dual-Band",	0),
87 	BWN_BCM_DEV(BCM4322_D11N2G,	"BCM4322 802.11n 2GHz",
88 	    BWN_QUIRK_UNTESTED),
89 	BWN_BCM_DEV(BCM4322_D11N5G,	"BCM4322 802.11n 5GHz",
90 	    BWN_QUIRK_UNTESTED),
91 
92 	BWN_BCM_DEV(BCM4328_D11G,	"BCM4328/4312 802.11g",		0),
93 
94 	{ 0, 0, NULL, 0 }
95 };
96 
97 /** BCMA Devices */
98 static const struct bwn_pci_device bcma_devices[] = {
99 	BWN_BCM_DEV(BCM4331_D11N,	"BCM4331 802.11n Dual-Band",	0),
100 	BWN_BCM_DEV(BCM4331_D11N2G,	"BCM4331 802.11n 2GHz",		0),
101 	BWN_BCM_DEV(BCM4331_D11N5G,	"BCM4331 802.11n 5GHz",		0),
102 	BWN_BCM_DEV(BCM43224_D11N,	"BCM43224 802.11n Dual-Band",	0),
103 	BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0),
104 	BWN_BCM_DEV(BCM43225_D11N2G,	"BCM43225 802.11n 2GHz",	0),
105 
106 	{ 0, 0, NULL, 0}
107 };
108 
109 /** Device configuration table */
110 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
111 	/* SIBA devices */
112 	{
113 		.bridge_hwcfg	= &bhndb_pci_siba_generic_hwcfg,
114 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
115 		.bridge_hwprio	= bhndb_siba_priority_table,
116 		.devices	= siba_devices
117 	},
118 	/* BCMA devices */
119 	{
120 		.bridge_hwcfg	= &bhndb_pci_bcma_generic_hwcfg,
121 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
122 		.bridge_hwprio	= bhndb_bcma_priority_table,
123 		.devices	= bcma_devices
124 	},
125 	{ NULL, NULL, NULL }
126 };
127 
128 /** Search the device configuration table for an entry matching @p dev. */
129 static int
130 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
131     const struct bwn_pci_device **device)
132 {
133 	const struct bwn_pci_devcfg	*dvc;
134 	const struct bwn_pci_device	*dv;
135 
136 	for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
137 		for (dv = dvc->devices; dv->device != 0; dv++) {
138 			if (pci_get_vendor(dev) == dv->vendor &&
139 			    pci_get_device(dev) == dv->device)
140 			{
141 				if (cfg != NULL)
142 					*cfg = dvc;
143 
144 				if (device != NULL)
145 					*device = dv;
146 
147 				return (0);
148 			}
149 		}
150 	}
151 
152 	return (ENOENT);
153 }
154 
155 static int
156 bwn_pci_probe(device_t dev)
157 {
158 	const struct bwn_pci_device	*ident;
159 
160 	if (bwn_pci_find_devcfg(dev, NULL, &ident))
161 		return (ENXIO);
162 
163 	/* Skip untested devices */
164 	if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
165 		return (ENXIO);
166 
167 	device_set_desc(dev, ident->desc);
168 	return (BUS_PROBE_DEFAULT);
169 }
170 
171 static int
172 bwn_pci_attach(device_t dev)
173 {
174 	struct bwn_pci_softc		*sc;
175 	const struct bwn_pci_device	*ident;
176 	int				 error;
177 
178 	sc = device_get_softc(dev);
179 	sc->dev = dev;
180 
181 	/* Find our hardware config */
182 	if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
183 		return (ENXIO);
184 
185 	/* Save quirk flags */
186 	sc->quirks = ident->quirks;
187 
188 	/* Attach bridge device */
189 	if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
190 		return (ENXIO);
191 
192 	/* Success */
193 	return (0);
194 }
195 
196 static int
197 bwn_pci_detach(device_t dev)
198 {
199 	int error;
200 
201 	if ((error = bus_generic_detach(dev)))
202 		return (error);
203 
204 	return (device_delete_children(dev));
205 }
206 
207 static void
208 bwn_pci_probe_nomatch(device_t dev, device_t child)
209 {
210 	const char *name;
211 
212 	name = device_get_name(child);
213 	if (name == NULL)
214 		name = "unknown device";
215 
216 	device_printf(dev, "<%s> (no driver attached)\n", name);
217 }
218 
219 static const struct bhndb_hwcfg *
220 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
221 {
222 	struct bwn_pci_softc *sc = device_get_softc(dev);
223 	return (sc->devcfg->bridge_hwcfg);
224 }
225 
226 static const struct bhndb_hw *
227 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
228 {
229 	struct bwn_pci_softc *sc = device_get_softc(dev);
230 	return (sc->devcfg->bridge_hwtable);
231 }
232 
233 static const struct bhndb_hw_priority *
234 bwn_pci_get_bhndb_hwprio(device_t dev, device_t child)
235 {
236 	struct bwn_pci_softc *sc = device_get_softc(dev);
237 	return (sc->devcfg->bridge_hwprio);
238 }
239 
240 static bool
241 bwn_pci_is_core_disabled(device_t dev, device_t child,
242     struct bhnd_core_info *core)
243 {
244 	struct bwn_pci_softc	*sc;
245 
246 	sc = device_get_softc(dev);
247 
248 	switch (bhnd_core_class(core)) {
249 	case BHND_DEVCLASS_WLAN:
250 		if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
251 			return (true);
252 
253 		return (false);
254 
255 	case BHND_DEVCLASS_ENET:
256 	case BHND_DEVCLASS_ENET_MAC:
257 	case BHND_DEVCLASS_ENET_PHY:
258 		return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
259 
260 	case BHND_DEVCLASS_USB_HOST:
261 		return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0);
262 
263 	case BHND_DEVCLASS_SOFTMODEM:
264 		return ((sc->quirks & BWN_QUIRK_SOFTMODEM_UNPOPULATED) != 0);
265 
266 	default:
267 		return (false);
268 	}
269 }
270 
271 static device_method_t bwn_pci_methods[] = {
272 	/* Device interface */
273 	DEVMETHOD(device_probe,			bwn_pci_probe),
274 	DEVMETHOD(device_attach,		bwn_pci_attach),
275 	DEVMETHOD(device_detach,		bwn_pci_detach),
276 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
277 	DEVMETHOD(device_suspend,		bus_generic_suspend),
278 	DEVMETHOD(device_resume,		bus_generic_resume),
279 
280 	/* Bus interface */
281 	DEVMETHOD(bus_probe_nomatch,		bwn_pci_probe_nomatch),
282 
283 	/* BHNDB_BUS Interface */
284 	DEVMETHOD(bhndb_bus_get_generic_hwcfg,	bwn_pci_get_generic_hwcfg),
285 	DEVMETHOD(bhndb_bus_get_hardware_table,	bwn_pci_get_bhndb_hwtable),
286 	DEVMETHOD(bhndb_bus_get_hardware_prio,	bwn_pci_get_bhndb_hwprio),
287 	DEVMETHOD(bhndb_bus_is_core_disabled,	bwn_pci_is_core_disabled),
288 
289 	DEVMETHOD_END
290 };
291 
292 static devclass_t bwn_pci_devclass;
293 
294 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods,
295     sizeof(struct bwn_pci_softc));
296 DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL,
297     NULL, SI_ORDER_ANY);
298 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_siba,
299     siba_devices, nitems(siba_devices) - 1);
300 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_bcma,
301     bcma_devices, nitems(bcma_devices) - 1);
302 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL);
303 
304 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
305 MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1);
306 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
307 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
308 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
309 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);
310 MODULE_VERSION(bwn_pci, 1);
311