kernel: Use NULL for pointers in DRIVER_MODULE().
[dragonfly.git] / sys / dev / netif / ndis / if_ndis_pccard.c
blob5ebab77d6fa2a5ef7963b621a3cfa0d0a8a90e43
1 /*-
2 * Copyright (c) 2003
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
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 * 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.
32 * $FreeBSD: head/sys/dev/if_ndis/if_ndis_pccard.c 296137 2016-02-27 03:38:01Z jhibbits $
35 #include <sys/ctype.h>
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>
43 #include <sys/lock.h>
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_arp.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
51 #if !defined(__DragonFly__)
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #endif
55 #include <sys/bus.h>
56 #include <sys/rman.h>
58 #if defined(__DragonFly__)
59 #include <bus/u4b/usb.h>
60 #include <bus/u4b/usbdi.h>
61 #else
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #endif
66 #if defined(__DragonFly__)
67 #include <netproto/802_11/ieee80211_var.h>
68 #else
69 #include <net80211/ieee80211_var.h>
70 #endif
72 #if defined(__DragonFly__)
73 #include <emulation/ndis/pe_var.h>
74 #include <emulation/ndis/cfg_var.h>
75 #include <emulation/ndis/resource_var.h>
76 #include <emulation/ndis/ntoskrnl_var.h>
77 #include <emulation/ndis/ndis_var.h>
78 #include <dev/netif/ndis/if_ndisvar.h>
79 #else
80 #include <compat/ndis/pe_var.h>
81 #include <compat/ndis/cfg_var.h>
82 #include <compat/ndis/resource_var.h>
83 #include <compat/ndis/ntoskrnl_var.h>
84 #include <compat/ndis/ndis_var.h>
85 #include <dev/if_ndis/if_ndisvar.h>
86 #endif
88 #if defined(__DragonFly__)
89 #include <bus/pccard/pccardvar.h>
90 #else
91 #include <dev/pccard/pccardvar.h>
92 #endif
93 #include "card_if.h"
95 MODULE_DEPEND(if_ndis, pccard, 1, 1, 1);
97 static int ndis_probe_pccard (device_t);
98 static int ndis_attach_pccard (device_t);
99 static struct resource_list *ndis_get_resource_list
100 (device_t, device_t);
101 static int ndis_devcompare (interface_type,
102 struct ndis_pccard_type *, device_t);
103 extern int ndisdrv_modevent (module_t, int, void *);
104 extern int ndis_attach (device_t);
105 extern int ndis_shutdown (device_t);
106 extern int ndis_detach (device_t);
107 extern int ndis_suspend (device_t);
108 extern int ndis_resume (device_t);
110 extern unsigned char drv_data[];
112 static device_method_t ndis_methods[] = {
113 /* Device interface */
114 DEVMETHOD(device_probe, ndis_probe_pccard),
115 DEVMETHOD(device_attach, ndis_attach_pccard),
116 DEVMETHOD(device_detach, ndis_detach),
117 DEVMETHOD(device_shutdown, ndis_shutdown),
118 DEVMETHOD(device_suspend, ndis_suspend),
119 DEVMETHOD(device_resume, ndis_resume),
121 /* Bus interface. */
124 * This is an awful kludge, but we need it becase pccard
125 * does not implement a bus_get_resource_list() method.
128 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
130 DEVMETHOD_END
133 static driver_t ndis_driver = {
134 "ndis",
135 ndis_methods,
136 sizeof(struct ndis_softc)
139 static devclass_t ndis_devclass;
141 DRIVER_MODULE(if_ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, NULL);
143 static int
144 ndis_devcompare(interface_type bustype, struct ndis_pccard_type *t,
145 device_t dev)
147 const char *prodstr, *vendstr;
148 #if !defined(__DragonFly__)
149 int error;
150 #endif
152 if (bustype != PCMCIABus)
153 return(FALSE);
155 #if defined(__DragonFly__)
156 prodstr = pccard_get_product_str(dev);
157 vendstr = pccard_get_vendor_str(dev);
158 #else
159 error = pccard_get_product_str(dev, &prodstr);
160 if (error)
161 return(FALSE);
162 error = pccard_get_vendor_str(dev, &vendstr);
163 if (error)
164 return(FALSE);
165 #endif
167 while(t->ndis_name != NULL) {
168 if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
169 strcasecmp(prodstr, t->ndis_did) == 0) {
170 device_set_desc(dev, t->ndis_name);
171 return(TRUE);
173 t++;
176 return(FALSE);
180 * Probe for an NDIS device. Check the PCI vendor and device
181 * IDs against our list and return a device name if we find a match.
183 static int
184 ndis_probe_pccard(device_t dev)
186 driver_object *drv;
187 struct drvdb_ent *db;
189 drv = windrv_lookup(0, "PCCARD Bus");
190 if (drv == NULL)
191 return(ENXIO);
193 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
195 if (db != NULL) {
196 /* Create PDO for this device instance */
197 windrv_create_pdo(drv, dev);
198 return(0);
201 return(ENXIO);
205 * Attach the interface. Allocate softc structures, do ifmedia
206 * setup and ethernet/BPF attach.
208 static int
209 ndis_attach_pccard(device_t dev)
211 struct ndis_softc *sc;
212 int unit, error = 0, rid;
213 struct ndis_pccard_type *t;
214 int devidx = 0;
215 const char *prodstr, *vendstr;
216 struct drvdb_ent *db;
218 sc = device_get_softc(dev);
219 unit = device_get_unit(dev);
220 sc->ndis_dev = dev;
222 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
223 if (db == NULL)
224 return (ENXIO);
225 sc->ndis_dobj = db->windrv_object;
226 sc->ndis_regvals = db->windrv_regvals;
227 resource_list_init(&sc->ndis_rl);
229 sc->ndis_io_rid = 0;
230 sc->ndis_res_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
231 &sc->ndis_io_rid, RF_ACTIVE);
232 if (sc->ndis_res_io == NULL) {
233 device_printf(dev,
234 "couldn't map iospace\n");
235 error = ENXIO;
236 goto fail;
238 sc->ndis_rescnt++;
239 #if defined(__DragonFly__)
240 resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, sc->ndis_io_rid,
241 rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
242 rman_get_size(sc->ndis_res_io), -1);
243 #else
244 resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, sc->ndis_io_rid,
245 rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
246 rman_get_size(sc->ndis_res_io));
247 #endif
249 rid = 0;
250 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
251 RF_SHAREABLE | RF_ACTIVE);
252 if (sc->ndis_irq == NULL) {
253 device_printf(dev,
254 "couldn't map interrupt\n");
255 error = ENXIO;
256 goto fail;
258 sc->ndis_rescnt++;
259 #if defined(__DragonFly__)
260 resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
261 rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1,
262 rman_get_cpuid(sc->ndis_irq));
263 #else
264 resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
265 rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
266 #endif
268 sc->ndis_iftype = PCMCIABus;
270 /* Figure out exactly which device we matched. */
272 t = db->windrv_devlist;
274 #if defined(__DragonFly__)
275 prodstr = pccard_get_product_str(dev);
276 vendstr = pccard_get_vendor_str(dev);
277 #else
278 error = pccard_get_product_str(dev, &prodstr);
279 if (error)
280 return(error);
281 error = pccard_get_vendor_str(dev, &vendstr);
282 if (error)
283 return(error);
284 #endif
286 while(t->ndis_name != NULL) {
287 if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
288 strcasecmp(prodstr, t->ndis_did) == 0)
289 break;
290 t++;
291 devidx++;
294 sc->ndis_devidx = devidx;
296 error = ndis_attach(dev);
298 fail:
299 return(error);
302 static struct resource_list *
303 ndis_get_resource_list(device_t dev, device_t child)
305 struct ndis_softc *sc;
307 sc = device_get_softc(dev);
308 return (&sc->ndis_rl);
311 #define NDIS_AM_RID 3
314 ndis_alloc_amem(void *arg)
316 struct ndis_softc *sc;
317 int error, rid;
319 if (arg == NULL)
320 return(EINVAL);
322 sc = arg;
323 rid = NDIS_AM_RID;
324 #if defined(__DragonFly__)
325 sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY,
326 &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
327 #else
328 sc->ndis_res_am = bus_alloc_resource_anywhere(sc->ndis_dev,
329 SYS_RES_MEMORY, &rid, 0x1000, RF_ACTIVE);
330 #endif
332 if (sc->ndis_res_am == NULL) {
333 device_printf(sc->ndis_dev,
334 "failed to allocate attribute memory\n");
335 return(ENXIO);
337 sc->ndis_rescnt++;
338 #if defined(__DragonFly__)
339 resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
340 rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
341 rman_get_size(sc->ndis_res_am), -1);
342 #else
343 resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
344 rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
345 rman_get_size(sc->ndis_res_am));
346 #endif
348 error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
349 sc->ndis_dev, rid, 0, NULL);
351 if (error) {
352 device_printf(sc->ndis_dev,
353 "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
354 return(error);
357 error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
358 sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
360 if (error) {
361 device_printf(sc->ndis_dev,
362 "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
363 return(error);
366 sc->ndis_am_rid = rid;
368 return(0);
371 void
372 ndis_free_amem(void *arg)
374 struct ndis_softc *sc;
376 if (arg == NULL)
377 return;
379 sc = arg;
381 if (sc->ndis_res_am != NULL)
382 bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
383 sc->ndis_am_rid, sc->ndis_res_am);
384 resource_list_free(&sc->ndis_rl);
386 return;