kernel: Use NULL for pointers in DRIVER_MODULE().
[dragonfly.git] / sys / dev / netif / ndis / if_ndis_pci.c
CommitLineData
efba76b4 1/*-
60d6dac1
MD
2 * Copyright (c) 2003
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
4 *
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.
19 *
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.
31 *
34f8ce05 32 * $FreeBSD: head/sys/dev/if_ndis/if_ndis_pci.c 292411 2015-12-17 21:01:19Z glebius $
60d6dac1
MD
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
34f8ce05 38#include <sys/malloc.h>
60d6dac1
MD
39#include <sys/module.h>
40#include <sys/socket.h>
41#include <sys/queue.h>
42#include <sys/sysctl.h>
efba76b4 43#include <sys/lock.h>
60d6dac1
MD
44
45#include <net/if.h>
34f8ce05 46#include <net/if_var.h>
60d6dac1
MD
47#include <net/if_arp.h>
48#include <net/if_media.h>
34f8ce05 49#include <net/ethernet.h>
60d6dac1 50
34f8ce05
IV
51#if !defined(__DragonFly__)
52#include <machine/bus.h>
53#include <machine/resource.h>
54#endif
efba76b4
SW
55#include <sys/bus.h>
56#include <sys/rman.h>
57
34f8ce05 58#if defined(__DragonFly__)
60d6dac1 59#include <netproto/802_11/ieee80211_var.h>
34f8ce05
IV
60#else
61#include <net80211/ieee80211_var.h>
62#endif
60d6dac1 63
34f8ce05 64#if defined(__DragonFly__)
60d6dac1
MD
65#include <bus/pci/pcireg.h>
66#include <bus/pci/pcivar.h>
2e4157ef
SW
67#include <bus/u4b/usb.h>
68#include <bus/u4b/usbdi.h>
34f8ce05
IV
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
75
76#if defined(__DragonFly__)
60d6dac1 77#include <emulation/ndis/pe_var.h>
efba76b4 78#include <emulation/ndis/cfg_var.h>
60d6dac1
MD
79#include <emulation/ndis/resource_var.h>
80#include <emulation/ndis/ntoskrnl_var.h>
81#include <emulation/ndis/ndis_var.h>
efba76b4 82#include <dev/netif/ndis/if_ndisvar.h>
34f8ce05
IV
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
60d6dac1 91
efba76b4 92MODULE_DEPEND(if_ndis, pci, 1, 1, 1);
60d6dac1
MD
93
94static int ndis_probe_pci (device_t);
95static int ndis_attach_pci (device_t);
96static struct resource_list *ndis_get_resource_list
97 (device_t, device_t);
efba76b4
SW
98static int ndis_devcompare (interface_type,
99 struct ndis_pci_type *, device_t);
100extern int ndisdrv_modevent (module_t, int, void *);
60d6dac1
MD
101extern int ndis_attach (device_t);
102extern int ndis_shutdown (device_t);
103extern int ndis_detach (device_t);
104extern int ndis_suspend (device_t);
105extern int ndis_resume (device_t);
106
107static device_method_t ndis_methods[] = {
108 /* Device interface */
109 DEVMETHOD(device_probe, ndis_probe_pci),
110 DEVMETHOD(device_attach, ndis_attach_pci),
34f8ce05 111 DEVMETHOD(device_detach, ndis_detach),
60d6dac1
MD
112 DEVMETHOD(device_shutdown, ndis_shutdown),
113 DEVMETHOD(device_suspend, ndis_suspend),
114 DEVMETHOD(device_resume, ndis_resume),
115
116 /* Bus interface */
117 DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
118
d3c9c58e 119 DEVMETHOD_END
60d6dac1
MD
120};
121
122static driver_t ndis_driver = {
60d6dac1 123 "ndis",
60d6dac1
MD
124 ndis_methods,
125 sizeof(struct ndis_softc)
126};
127
128static devclass_t ndis_devclass;
129
17975de1
SW
130DRIVER_MODULE(if_ndis, pci, ndis_driver, ndis_devclass, ndisdrv_modevent,
131 NULL);
efba76b4
SW
132
133static int
134ndis_devcompare(interface_type bustype, struct ndis_pci_type *t, device_t dev)
135{
136 uint16_t vid, did;
137 uint32_t subsys;
138
139 if (bustype != PCIBus)
140 return(FALSE);
141
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);
146
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);
152 }
153 t++;
154 }
155
156 return(FALSE);
157}
60d6dac1
MD
158
159/*
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.
162 */
163static int
7b9f668c 164ndis_probe_pci(device_t dev)
60d6dac1 165{
efba76b4
SW
166 driver_object *drv;
167 struct drvdb_ent *db;
60d6dac1 168
efba76b4 169 drv = windrv_lookup(0, "PCI Bus");
60d6dac1 170
efba76b4
SW
171 if (drv == NULL)
172 return(ENXIO);
173
174 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
175
176 if (db != NULL) {
177 /* Create PDO for this device instance */
178 windrv_create_pdo(drv, dev);
179 return(0);
60d6dac1
MD
180 }
181
182 return(ENXIO);
183}
184
185/*
186 * Attach the interface. Allocate softc structures, do ifmedia
187 * setup and ethernet/BPF attach.
188 */
189static int
7b9f668c 190ndis_attach_pci(device_t dev)
60d6dac1
MD
191{
192 struct ndis_softc *sc;
34f8ce05 193 int unit, error = 0, rid;
60d6dac1
MD
194 struct ndis_pci_type *t;
195 int devidx = 0, defidx = 0;
196 struct resource_list *rl;
197 struct resource_list_entry *rle;
efba76b4
SW
198 struct drvdb_ent *db;
199 uint16_t vid, did;
200 uint32_t subsys;
201
60d6dac1 202 sc = device_get_softc(dev);
34f8ce05 203 unit = device_get_unit(dev);
60d6dac1
MD
204 sc->ndis_dev = dev;
205
efba76b4 206 db = windrv_match((matchfuncptr)ndis_devcompare, dev);
34f8ce05 207 if (db == NULL)
efba76b4 208 return (ENXIO);
efba76b4
SW
209 sc->ndis_dobj = db->windrv_object;
210 sc->ndis_regvals = db->windrv_regvals;
211
60d6dac1
MD
212 /*
213 * Map control/status registers.
214 */
215
216 pci_enable_busmaster(dev);
217
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;
4e6d744d 224 sc->ndis_res_io = bus_alloc_resource_any(dev,
60d6dac1 225 SYS_RES_IOPORT, &sc->ndis_io_rid,
4e6d744d 226 RF_ACTIVE);
60d6dac1
MD
227 if (sc->ndis_res_io == NULL) {
228 device_printf(dev,
229 "couldn't map iospace\n");
230 error = ENXIO;
231 goto fail;
232 }
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;
241 }
efba76b4 242 if (sc->ndis_res_mem) {
60d6dac1
MD
243 sc->ndis_altmem_rid = rle->rid;
244 sc->ndis_res_altmem =
4e6d744d 245 bus_alloc_resource_any(dev,
60d6dac1
MD
246 SYS_RES_MEMORY,
247 &sc->ndis_altmem_rid,
4e6d744d 248 RF_ACTIVE);
60d6dac1
MD
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;
255 }
256 } else {
257 sc->ndis_mem_rid = rle->rid;
258 sc->ndis_res_mem =
4e6d744d 259 bus_alloc_resource_any(dev,
60d6dac1
MD
260 SYS_RES_MEMORY,
261 &sc->ndis_mem_rid,
4e6d744d 262 RF_ACTIVE);
60d6dac1
MD
263 if (sc->ndis_res_mem == NULL) {
264 device_printf(dev,
265 "couldn't map memory\n");
266 error = ENXIO;
267 goto fail;
268 }
269 }
270 break;
271 case SYS_RES_IRQ:
272 rid = rle->rid;
4e6d744d
JS
273 sc->ndis_irq = bus_alloc_resource_any(dev,
274 SYS_RES_IRQ, &rid,
275 RF_SHAREABLE | RF_ACTIVE);
60d6dac1
MD
276 if (sc->ndis_irq == NULL) {
277 device_printf(dev,
278 "couldn't map interrupt\n");
279 error = ENXIO;
280 goto fail;
281 }
282 break;
283 default:
284 break;
285 }
286 sc->ndis_rescnt++;
287 }
288 }
289
290 /*
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.
297 */
298 if (sc->ndis_irq == NULL) {
299 rid = 0;
4e6d744d
JS
300 sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
301 &rid, RF_SHAREABLE | RF_ACTIVE);
60d6dac1
MD
302 if (sc->ndis_irq == NULL) {
303 device_printf(dev, "couldn't route interrupt\n");
304 error = ENXIO;
305 goto fail;
306 }
307 sc->ndis_rescnt++;
308 }
309
310 /*
311 * Allocate the parent bus DMA tag appropriate for PCI.
312 */
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 */
34f8ce05 319 DFLTPHYS, NDIS_NSEG_NEW,/* maxsize, nsegments */
60d6dac1
MD
320 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
321 BUS_DMA_ALLOCNOW, /* flags */
322 &sc->ndis_parent_tag);
323
324 if (error)
325 goto fail;
326
327 sc->ndis_iftype = PCIBus;
328
329 /* Figure out exactly which device we matched. */
330
efba76b4
SW
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);
335
336 t = db->windrv_devlist;
60d6dac1
MD
337
338 while(t->ndis_name != NULL) {
efba76b4 339 if (t->ndis_vid == vid && t->ndis_did == did) {
60d6dac1
MD
340 if (t->ndis_subsys == 0)
341 defidx = devidx;
efba76b4
SW
342 else if (t->ndis_subsys == subsys)
343 break;
60d6dac1
MD
344 }
345 t++;
346 devidx++;
347 }
348
efba76b4 349 if (t->ndis_name == NULL)
60d6dac1
MD
350 sc->ndis_devidx = defidx;
351 else
352 sc->ndis_devidx = devidx;
353
354 error = ndis_attach(dev);
355
356fail:
357 return(error);
358}
359
360static struct resource_list *
7b9f668c 361ndis_get_resource_list(device_t dev, device_t child)
60d6dac1
MD
362{
363 struct ndis_softc *sc;
364
365 sc = device_get_softc(dev);
366 return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
367}