kernel: Use NULL for pointers in DRIVER_MODULE().
[dragonfly.git] / sys / dev / netif / ndis / if_ndis_pci.c
blob2e15a5f3a8fb19fd3b20922649ba8076960bab3a
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_pci.c 292411 2015-12-17 21:01:19Z glebius $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.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 <netproto/802_11/ieee80211_var.h>
60 #else
61 #include <net80211/ieee80211_var.h>
62 #endif
64 #if defined(__DragonFly__)
65 #include <bus/pci/pcireg.h>
66 #include <bus/pci/pcivar.h>
67 #include <bus/u4b/usb.h>
68 #include <bus/u4b/usbdi.h>
69 #else
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #endif
76 #if defined(__DragonFly__)
77 #include <emulation/ndis/pe_var.h>
78 #include <emulation/ndis/cfg_var.h>
79 #include <emulation/ndis/resource_var.h>
80 #include <emulation/ndis/ntoskrnl_var.h>
81 #include <emulation/ndis/ndis_var.h>
82 #include <dev/netif/ndis/if_ndisvar.h>
83 #else
84 #include <compat/ndis/pe_var.h>
85 #include <compat/ndis/cfg_var.h>
86 #include <compat/ndis/resource_var.h>
87 #include <compat/ndis/ntoskrnl_var.h>
88 #include <compat/ndis/ndis_var.h>
89 #include <dev/if_ndis/if_ndisvar.h>
90 #endif
92 MODULE_DEPEND(if_ndis, pci, 1, 1, 1);
94 static int ndis_probe_pci (device_t);
95 static int ndis_attach_pci (device_t);
96 static struct resource_list *ndis_get_resource_list
97 (device_t, device_t);
98 static int ndis_devcompare (interface_type,
99 struct ndis_pci_type *, device_t);
100 extern int ndisdrv_modevent (module_t, int, void *);
101 extern int ndis_attach (device_t);
102 extern int ndis_shutdown (device_t);
103 extern int ndis_detach (device_t);
104 extern int ndis_suspend (device_t);
105 extern int ndis_resume (device_t);
107 static device_method_t ndis_methods[] = {
108 /* Device interface */
109 DEVMETHOD(device_probe, ndis_probe_pci),
110 DEVMETHOD(device_attach, ndis_attach_pci),
111 DEVMETHOD(device_detach, ndis_detach),
112 DEVMETHOD(device_shutdown, ndis_shutdown),
113 DEVMETHOD(device_suspend, ndis_suspend),
114 DEVMETHOD(device_resume, ndis_resume),
116 /* Bus interface */
117 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
119 DEVMETHOD_END
122 static driver_t ndis_driver = {
123 "ndis",
124 ndis_methods,
125 sizeof(struct ndis_softc)
128 static devclass_t ndis_devclass;
130 DRIVER_MODULE(if_ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent,
131 NULL);
133 static int
134 ndis_devcompare(interface_type bustype, struct ndis_pci_type *t, device_t dev)
136 uint16_t vid, did;
137 uint32_t subsys;
139 if (bustype != PCIBus)
140 return(FALSE);
142 vid = pci_get_vendor(dev);
143 did = pci_get_device(dev);
144 subsys = pci_get_subdevice(dev);
145 subsys = (subsys << 16) | pci_get_subvendor(dev);
147 while(t->ndis_name != NULL) {
148 if ((t->ndis_vid == vid) && (t->ndis_did == did) &&
149 (t->ndis_subsys == subsys || t->ndis_subsys == 0)) {
150 device_set_desc(dev, t->ndis_name);
151 return(TRUE);
153 t++;
156 return(FALSE);
160 * Probe for an NDIS device. Check the PCI vendor and device
161 * IDs against our list and return a device name if we find a match.
163 static int
164 ndis_probe_pci(device_t dev)
166 driver_object *drv;
167 struct drvdb_ent *db;
169 drv = windrv_lookup(0, "PCI Bus");
171 if (drv == NULL)
172 return(ENXIO);
174 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
176 if (db != NULL) {
177 /* Create PDO for this device instance */
178 windrv_create_pdo(drv, dev);
179 return(0);
182 return(ENXIO);
186 * Attach the interface. Allocate softc structures, do ifmedia
187 * setup and ethernet/BPF attach.
189 static int
190 ndis_attach_pci(device_t dev)
192 struct ndis_softc *sc;
193 int unit, error = 0, rid;
194 struct ndis_pci_type *t;
195 int devidx = 0, defidx = 0;
196 struct resource_list *rl;
197 struct resource_list_entry *rle;
198 struct drvdb_ent *db;
199 uint16_t vid, did;
200 uint32_t subsys;
202 sc = device_get_softc(dev);
203 unit = device_get_unit(dev);
204 sc->ndis_dev = dev;
206 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
207 if (db == NULL)
208 return (ENXIO);
209 sc->ndis_dobj = db->windrv_object;
210 sc->ndis_regvals = db->windrv_regvals;
213 * Map control/status registers.
216 pci_enable_busmaster(dev);
218 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
219 if (rl != NULL) {
220 SLIST_FOREACH(rle, rl, link) {
221 switch (rle->type) {
222 case SYS_RES_IOPORT:
223 sc->ndis_io_rid = rle->rid;
224 sc->ndis_res_io = bus_alloc_resource_any(dev,
225 SYS_RES_IOPORT, &sc->ndis_io_rid,
226 RF_ACTIVE);
227 if (sc->ndis_res_io == NULL) {
228 device_printf(dev,
229 "couldn't map iospace\n");
230 error = ENXIO;
231 goto fail;
233 break;
234 case SYS_RES_MEMORY:
235 if (sc->ndis_res_altmem != NULL &&
236 sc->ndis_res_mem != NULL) {
237 device_printf(dev,
238 "too many memory resources\n");
239 error = ENXIO;
240 goto fail;
242 if (sc->ndis_res_mem) {
243 sc->ndis_altmem_rid = rle->rid;
244 sc->ndis_res_altmem =
245 bus_alloc_resource_any(dev,
246 SYS_RES_MEMORY,
247 &sc->ndis_altmem_rid,
248 RF_ACTIVE);
249 if (sc->ndis_res_altmem == NULL) {
250 device_printf(dev,
251 "couldn't map alt "
252 "memory\n");
253 error = ENXIO;
254 goto fail;
256 } else {
257 sc->ndis_mem_rid = rle->rid;
258 sc->ndis_res_mem =
259 bus_alloc_resource_any(dev,
260 SYS_RES_MEMORY,
261 &sc->ndis_mem_rid,
262 RF_ACTIVE);
263 if (sc->ndis_res_mem == NULL) {
264 device_printf(dev,
265 "couldn't map memory\n");
266 error = ENXIO;
267 goto fail;
270 break;
271 case SYS_RES_IRQ:
272 rid = rle->rid;
273 sc->ndis_irq = bus_alloc_resource_any(dev,
274 SYS_RES_IRQ, &rid,
275 RF_SHAREABLE | RF_ACTIVE);
276 if (sc->ndis_irq == NULL) {
277 device_printf(dev,
278 "couldn't map interrupt\n");
279 error = ENXIO;
280 goto fail;
282 break;
283 default:
284 break;
286 sc->ndis_rescnt++;
291 * If the BIOS did not set up an interrupt for this device,
292 * the resource traversal code above will fail to set up
293 * an IRQ resource. This is usually a bad thing, so try to
294 * force the allocation of an interrupt here. If one was
295 * not assigned to us by the BIOS, bus_alloc_resource()
296 * should route one for us.
298 if (sc->ndis_irq == NULL) {
299 rid = 0;
300 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
301 &rid, RF_SHAREABLE | RF_ACTIVE);
302 if (sc->ndis_irq == NULL) {
303 device_printf(dev, "couldn't route interrupt\n");
304 error = ENXIO;
305 goto fail;
307 sc->ndis_rescnt++;
311 * Allocate the parent bus DMA tag appropriate for PCI.
313 #define NDIS_NSEG_NEW 32
314 error = bus_dma_tag_create(NULL, /* parent */
315 1, 0, /* alignment, boundary */
316 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
317 BUS_SPACE_MAXADDR, /* highaddr */
318 NULL, NULL, /* filter, filterarg */
319 DFLTPHYS, NDIS_NSEG_NEW,/* maxsize, nsegments */
320 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
321 BUS_DMA_ALLOCNOW, /* flags */
322 &sc->ndis_parent_tag);
324 if (error)
325 goto fail;
327 sc->ndis_iftype = PCIBus;
329 /* Figure out exactly which device we matched. */
331 vid = pci_get_vendor(dev);
332 did = pci_get_device(dev);
333 subsys = pci_get_subdevice(dev);
334 subsys = (subsys << 16) | pci_get_subvendor(dev);
336 t = db->windrv_devlist;
338 while(t->ndis_name != NULL) {
339 if (t->ndis_vid == vid && t->ndis_did == did) {
340 if (t->ndis_subsys == 0)
341 defidx = devidx;
342 else if (t->ndis_subsys == subsys)
343 break;
345 t++;
346 devidx++;
349 if (t->ndis_name == NULL)
350 sc->ndis_devidx = defidx;
351 else
352 sc->ndis_devidx = devidx;
354 error = ndis_attach(dev);
356 fail:
357 return(error);
360 static struct resource_list *
361 ndis_get_resource_list(device_t dev, device_t child)
363 struct ndis_softc *sc;
365 sc = device_get_softc(dev);
366 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));