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/ctype.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.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 <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
61 #include <net80211/ieee80211_var.h>
63 #include <compat/ndis/pe_var.h>
64 #include <compat/ndis/cfg_var.h>
65 #include <compat/ndis/resource_var.h>
66 #include <compat/ndis/ntoskrnl_var.h>
67 #include <compat/ndis/ndis_var.h>
68 #include <dev/if_ndis/if_ndisvar.h>
70 #include <dev/pccard/pccardvar.h>
73 MODULE_DEPEND(ndis, pccard, 1, 1, 1);
75 static int ndis_probe_pccard (device_t);
76 static int ndis_attach_pccard (device_t);
77 static struct resource_list *ndis_get_resource_list
79 static int ndis_devcompare (interface_type,
80 struct ndis_pccard_type *, device_t);
81 extern int ndisdrv_modevent (module_t, int, void *);
82 extern int ndis_attach (device_t);
83 extern int ndis_shutdown (device_t);
84 extern int ndis_detach (device_t);
85 extern int ndis_suspend (device_t);
86 extern int ndis_resume (device_t);
88 extern unsigned char drv_data[];
90 static device_method_t ndis_methods[] = {
91 /* Device interface */
92 DEVMETHOD(device_probe, ndis_probe_pccard),
93 DEVMETHOD(device_attach, ndis_attach_pccard),
94 DEVMETHOD(device_detach, ndis_detach),
95 DEVMETHOD(device_shutdown, ndis_shutdown),
96 DEVMETHOD(device_suspend, ndis_suspend),
97 DEVMETHOD(device_resume, ndis_resume),
102 * This is an awful kludge, but we need it becase pccard
103 * does not implement a bus_get_resource_list() method.
106 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
111 static driver_t ndis_driver = {
114 sizeof(struct ndis_softc)
117 static devclass_t ndis_devclass;
119 DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
122 ndis_devcompare(bustype, t, dev)
123 interface_type bustype;
124 struct ndis_pccard_type *t;
127 const char *prodstr, *vendstr;
130 if (bustype != PCMCIABus)
133 error = pccard_get_product_str(dev, &prodstr);
136 error = pccard_get_vendor_str(dev, &vendstr);
140 while(t->ndis_name != NULL) {
141 if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
142 strcasecmp(prodstr, t->ndis_did) == 0) {
143 device_set_desc(dev, t->ndis_name);
153 * Probe for an NDIS device. Check the PCI vendor and device
154 * IDs against our list and return a device name if we find a match.
157 ndis_probe_pccard(dev)
161 struct drvdb_ent *db;
163 drv = windrv_lookup(0, "PCCARD Bus");
167 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
170 /* Create PDO for this device instance */
171 windrv_create_pdo(drv, dev);
179 * Attach the interface. Allocate softc structures, do ifmedia
180 * setup and ethernet/BPF attach.
183 ndis_attach_pccard(dev)
186 struct ndis_softc *sc;
187 int unit, error = 0, rid;
188 struct ndis_pccard_type *t;
190 const char *prodstr, *vendstr;
191 struct drvdb_ent *db;
193 sc = device_get_softc(dev);
194 unit = device_get_unit(dev);
197 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
200 sc->ndis_dobj = db->windrv_object;
201 sc->ndis_regvals = db->windrv_regvals;
202 resource_list_init(&sc->ndis_rl);
205 sc->ndis_res_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
206 &sc->ndis_io_rid, RF_ACTIVE);
207 if (sc->ndis_res_io == NULL) {
209 "couldn't map iospace\n");
214 resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, sc->ndis_io_rid,
215 rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
216 rman_get_size(sc->ndis_res_io));
219 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
220 RF_SHAREABLE | RF_ACTIVE);
221 if (sc->ndis_irq == NULL) {
223 "couldn't map interrupt\n");
228 resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
229 rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
231 sc->ndis_iftype = PCMCIABus;
233 /* Figure out exactly which device we matched. */
235 t = db->windrv_devlist;
237 error = pccard_get_product_str(dev, &prodstr);
240 error = pccard_get_vendor_str(dev, &vendstr);
244 while(t->ndis_name != NULL) {
245 if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
246 strcasecmp(prodstr, t->ndis_did) == 0)
252 sc->ndis_devidx = devidx;
254 error = ndis_attach(dev);
260 static struct resource_list *
261 ndis_get_resource_list(dev, child)
265 struct ndis_softc *sc;
267 sc = device_get_softc(dev);
268 return (&sc->ndis_rl);
271 #define NDIS_AM_RID 3
277 struct ndis_softc *sc;
285 sc->ndis_res_am = bus_alloc_resource_anywhere(sc->ndis_dev,
286 SYS_RES_MEMORY, &rid, 0x1000, RF_ACTIVE);
288 if (sc->ndis_res_am == NULL) {
289 device_printf(sc->ndis_dev,
290 "failed to allocate attribute memory\n");
294 resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
295 rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
296 rman_get_size(sc->ndis_res_am));
298 error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
299 sc->ndis_dev, rid, 0, NULL);
302 device_printf(sc->ndis_dev,
303 "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
307 error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
308 sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
311 device_printf(sc->ndis_dev,
312 "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
316 sc->ndis_am_rid = rid;
325 struct ndis_softc *sc;
332 if (sc->ndis_res_am != NULL)
333 bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
334 sc->ndis_am_rid, sc->ndis_res_am);
335 resource_list_free(&sc->ndis_rl);