3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/queue.h>
42 #include <sys/sysctl.h>
45 #include <net/if_arp.h>
46 #include <net/if_media.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
53 #include <net80211/ieee80211_var.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
60 #include <compat/ndis/pe_var.h>
61 #include <compat/ndis/cfg_var.h>
62 #include <compat/ndis/resource_var.h>
63 #include <compat/ndis/ntoskrnl_var.h>
64 #include <compat/ndis/ndis_var.h>
65 #include <dev/if_ndis/if_ndisvar.h>
67 MODULE_DEPEND(ndis, pci, 1, 1, 1);
69 static int ndis_probe_pci (device_t);
70 static int ndis_attach_pci (device_t);
71 static struct resource_list *ndis_get_resource_list
73 static int ndis_devcompare (interface_type,
74 struct ndis_pci_type *, device_t);
75 extern int ndisdrv_modevent (module_t, int, void *);
76 extern int ndis_attach (device_t);
77 extern int ndis_shutdown (device_t);
78 extern int ndis_detach (device_t);
79 extern int ndis_suspend (device_t);
80 extern int ndis_resume (device_t);
82 static device_method_t ndis_methods[] = {
83 /* Device interface */
84 DEVMETHOD(device_probe, ndis_probe_pci),
85 DEVMETHOD(device_attach, ndis_attach_pci),
86 DEVMETHOD(device_detach, ndis_detach),
87 DEVMETHOD(device_shutdown, ndis_shutdown),
88 DEVMETHOD(device_suspend, ndis_suspend),
89 DEVMETHOD(device_resume, ndis_resume),
92 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
97 static driver_t ndis_driver = {
100 sizeof(struct ndis_softc)
103 static devclass_t ndis_devclass;
105 DRIVER_MODULE(ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
108 ndis_devcompare(bustype, t, dev)
109 interface_type bustype;
110 struct ndis_pci_type *t;
116 if (bustype != PCIBus)
119 vid = pci_get_vendor(dev);
120 did = pci_get_device(dev);
121 subsys = pci_get_subdevice(dev);
122 subsys = (subsys << 16) | pci_get_subvendor(dev);
124 while(t->ndis_name != NULL) {
125 if ((t->ndis_vid == vid) && (t->ndis_did == did) &&
126 (t->ndis_subsys == subsys || t->ndis_subsys == 0)) {
127 device_set_desc(dev, t->ndis_name);
137 * Probe for an NDIS device. Check the PCI vendor and device
138 * IDs against our list and return a device name if we find a match.
145 struct drvdb_ent *db;
147 drv = windrv_lookup(0, "PCI Bus");
152 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
155 /* Create PDO for this device instance */
156 windrv_create_pdo(drv, dev);
164 * Attach the interface. Allocate softc structures, do ifmedia
165 * setup and ethernet/BPF attach.
171 struct ndis_softc *sc;
172 int unit, error = 0, rid;
173 struct ndis_pci_type *t;
174 int devidx = 0, defidx = 0;
175 struct resource_list *rl;
176 struct resource_list_entry *rle;
177 struct drvdb_ent *db;
181 sc = device_get_softc(dev);
182 unit = device_get_unit(dev);
185 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
188 sc->ndis_dobj = db->windrv_object;
189 sc->ndis_regvals = db->windrv_regvals;
192 * Map control/status registers.
195 pci_enable_busmaster(dev);
197 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
199 STAILQ_FOREACH(rle, rl, link) {
202 sc->ndis_io_rid = rle->rid;
203 sc->ndis_res_io = bus_alloc_resource_any(dev,
204 SYS_RES_IOPORT, &sc->ndis_io_rid,
206 if (sc->ndis_res_io == NULL) {
208 "couldn't map iospace\n");
214 if (sc->ndis_res_altmem != NULL &&
215 sc->ndis_res_mem != NULL) {
217 "too many memory resources\n");
221 if (sc->ndis_res_mem) {
222 sc->ndis_altmem_rid = rle->rid;
223 sc->ndis_res_altmem =
224 bus_alloc_resource_any(dev,
226 &sc->ndis_altmem_rid,
228 if (sc->ndis_res_altmem == NULL) {
236 sc->ndis_mem_rid = rle->rid;
238 bus_alloc_resource_any(dev,
242 if (sc->ndis_res_mem == NULL) {
244 "couldn't map memory\n");
252 sc->ndis_irq = bus_alloc_resource_any(dev,
254 RF_SHAREABLE | RF_ACTIVE);
255 if (sc->ndis_irq == NULL) {
257 "couldn't map interrupt\n");
270 * If the BIOS did not set up an interrupt for this device,
271 * the resource traversal code above will fail to set up
272 * an IRQ resource. This is usually a bad thing, so try to
273 * force the allocation of an interrupt here. If one was
274 * not assigned to us by the BIOS, bus_alloc_resource()
275 * should route one for us.
277 if (sc->ndis_irq == NULL) {
279 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
280 &rid, RF_SHAREABLE | RF_ACTIVE);
281 if (sc->ndis_irq == NULL) {
282 device_printf(dev, "couldn't route interrupt\n");
290 * Allocate the parent bus DMA tag appropriate for PCI.
292 #define NDIS_NSEG_NEW 32
293 error = bus_dma_tag_create(bus_get_dma_tag(dev),/* PCI parent */
294 1, 0, /* alignment, boundary */
295 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
296 BUS_SPACE_MAXADDR, /* highaddr */
297 NULL, NULL, /* filter, filterarg */
298 MAXBSIZE, NDIS_NSEG_NEW,/* maxsize, nsegments */
299 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
300 BUS_DMA_ALLOCNOW, /* flags */
301 NULL, NULL, /* lockfunc, lockarg */
302 &sc->ndis_parent_tag);
307 sc->ndis_iftype = PCIBus;
309 /* Figure out exactly which device we matched. */
311 vid = pci_get_vendor(dev);
312 did = pci_get_device(dev);
313 subsys = pci_get_subdevice(dev);
314 subsys = (subsys << 16) | pci_get_subvendor(dev);
316 t = db->windrv_devlist;
318 while(t->ndis_name != NULL) {
319 if (t->ndis_vid == vid && t->ndis_did == did) {
320 if (t->ndis_subsys == 0)
322 else if (t->ndis_subsys == subsys)
329 if (t->ndis_name == NULL)
330 sc->ndis_devidx = defidx;
332 sc->ndis_devidx = devidx;
334 error = ndis_attach(dev);
340 static struct resource_list *
341 ndis_get_resource_list(dev, child)
345 struct ndis_softc *sc;
347 sc = device_get_softc(dev);
348 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));