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