2 * SPDX-License-Identifier: BSD-4-Clause
5 * Bill Paul <wpaul@windriver.com>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/socket.h>
44 #include <sys/queue.h>
45 #include <sys/sysctl.h>
48 #include <net/if_var.h>
49 #include <net/if_arp.h>
50 #include <net/if_media.h>
51 #include <net/ethernet.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
58 #include <net80211/ieee80211_var.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
65 #include <compat/ndis/pe_var.h>
66 #include <compat/ndis/cfg_var.h>
67 #include <compat/ndis/resource_var.h>
68 #include <compat/ndis/ntoskrnl_var.h>
69 #include <compat/ndis/ndis_var.h>
70 #include <dev/if_ndis/if_ndisvar.h>
72 MODULE_DEPEND(ndis, pci, 1, 1, 1);
74 static int ndis_probe_pci (device_t);
75 static int ndis_attach_pci (device_t);
76 static struct resource_list *ndis_get_resource_list
78 static int ndis_devcompare (interface_type,
79 struct ndis_pci_type *, device_t);
80 extern int ndisdrv_modevent (module_t, int, void *);
81 extern int ndis_attach (device_t);
82 extern int ndis_shutdown (device_t);
83 extern int ndis_detach (device_t);
84 extern int ndis_suspend (device_t);
85 extern int ndis_resume (device_t);
87 static device_method_t ndis_methods[] = {
88 /* Device interface */
89 DEVMETHOD(device_probe, ndis_probe_pci),
90 DEVMETHOD(device_attach, ndis_attach_pci),
91 DEVMETHOD(device_detach, ndis_detach),
92 DEVMETHOD(device_shutdown, ndis_shutdown),
93 DEVMETHOD(device_suspend, ndis_suspend),
94 DEVMETHOD(device_resume, ndis_resume),
97 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
102 static driver_t ndis_driver = {
105 sizeof(struct ndis_softc)
108 static devclass_t ndis_devclass;
110 DRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
113 ndis_devcompare(bustype, t, dev)
114 interface_type bustype;
115 struct ndis_pci_type *t;
121 if (bustype != PCIBus)
124 vid = pci_get_vendor(dev);
125 did = pci_get_device(dev);
126 subsys = pci_get_subdevice(dev);
127 subsys = (subsys << 16) | pci_get_subvendor(dev);
129 while(t->ndis_name != NULL) {
130 if ((t->ndis_vid == vid) && (t->ndis_did == did) &&
131 (t->ndis_subsys == subsys || t->ndis_subsys == 0)) {
132 device_set_desc(dev, t->ndis_name);
142 * Probe for an NDIS device. Check the PCI vendor and device
143 * IDs against our list and return a device name if we find a match.
150 struct drvdb_ent *db;
152 drv = windrv_lookup(0, "PCI Bus");
157 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
160 /* Create PDO for this device instance */
161 windrv_create_pdo(drv, dev);
169 * Attach the interface. Allocate softc structures, do ifmedia
170 * setup and ethernet/BPF attach.
176 struct ndis_softc *sc;
177 int unit, error = 0, rid;
178 struct ndis_pci_type *t;
179 int devidx = 0, defidx = 0;
180 struct resource_list *rl;
181 struct resource_list_entry *rle;
182 struct drvdb_ent *db;
186 sc = device_get_softc(dev);
187 unit = device_get_unit(dev);
190 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
193 sc->ndis_dobj = db->windrv_object;
194 sc->ndis_regvals = db->windrv_regvals;
197 * Map control/status registers.
200 pci_enable_busmaster(dev);
202 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
204 STAILQ_FOREACH(rle, rl, link) {
207 sc->ndis_io_rid = rle->rid;
208 sc->ndis_res_io = bus_alloc_resource_any(dev,
209 SYS_RES_IOPORT, &sc->ndis_io_rid,
211 if (sc->ndis_res_io == NULL) {
213 "couldn't map iospace\n");
219 if (sc->ndis_res_altmem != NULL &&
220 sc->ndis_res_mem != NULL) {
222 "too many memory resources\n");
226 if (sc->ndis_res_mem) {
227 sc->ndis_altmem_rid = rle->rid;
228 sc->ndis_res_altmem =
229 bus_alloc_resource_any(dev,
231 &sc->ndis_altmem_rid,
233 if (sc->ndis_res_altmem == NULL) {
241 sc->ndis_mem_rid = rle->rid;
243 bus_alloc_resource_any(dev,
247 if (sc->ndis_res_mem == NULL) {
249 "couldn't map memory\n");
257 sc->ndis_irq = bus_alloc_resource_any(dev,
259 RF_SHAREABLE | RF_ACTIVE);
260 if (sc->ndis_irq == NULL) {
262 "couldn't map interrupt\n");
275 * If the BIOS did not set up an interrupt for this device,
276 * the resource traversal code above will fail to set up
277 * an IRQ resource. This is usually a bad thing, so try to
278 * force the allocation of an interrupt here. If one was
279 * not assigned to us by the BIOS, bus_alloc_resource()
280 * should route one for us.
282 if (sc->ndis_irq == NULL) {
284 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
285 &rid, RF_SHAREABLE | RF_ACTIVE);
286 if (sc->ndis_irq == NULL) {
287 device_printf(dev, "couldn't route interrupt\n");
295 * Allocate the parent bus DMA tag appropriate for PCI.
297 #define NDIS_NSEG_NEW 32
298 error = bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
299 1, 0, /* alignment, boundary */
300 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
301 BUS_SPACE_MAXADDR, /* highaddr */
302 NULL, NULL, /* filter, filterarg */
303 DFLTPHYS, NDIS_NSEG_NEW,/* maxsize, nsegments */
304 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
305 BUS_DMA_ALLOCNOW, /* flags */
306 NULL, NULL, /* lockfunc, lockarg */
307 &sc->ndis_parent_tag);
312 sc->ndis_iftype = PCIBus;
314 /* Figure out exactly which device we matched. */
316 vid = pci_get_vendor(dev);
317 did = pci_get_device(dev);
318 subsys = pci_get_subdevice(dev);
319 subsys = (subsys << 16) | pci_get_subvendor(dev);
321 t = db->windrv_devlist;
323 while(t->ndis_name != NULL) {
324 if (t->ndis_vid == vid && t->ndis_did == did) {
325 if (t->ndis_subsys == 0)
327 else if (t->ndis_subsys == subsys)
334 if (t->ndis_name == NULL)
335 sc->ndis_devidx = defidx;
337 sc->ndis_devidx = devidx;
339 error = ndis_attach(dev);
345 static struct resource_list *
346 ndis_get_resource_list(dev, child)
350 struct ndis_softc *sc;
352 sc = device_get_softc(dev);
353 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));