xref: /freebsd/sys/dev/bwn/if_bwn_pci.c (revision 02e9120893770924227138ba49df1edb3896112a)
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 #include "opt_bwn.h"
31 #include "opt_wlan.h"
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 
38 #include <dev/pci/pcireg.h>
39 #include <dev/pci/pcivar.h>
40 
41 #include <dev/bhnd/bhndb/bhndb_pcivar.h>
42 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
43 #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h>
44 
45 #include <dev/bhnd/bhnd_ids.h>
46 
47 #include "bhndb_bus_if.h"
48 
49 #include "if_bwn_pcivar.h"
50 
51 /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */
52 static int attach_untested = 0;
53 TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested);
54 
55 /* SIBA Devices */
56 static const struct bwn_pci_device siba_devices[] = {
57 	BWN_BCM_DEV(BCM4306_D11A,	"BCM4306 802.11a",
58 	    BWN_QUIRK_WLAN_DUALCORE|BWN_QUIRK_SOFTMODEM_UNPOPULATED),
59 	BWN_BCM_DEV(BCM4306_D11G,	"BCM4306 802.11b/g",
60 	    BWN_QUIRK_SOFTMODEM_UNPOPULATED),
61 	BWN_BCM_DEV(BCM4306_D11G_ID2,	"BCM4306 802.11b/g",
62 	    BWN_QUIRK_SOFTMODEM_UNPOPULATED),
63 	BWN_BCM_DEV(BCM4306_D11DUAL,	"BCM4306 802.11a/b/g",
64 	    BWN_QUIRK_SOFTMODEM_UNPOPULATED),
65 	BWN_BCM_DEV(BCM4307,		"BCM4307 802.11b",		0),
66 
67 	BWN_BCM_DEV(BCM4311_D11G,	"BCM4311 802.11b/g",		0),
68 	BWN_BCM_DEV(BCM4311_D11DUAL,	"BCM4311 802.11a/b/g",		0),
69 	BWN_BCM_DEV(BCM4311_D11A,	"BCM4311 802.11a",
70 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
71 
72 	BWN_BCM_DEV(BCM4318_D11G,	"BCM4318 802.11b/g",		0),
73 	BWN_BCM_DEV(BCM4318_D11DUAL,	"BCM4318 802.11a/b/g",		0),
74 	BWN_BCM_DEV(BCM4318_D11A,	"BCM4318 802.11a",
75 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
76 
77 	BWN_BCM_DEV(BCM4321_D11N,	"BCM4321 802.11n Dual-Band",
78 	    BWN_QUIRK_USBH_UNPOPULATED),
79 	BWN_BCM_DEV(BCM4321_D11N2G,	"BCM4321 802.11n 2GHz",
80 	    BWN_QUIRK_USBH_UNPOPULATED),
81 	BWN_BCM_DEV(BCM4321_D11N5G,	"BCM4321 802.11n 5GHz",
82 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_USBH_UNPOPULATED),
83 
84 	BWN_BCM_DEV(BCM4322_D11N,	"BCM4322 802.11n Dual-Band",	0),
85 	BWN_BCM_DEV(BCM4322_D11N2G,	"BCM4322 802.11n 2GHz",
86 	    BWN_QUIRK_UNTESTED),
87 	BWN_BCM_DEV(BCM4322_D11N5G,	"BCM4322 802.11n 5GHz",
88 	    BWN_QUIRK_UNTESTED),
89 
90 	BWN_BCM_DEV(BCM4328_D11G,	"BCM4328/4312 802.11g",		0),
91 	{ 0, 0, NULL, 0 }
92 };
93 
94 /** BCMA Devices */
95 static const struct bwn_pci_device bcma_devices[] = {
96 	BWN_BCM_DEV(BCM4331_D11N,	"BCM4331 802.11n Dual-Band",	0),
97 	BWN_BCM_DEV(BCM4331_D11N2G,	"BCM4331 802.11n 2GHz",		0),
98 	BWN_BCM_DEV(BCM4331_D11N5G,	"BCM4331 802.11n 5GHz",		0),
99 	BWN_BCM_DEV(BCM43224_D11N,	"BCM43224 802.11n Dual-Band",	0),
100 	BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0),
101 	BWN_BCM_DEV(BCM43225_D11N2G,	"BCM43225 802.11n 2GHz",	0),
102 	{ 0, 0, NULL, 0}
103 };
104 
105 /** Device configuration table */
106 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
107 	/* SIBA devices */
108 	{
109 		.bridge_hwcfg	= &bhndb_pci_siba_generic_hwcfg,
110 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
111 		.bridge_hwprio	= bhndb_siba_priority_table,
112 		.devices	= siba_devices
113 	},
114 	/* BCMA devices */
115 	{
116 		.bridge_hwcfg	= &bhndb_pci_bcma_generic_hwcfg,
117 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
118 		.bridge_hwprio	= bhndb_bcma_priority_table,
119 		.devices	= bcma_devices
120 	},
121 	{ NULL, NULL, NULL }
122 };
123 
124 /** Search the device configuration table for an entry matching @p dev. */
125 static int
126 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
127     const struct bwn_pci_device **device)
128 {
129 	const struct bwn_pci_devcfg	*dvc;
130 	const struct bwn_pci_device	*dv;
131 
132 	for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
133 		for (dv = dvc->devices; dv->device != 0; dv++) {
134 			if (pci_get_vendor(dev) == dv->vendor &&
135 			    pci_get_device(dev) == dv->device)
136 			{
137 				if (cfg != NULL)
138 					*cfg = dvc;
139 
140 				if (device != NULL)
141 					*device = dv;
142 
143 				return (0);
144 			}
145 		}
146 	}
147 
148 	return (ENOENT);
149 }
150 
151 static int
152 bwn_pci_probe(device_t dev)
153 {
154 	const struct bwn_pci_device	*ident;
155 
156 	if (bwn_pci_find_devcfg(dev, NULL, &ident))
157 		return (ENXIO);
158 
159 	/* Skip untested devices */
160 	if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
161 		return (ENXIO);
162 
163 	device_set_desc(dev, ident->desc);
164 	return (BUS_PROBE_DEFAULT);
165 }
166 
167 static int
168 bwn_pci_attach(device_t dev)
169 {
170 	struct bwn_pci_softc		*sc;
171 	const struct bwn_pci_device	*ident;
172 	int				 error;
173 
174 	sc = device_get_softc(dev);
175 	sc->dev = dev;
176 
177 	/* Find our hardware config */
178 	if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
179 		return (ENXIO);
180 
181 	/* Save quirk flags */
182 	sc->quirks = ident->quirks;
183 
184 	/* Attach bridge device */
185 	if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
186 		return (ENXIO);
187 
188 	/* Success */
189 	return (0);
190 }
191 
192 static int
193 bwn_pci_detach(device_t dev)
194 {
195 	int error;
196 
197 	if ((error = bus_generic_detach(dev)))
198 		return (error);
199 
200 	return (device_delete_children(dev));
201 }
202 
203 static void
204 bwn_pci_probe_nomatch(device_t dev, device_t child)
205 {
206 	const char *name;
207 
208 	name = device_get_name(child);
209 	if (name == NULL)
210 		name = "unknown device";
211 
212 	device_printf(dev, "<%s> (no driver attached)\n", name);
213 }
214 
215 static const struct bhndb_hwcfg *
216 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
217 {
218 	struct bwn_pci_softc *sc = device_get_softc(dev);
219 	return (sc->devcfg->bridge_hwcfg);
220 }
221 
222 static const struct bhndb_hw *
223 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
224 {
225 	struct bwn_pci_softc *sc = device_get_softc(dev);
226 	return (sc->devcfg->bridge_hwtable);
227 }
228 
229 static const struct bhndb_hw_priority *
230 bwn_pci_get_bhndb_hwprio(device_t dev, device_t child)
231 {
232 	struct bwn_pci_softc *sc = device_get_softc(dev);
233 	return (sc->devcfg->bridge_hwprio);
234 }
235 
236 static bool
237 bwn_pci_is_core_disabled(device_t dev, device_t child,
238     struct bhnd_core_info *core)
239 {
240 	struct bwn_pci_softc	*sc;
241 
242 	sc = device_get_softc(dev);
243 
244 	switch (bhnd_core_class(core)) {
245 	case BHND_DEVCLASS_WLAN:
246 		if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
247 			return (true);
248 
249 		return (false);
250 
251 	case BHND_DEVCLASS_ENET:
252 	case BHND_DEVCLASS_ENET_MAC:
253 	case BHND_DEVCLASS_ENET_PHY:
254 		return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
255 
256 	case BHND_DEVCLASS_USB_HOST:
257 		return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0);
258 
259 	case BHND_DEVCLASS_SOFTMODEM:
260 		return ((sc->quirks & BWN_QUIRK_SOFTMODEM_UNPOPULATED) != 0);
261 
262 	default:
263 		return (false);
264 	}
265 }
266 
267 static device_method_t bwn_pci_methods[] = {
268 	/* Device interface */
269 	DEVMETHOD(device_probe,			bwn_pci_probe),
270 	DEVMETHOD(device_attach,		bwn_pci_attach),
271 	DEVMETHOD(device_detach,		bwn_pci_detach),
272 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
273 	DEVMETHOD(device_suspend,		bus_generic_suspend),
274 	DEVMETHOD(device_resume,		bus_generic_resume),
275 
276 	/* Bus interface */
277 	DEVMETHOD(bus_probe_nomatch,		bwn_pci_probe_nomatch),
278 
279 	/* BHNDB_BUS Interface */
280 	DEVMETHOD(bhndb_bus_get_generic_hwcfg,	bwn_pci_get_generic_hwcfg),
281 	DEVMETHOD(bhndb_bus_get_hardware_table,	bwn_pci_get_bhndb_hwtable),
282 	DEVMETHOD(bhndb_bus_get_hardware_prio,	bwn_pci_get_bhndb_hwprio),
283 	DEVMETHOD(bhndb_bus_is_core_disabled,	bwn_pci_is_core_disabled),
284 
285 	DEVMETHOD_END
286 };
287 
288 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods,
289     sizeof(struct bwn_pci_softc));
290 DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, NULL, NULL, SI_ORDER_ANY);
291 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_siba,
292     siba_devices, nitems(siba_devices) - 1);
293 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, bwn_bcma,
294     bcma_devices, nitems(bcma_devices) - 1);
295 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, NULL, NULL);
296 
297 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
298 MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1);
299 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
300 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
301 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
302 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);
303 MODULE_VERSION(bwn_pci, 1);
304