xref: /freebsd/sys/dev/bwn/if_bwn_pci.c (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 	{ 0, 0, NULL, 0 }
94 };
95 
96 /** BCMA Devices */
97 static const struct bwn_pci_device bcma_devices[] = {
98 	BWN_BCM_DEV(BCM4331_D11N,	"BCM4331 802.11n Dual-Band",	0),
99 	BWN_BCM_DEV(BCM4331_D11N2G,	"BCM4331 802.11n 2GHz",		0),
100 	BWN_BCM_DEV(BCM4331_D11N5G,	"BCM4331 802.11n 5GHz",		0),
101 	BWN_BCM_DEV(BCM43224_D11N,	"BCM43224 802.11n Dual-Band",	0),
102 	BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0),
103 	BWN_BCM_DEV(BCM43225_D11N2G,	"BCM43225 802.11n 2GHz",	0),
104 	{ 0, 0, NULL, 0}
105 };
106 
107 /** Device configuration table */
108 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
109 	/* SIBA devices */
110 	{
111 		.bridge_hwcfg	= &bhndb_pci_siba_generic_hwcfg,
112 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
113 		.bridge_hwprio	= bhndb_siba_priority_table,
114 		.devices	= siba_devices
115 	},
116 	/* BCMA devices */
117 	{
118 		.bridge_hwcfg	= &bhndb_pci_bcma_generic_hwcfg,
119 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
120 		.bridge_hwprio	= bhndb_bcma_priority_table,
121 		.devices	= bcma_devices
122 	},
123 	{ NULL, NULL, NULL }
124 };
125 
126 /** Search the device configuration table for an entry matching @p dev. */
127 static int
128 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
129     const struct bwn_pci_device **device)
130 {
131 	const struct bwn_pci_devcfg	*dvc;
132 	const struct bwn_pci_device	*dv;
133 
134 	for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
135 		for (dv = dvc->devices; dv->device != 0; dv++) {
136 			if (pci_get_vendor(dev) == dv->vendor &&
137 			    pci_get_device(dev) == dv->device)
138 			{
139 				if (cfg != NULL)
140 					*cfg = dvc;
141 
142 				if (device != NULL)
143 					*device = dv;
144 
145 				return (0);
146 			}
147 		}
148 	}
149 
150 	return (ENOENT);
151 }
152 
153 static int
154 bwn_pci_probe(device_t dev)
155 {
156 	const struct bwn_pci_device	*ident;
157 
158 	if (bwn_pci_find_devcfg(dev, NULL, &ident))
159 		return (ENXIO);
160 
161 	/* Skip untested devices */
162 	if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
163 		return (ENXIO);
164 
165 	device_set_desc(dev, ident->desc);
166 	return (BUS_PROBE_DEFAULT);
167 }
168 
169 static int
170 bwn_pci_attach(device_t dev)
171 {
172 	struct bwn_pci_softc		*sc;
173 	const struct bwn_pci_device	*ident;
174 	int				 error;
175 
176 	sc = device_get_softc(dev);
177 	sc->dev = dev;
178 
179 	/* Find our hardware config */
180 	if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
181 		return (ENXIO);
182 
183 	/* Save quirk flags */
184 	sc->quirks = ident->quirks;
185 
186 	/* Attach bridge device */
187 	if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
188 		return (ENXIO);
189 
190 	/* Success */
191 	return (0);
192 }
193 
194 static int
195 bwn_pci_detach(device_t dev)
196 {
197 	int error;
198 
199 	if ((error = bus_generic_detach(dev)))
200 		return (error);
201 
202 	return (device_delete_children(dev));
203 }
204 
205 static void
206 bwn_pci_probe_nomatch(device_t dev, device_t child)
207 {
208 	const char *name;
209 
210 	name = device_get_name(child);
211 	if (name == NULL)
212 		name = "unknown device";
213 
214 	device_printf(dev, "<%s> (no driver attached)\n", name);
215 }
216 
217 static const struct bhndb_hwcfg *
218 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
219 {
220 	struct bwn_pci_softc *sc = device_get_softc(dev);
221 	return (sc->devcfg->bridge_hwcfg);
222 }
223 
224 static const struct bhndb_hw *
225 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
226 {
227 	struct bwn_pci_softc *sc = device_get_softc(dev);
228 	return (sc->devcfg->bridge_hwtable);
229 }
230 
231 static const struct bhndb_hw_priority *
232 bwn_pci_get_bhndb_hwprio(device_t dev, device_t child)
233 {
234 	struct bwn_pci_softc *sc = device_get_softc(dev);
235 	return (sc->devcfg->bridge_hwprio);
236 }
237 
238 static bool
239 bwn_pci_is_core_disabled(device_t dev, device_t child,
240     struct bhnd_core_info *core)
241 {
242 	struct bwn_pci_softc	*sc;
243 
244 	sc = device_get_softc(dev);
245 
246 	switch (bhnd_core_class(core)) {
247 	case BHND_DEVCLASS_WLAN:
248 		if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
249 			return (true);
250 
251 		return (false);
252 
253 	case BHND_DEVCLASS_ENET:
254 	case BHND_DEVCLASS_ENET_MAC:
255 	case BHND_DEVCLASS_ENET_PHY:
256 		return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
257 
258 	case BHND_DEVCLASS_USB_HOST:
259 		return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0);
260 
261 	case BHND_DEVCLASS_SOFTMODEM:
262 		return ((sc->quirks & BWN_QUIRK_SOFTMODEM_UNPOPULATED) != 0);
263 
264 	default:
265 		return (false);
266 	}
267 }
268 
269 static device_method_t bwn_pci_methods[] = {
270 	/* Device interface */
271 	DEVMETHOD(device_probe,			bwn_pci_probe),
272 	DEVMETHOD(device_attach,		bwn_pci_attach),
273 	DEVMETHOD(device_detach,		bwn_pci_detach),
274 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
275 	DEVMETHOD(device_suspend,		bus_generic_suspend),
276 	DEVMETHOD(device_resume,		bus_generic_resume),
277 
278 	/* Bus interface */
279 	DEVMETHOD(bus_probe_nomatch,		bwn_pci_probe_nomatch),
280 
281 	/* BHNDB_BUS Interface */
282 	DEVMETHOD(bhndb_bus_get_generic_hwcfg,	bwn_pci_get_generic_hwcfg),
283 	DEVMETHOD(bhndb_bus_get_hardware_table,	bwn_pci_get_bhndb_hwtable),
284 	DEVMETHOD(bhndb_bus_get_hardware_prio,	bwn_pci_get_bhndb_hwprio),
285 	DEVMETHOD(bhndb_bus_is_core_disabled,	bwn_pci_is_core_disabled),
286 
287 	DEVMETHOD_END
288 };
289 
290 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods,
291     sizeof(struct bwn_pci_softc));
292 DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, NULL, NULL, SI_ORDER_ANY);
293 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_siba,
294     siba_devices, nitems(siba_devices) - 1);
295 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_bcma,
296     bcma_devices, nitems(bcma_devices) - 1);
297 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, NULL, NULL);
298 
299 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
300 MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1);
301 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
302 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
303 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
304 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);
305 MODULE_VERSION(bwn_pci, 1);
306