]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bwn/if_bwn_pci.c
Merge llvm trunk r321414 to contrib/llvm.
[FreeBSD/FreeBSD.git] / sys / dev / bwn / if_bwn_pci.c
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 /* If non-zero, probe at a higher priority than the stable if_bwn driver. */
58 static int prefer_new_driver = 0; 
59 TUNABLE_INT("hw.bwn_pci.preferred", &prefer_new_driver);
60
61 /* SIBA Devices */
62 static const struct bwn_pci_device siba_devices[] = {
63         BWN_BCM_DEV(BCM4306_D11A,       "BCM4306 802.11a",
64             BWN_QUIRK_WLAN_DUALCORE),
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
92         { 0, 0, NULL, 0 }
93 };
94
95 /** BCMA Devices */
96 static const struct bwn_pci_device bcma_devices[] = {
97         BWN_BCM_DEV(BCM4331_D11N,       "BCM4331 802.11n Dual-Band",    0),
98         BWN_BCM_DEV(BCM4331_D11N2G,     "BCM4331 802.11n 2GHz",         0),
99         BWN_BCM_DEV(BCM4331_D11N5G,     "BCM4331 802.11n 5GHz",         0),
100         BWN_BCM_DEV(BCM43224_D11N,      "BCM43224 802.11n Dual-Band",   0),
101         BWN_BCM_DEV(BCM43224_D11N_ID_VEN1, "BCM43224 802.11n Dual-Band",0),
102         BWN_BCM_DEV(BCM43225_D11N2G,    "BCM43225 802.11n 2GHz",        0),
103
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
167         /* Until this driver is complete, require explicit opt-in before
168          * superceding if_bwn/siba_bwn. */
169         if (prefer_new_driver)
170                 return (BUS_PROBE_DEFAULT+1);
171         else
172                 return (BUS_PROBE_LOW_PRIORITY);
173
174         // return (BUS_PROBE_DEFAULT);
175 }
176
177 static int
178 bwn_pci_attach(device_t dev)
179 {
180         struct bwn_pci_softc            *sc;
181         const struct bwn_pci_device     *ident;
182         int                              error;
183
184         sc = device_get_softc(dev);
185         sc->dev = dev;
186
187         /* Find our hardware config */
188         if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
189                 return (ENXIO);
190
191         /* Save quirk flags */
192         sc->quirks = ident->quirks;
193
194         /* Attach bridge device */
195         if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
196                 return (ENXIO);
197
198         /* Success */
199         return (0);
200 }
201
202 static int
203 bwn_pci_detach(device_t dev)
204 {
205         int error;
206
207         if ((error = bus_generic_detach(dev)))
208                 return (error);
209
210         return (device_delete_children(dev));
211 }
212
213 static void
214 bwn_pci_probe_nomatch(device_t dev, device_t child)
215 {
216         const char *name;
217
218         name = device_get_name(child);
219         if (name == NULL)
220                 name = "unknown device";
221
222         device_printf(dev, "<%s> (no driver attached)\n", name);
223 }
224
225 static const struct bhndb_hwcfg *
226 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
227 {
228         struct bwn_pci_softc *sc = device_get_softc(dev);
229         return (sc->devcfg->bridge_hwcfg);
230 }
231
232 static const struct bhndb_hw *
233 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
234 {
235         struct bwn_pci_softc *sc = device_get_softc(dev);
236         return (sc->devcfg->bridge_hwtable);
237 }
238
239 static const struct bhndb_hw_priority *
240 bwn_pci_get_bhndb_hwprio(device_t dev, device_t child)
241 {
242         struct bwn_pci_softc *sc = device_get_softc(dev);
243         return (sc->devcfg->bridge_hwprio);
244 }
245
246 static bool
247 bwn_pci_is_core_disabled(device_t dev, device_t child,
248     struct bhnd_core_info *core)
249 {
250         struct bwn_pci_softc    *sc;
251
252         sc = device_get_softc(dev);
253
254         switch (bhnd_core_class(core)) {
255         case BHND_DEVCLASS_WLAN:
256                 if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
257                         return (true);
258
259                 return (false);
260
261         case BHND_DEVCLASS_ENET:
262         case BHND_DEVCLASS_ENET_MAC:
263         case BHND_DEVCLASS_ENET_PHY:
264                 return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
265                 
266         case BHND_DEVCLASS_USB_HOST:
267                 return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0);
268
269         default:
270                 return (false);
271         }
272 }
273
274 static device_method_t bwn_pci_methods[] = {
275         /* Device interface */
276         DEVMETHOD(device_probe,                 bwn_pci_probe),
277         DEVMETHOD(device_attach,                bwn_pci_attach),
278         DEVMETHOD(device_detach,                bwn_pci_detach),
279         DEVMETHOD(device_shutdown,              bus_generic_shutdown),
280         DEVMETHOD(device_suspend,               bus_generic_suspend),
281         DEVMETHOD(device_resume,                bus_generic_resume),
282
283         /* Bus interface */
284         DEVMETHOD(bus_probe_nomatch,            bwn_pci_probe_nomatch),
285
286         /* BHNDB_BUS Interface */
287         DEVMETHOD(bhndb_bus_get_generic_hwcfg,  bwn_pci_get_generic_hwcfg),
288         DEVMETHOD(bhndb_bus_get_hardware_table, bwn_pci_get_bhndb_hwtable),
289         DEVMETHOD(bhndb_bus_get_hardware_prio,  bwn_pci_get_bhndb_hwprio),
290         DEVMETHOD(bhndb_bus_is_core_disabled,   bwn_pci_is_core_disabled),
291
292         DEVMETHOD_END
293 };
294
295 static devclass_t bwn_pci_devclass;
296
297 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods,
298     sizeof(struct bwn_pci_softc));
299 DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL,
300     NULL, SI_ORDER_ANY);
301 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL);
302
303 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
304 MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1);
305 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
306 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
307 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
308 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);