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 int ndis_detach_pccard (device_t);
78 static struct resource_list *ndis_get_resource_list
80 static int ndis_devcompare (interface_type,
81 struct ndis_pccard_type *, device_t);
82 extern int ndisdrv_modevent (module_t, int, void *);
83 extern int ndis_attach (device_t);
84 extern int ndis_shutdown (device_t);
85 extern int ndis_detach (device_t);
86 extern int ndis_suspend (device_t);
87 extern int ndis_resume (device_t);
89 extern unsigned char drv_data[];
91 static device_method_t ndis_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, ndis_probe_pccard),
94 DEVMETHOD(device_attach, ndis_attach_pccard),
95 DEVMETHOD(device_detach, ndis_detach_pccard),
96 DEVMETHOD(device_shutdown, ndis_shutdown),
97 DEVMETHOD(device_suspend, ndis_suspend),
98 DEVMETHOD(device_resume, ndis_resume),
103 * This is an awful kludge, but we need it becase pccard
104 * does not implement a bus_get_resource_list() method.
107 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
112 static driver_t ndis_driver = {
115 sizeof(struct ndis_softc)
118 static devclass_t ndis_devclass;
120 DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
123 ndis_devcompare(bustype, t, dev)
124 interface_type bustype;
125 struct ndis_pccard_type *t;
128 const char *prodstr, *vendstr;
131 if (bustype != PCMCIABus)
134 error = pccard_get_product_str(dev, &prodstr);
137 error = pccard_get_vendor_str(dev, &vendstr);
141 while(t->ndis_name != NULL) {
142 if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
143 strcasecmp(prodstr, t->ndis_did) == 0) {
144 device_set_desc(dev, t->ndis_name);
154 * Probe for an NDIS device. Check the PCI vendor and device
155 * IDs against our list and return a device name if we find a match.
158 ndis_probe_pccard(dev)
162 struct drvdb_ent *db;
164 drv = windrv_lookup(0, "PCCARD Bus");
168 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
171 /* Create PDO for this device instance */
172 windrv_create_pdo(drv, dev);
179 #define NDIS_AM_RID 3
182 ndis_alloc_amem(struct ndis_softc *sc)
187 sc->ndis_res_am = bus_alloc_resource_anywhere(sc->ndis_dev,
188 SYS_RES_MEMORY, &rid, 0x1000, RF_ACTIVE);
190 if (sc->ndis_res_am == NULL) {
191 device_printf(sc->ndis_dev,
192 "failed to allocate attribute memory\n");
196 resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
197 rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
198 rman_get_size(sc->ndis_res_am));
200 error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
201 sc->ndis_dev, rid, 0, NULL);
204 device_printf(sc->ndis_dev,
205 "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
209 error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
210 sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
213 device_printf(sc->ndis_dev,
214 "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
218 sc->ndis_am_rid = rid;
224 * Attach the interface. Allocate softc structures, do ifmedia
225 * setup and ethernet/BPF attach.
228 ndis_attach_pccard(dev)
231 struct ndis_softc *sc;
232 int unit, error = 0, rid;
233 struct ndis_pccard_type *t;
235 const char *prodstr, *vendstr;
236 struct drvdb_ent *db;
238 sc = device_get_softc(dev);
239 unit = device_get_unit(dev);
242 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
245 sc->ndis_dobj = db->windrv_object;
246 sc->ndis_regvals = db->windrv_regvals;
247 resource_list_init(&sc->ndis_rl);
250 sc->ndis_res_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
251 &sc->ndis_io_rid, RF_ACTIVE);
252 if (sc->ndis_res_io == NULL) {
254 "couldn't map iospace\n");
259 resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, sc->ndis_io_rid,
260 rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
261 rman_get_size(sc->ndis_res_io));
264 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
265 RF_SHAREABLE | RF_ACTIVE);
266 if (sc->ndis_irq == NULL) {
268 "couldn't map interrupt\n");
273 resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
274 rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
276 sc->ndis_iftype = PCMCIABus;
278 /* Figure out exactly which device we matched. */
280 t = db->windrv_devlist;
282 error = pccard_get_product_str(dev, &prodstr);
285 error = pccard_get_vendor_str(dev, &vendstr);
289 while(t->ndis_name != NULL) {
290 if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
291 strcasecmp(prodstr, t->ndis_did) == 0)
297 sc->ndis_devidx = devidx;
299 error = ndis_alloc_amem(sc);
301 device_printf(dev, "failed to allocate attribute memory\n");
305 error = ndis_attach(dev);
312 ndis_detach_pccard(device_t dev)
314 struct ndis_softc *sc = device_get_softc(dev);
316 (void) ndis_detach(dev);
318 if (sc->ndis_res_am != NULL)
319 bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
320 sc->ndis_am_rid, sc->ndis_res_am);
321 resource_list_free(&sc->ndis_rl);
326 static struct resource_list *
327 ndis_get_resource_list(dev, child)
331 struct ndis_softc *sc;
333 sc = device_get_softc(dev);
334 return (&sc->ndis_rl);