]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bwn/if_bwn_pci.c
[bwn] add an implementation of "cordic" and imaginary math.
[FreeBSD/FreeBSD.git] / sys / dev / bwn / if_bwn_pci.c
1 /*-
2  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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_pci_hwdata.h>
43
44 #include <dev/bhnd/bhnd_ids.h>
45
46 #include "bhndb_bus_if.h"
47
48 #include "if_bwn_pcivar.h"
49
50 /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */
51 static int attach_untested = 0; 
52 TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested);
53
54 /* If non-zero, probe at a higher priority than the stable if_bwn driver. */
55 static int prefer_new_driver = 0; 
56 TUNABLE_INT("hw.bwn_pci.preferred", &prefer_new_driver);
57
58 /* SIBA Devices */
59 static const struct bwn_pci_device siba_devices[] = {
60         BWN_BCM_DEV(BCM4301,            "BCM4301 802.11b",
61             BWN_QUIRK_ENET_HW_UNPOPULATED),
62
63         BWN_BCM_DEV(BCM4306,            "BCM4306 802.11b/g",            0),
64         BWN_BCM_DEV(BCM4306_D11G,       "BCM4306 802.11g",              0),
65         BWN_BCM_DEV(BCM4306_D11A,       "BCM4306 802.11a",
66             BWN_QUIRK_WLAN_DUALCORE),
67         BWN_BCM_DEV(BCM4306_D11DUAL,    "BCM4306 802.11a/b",
68             BWN_QUIRK_WLAN_DUALCORE),
69         BWN_BCM_DEV(BCM4306_D11G_ID2,   "BCM4306 802.11g",              0),
70
71         BWN_BCM_DEV(BCM4307,            "BCM4307 802.11b",              0),
72
73         BWN_BCM_DEV(BCM4311_D11G,       "BCM4311 802.11b/g",            0),
74         BWN_BCM_DEV(BCM4311_D11DUAL,    "BCM4311 802.11a/b/g",          0),
75         BWN_BCM_DEV(BCM4311_D11A,       "BCM4311 802.11a",
76             BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
77
78         BWN_BCM_DEV(BCM4318_D11G,       "BCM4318 802.11b/g",            0),
79         BWN_BCM_DEV(BCM4318_D11DUAL,    "BCM4318 802.11a/b/g",          0),
80         BWN_BCM_DEV(BCM4318_D11A,       "BCM4318 802.11a",
81             BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
82
83         BWN_BCM_DEV(BCM4321_D11N,       "BCM4321 802.11n Dual-Band",    0),
84         BWN_BCM_DEV(BCM4321_D11N2G,     "BCM4321 802.11n 2GHz",         0),
85         BWN_BCM_DEV(BCM4321_D11N2G,     "BCM4321 802.11n 5GHz",
86             BWN_QUIRK_UNTESTED),
87
88         BWN_BCM_DEV(BCM4322_D11N,       "BCM4322 802.11n Dual-Band",    0),
89         BWN_BCM_DEV(BCM4322_D11N2G,     "BCM4322 802.11n 2GHz",
90             BWN_QUIRK_UNTESTED),
91         BWN_BCM_DEV(BCM4322_D11N5G,     "BCM4322 802.11n 5GHz",
92             BWN_QUIRK_UNTESTED),
93
94         BWN_BCM_DEV(BCM4328_D11G,       "BCM4328/4312 802.11g",         0),
95
96         { 0, 0, NULL, 0 }
97 };
98
99 /** BCMA Devices */
100 static const struct bwn_pci_device bcma_devices[] = {
101         BWN_BCM_DEV(BCM4331_D11N,       "BCM4331 802.11n Dual-Band",    0),
102         BWN_BCM_DEV(BCM4331_D11N2G,     "BCM4331 802.11n 2GHz",         0),
103         BWN_BCM_DEV(BCM4331_D11N5G,     "BCM4331 802.11n 5GHz",         0),
104
105         { 0, 0, NULL, 0}
106 };
107
108 /** Device configuration table */
109 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
110         /* SIBA devices */
111         {
112                 .bridge_hwcfg   = &bhndb_pci_siba_generic_hwcfg,
113                 .bridge_hwtable = bhndb_pci_generic_hw_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                 .devices        = bcma_devices
121         },
122         { NULL, NULL, NULL }
123 };
124
125 /** Search the device configuration table for an entry matching @p dev. */
126 static int
127 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
128     const struct bwn_pci_device **device)
129 {
130         const struct bwn_pci_devcfg     *dvc;
131         const struct bwn_pci_device     *dv;
132
133         for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
134                 for (dv = dvc->devices; dv->device != 0; dv++) {
135                         if (pci_get_vendor(dev) == dv->vendor &&
136                             pci_get_device(dev) == dv->device)
137                         {
138                                 if (cfg != NULL)
139                                         *cfg = dvc;
140                                 
141                                 if (device != NULL)
142                                         *device = dv;
143                                 
144                                 return (0);
145                         }
146                 }
147         }
148
149         return (ENOENT);
150 }
151
152 static int
153 bwn_pci_probe(device_t dev)
154 {
155         const struct bwn_pci_device     *ident;
156
157         if (bwn_pci_find_devcfg(dev, NULL, &ident))
158                 return (ENXIO);
159
160         /* Skip untested devices */
161         if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
162                 return (ENXIO);
163
164         device_set_desc(dev, ident->desc);
165
166         /* Until this driver is complete, require explicit opt-in before
167          * superceding if_bwn/siba_bwn. */
168         if (prefer_new_driver)
169                 return (BUS_PROBE_DEFAULT+1);
170         else
171                 return (BUS_PROBE_LOW_PRIORITY);
172
173         // return (BUS_PROBE_DEFAULT);
174 }
175
176 static int
177 bwn_pci_attach(device_t dev)
178 {
179         struct bwn_pci_softc            *sc;
180         const struct bwn_pci_device     *ident;
181         int                              error;
182
183         sc = device_get_softc(dev);
184         sc->dev = dev;
185
186         /* Find our hardware config */
187         if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
188                 return (ENXIO);
189
190         /* Save quirk flags */
191         sc->quirks = ident->quirks;
192
193         /* Attach bridge device */
194         if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
195                 return (ENXIO);
196
197         /* Success */
198         return (0);
199 }
200
201 static int
202 bwn_pci_detach(device_t dev)
203 {
204         return (bus_generic_detach(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 bool
234 bwn_pci_is_core_disabled(device_t dev, device_t child,
235     struct bhnd_core_info *core)
236 {
237         struct bwn_pci_softc    *sc;
238
239         sc = device_get_softc(dev);
240
241         switch (bhnd_core_class(core)) {
242         case BHND_DEVCLASS_WLAN:
243                 if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
244                         return (true);
245
246                 return (false);
247
248         case BHND_DEVCLASS_ENET:
249         case BHND_DEVCLASS_ENET_MAC:
250         case BHND_DEVCLASS_ENET_PHY:
251                 return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
252
253         default:
254                 return (false);
255         }
256 }
257
258 static device_method_t bwn_pci_methods[] = {
259         /* Device interface */
260         DEVMETHOD(device_probe,                 bwn_pci_probe),
261         DEVMETHOD(device_attach,                bwn_pci_attach),
262         DEVMETHOD(device_detach,                bwn_pci_detach),
263         DEVMETHOD(device_shutdown,              bus_generic_shutdown),
264         DEVMETHOD(device_suspend,               bus_generic_suspend),
265         DEVMETHOD(device_resume,                bus_generic_resume),
266
267         /* Bus interface */
268         DEVMETHOD(bus_probe_nomatch,            bwn_pci_probe_nomatch),
269
270         /* BHNDB_BUS Interface */
271         DEVMETHOD(bhndb_bus_get_generic_hwcfg,  bwn_pci_get_generic_hwcfg),
272         DEVMETHOD(bhndb_bus_get_hardware_table, bwn_pci_get_bhndb_hwtable),
273         DEVMETHOD(bhndb_bus_is_core_disabled,   bwn_pci_is_core_disabled),
274
275         DEVMETHOD_END
276 };
277
278 static devclass_t bwn_pci_devclass;
279
280 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, sizeof(struct bwn_pci_softc));
281 DRIVER_MODULE(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL, NULL);
282 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL);
283
284 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
285 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
286 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
287 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
288 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);