if_ndis_pccard.c revision 141524
1/*-
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 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis_pccard.c 141524 2005-02-08 17:23:25Z wpaul $");
35
36#include <sys/ctype.h>
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/module.h>
41#include <sys/socket.h>
42#include <sys/queue.h>
43#include <sys/sysctl.h>
44
45#include <net/if.h>
46#include <net/if_arp.h>
47#include <net/if_media.h>
48
49#include <machine/bus.h>
50#include <machine/resource.h>
51#include <sys/bus.h>
52#include <sys/rman.h>
53
54#include <net80211/ieee80211_var.h>
55
56#include <compat/ndis/pe_var.h>
57#include <compat/ndis/resource_var.h>
58#include <compat/ndis/ntoskrnl_var.h>
59#include <compat/ndis/ndis_var.h>
60#include <compat/ndis/cfg_var.h>
61#include <dev/if_ndis/if_ndisvar.h>
62
63#include <dev/pccard/pccardvar.h>
64#include "card_if.h"
65
66#include "ndis_driver_data.h"
67
68#ifdef NDIS_PCMCIA_DEV_TABLE
69
70MODULE_DEPEND(ndis, pccard, 1, 1, 1);
71MODULE_DEPEND(ndis, ether, 1, 1, 1);
72MODULE_DEPEND(ndis, wlan, 1, 1, 1);
73MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
74
75/*
76 * Various supported device vendors/types and their names.
77 * These are defined in the ndis_driver_data.h file.
78 */
79static struct ndis_pccard_type ndis_devs[] = {
80#ifdef NDIS_PCMCIA_DEV_TABLE
81	NDIS_PCMCIA_DEV_TABLE
82#endif
83	{ NULL, NULL, NULL }
84};
85
86static int ndis_probe_pccard	(device_t);
87static int ndis_attach_pccard	(device_t);
88static struct resource_list *ndis_get_resource_list
89				(device_t, device_t);
90extern int ndisdrv_modevent	(module_t, int, void *);
91extern int ndis_attach		(device_t);
92extern int ndis_shutdown	(device_t);
93extern int ndis_detach		(device_t);
94extern int ndis_suspend		(device_t);
95extern int ndis_resume		(device_t);
96
97extern unsigned char drv_data[];
98
99static device_method_t ndis_methods[] = {
100	/* Device interface */
101	DEVMETHOD(device_probe,		ndis_probe_pccard),
102	DEVMETHOD(device_attach,	ndis_attach_pccard),
103	DEVMETHOD(device_detach,	ndis_detach),
104	DEVMETHOD(device_shutdown,	ndis_shutdown),
105	DEVMETHOD(device_suspend,	ndis_suspend),
106	DEVMETHOD(device_resume,	ndis_resume),
107
108	/* Bus interface. */
109
110	/*
111	 * This is an awful kludge, but we need it becase pccard
112	 * does not implement a bus_get_resource_list() method.
113	 */
114
115	DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
116
117	{ 0, 0 }
118};
119
120static driver_t ndis_driver = {
121#ifdef NDIS_DEVNAME
122	NDIS_DEVNAME,
123#else
124	"ndis",
125#endif
126	ndis_methods,
127	sizeof(struct ndis_softc)
128};
129
130static devclass_t ndis_devclass;
131
132#ifdef NDIS_MODNAME
133#define NDIS_MODNAME_OVERRIDE_PCMCIA(x)					\
134	DRIVER_MODULE(x, pccard, ndis_driver, ndis_devclass,		\
135		ndisdrv_modevent, 0)
136NDIS_MODNAME_OVERRIDE_PCMCIA(NDIS_MODNAME);
137#else
138DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
139#endif
140
141/*
142 * Probe for an NDIS device. Check the PCI vendor and device
143 * IDs against our list and return a device name if we find a match.
144 */
145static int
146ndis_probe_pccard(dev)
147	device_t		dev;
148{
149	struct ndis_pccard_type	*t;
150	const char		*prodstr, *vendstr;
151	int			error;
152	driver_object		*drv;
153	vm_offset_t		img;
154
155	img = (vm_offset_t)drv_data;
156	drv = windrv_lookup(img);
157	if (drv == NULL)
158		return(ENXIO);
159
160	t = ndis_devs;
161
162	error = pccard_get_product_str(dev, &prodstr);
163	if (error)
164		return(error);
165	error = pccard_get_vendor_str(dev, &vendstr);
166	if (error)
167		return(error);
168
169	while(t->ndis_name != NULL) {
170		if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
171		    ndis_strcasecmp(prodstr, t->ndis_did) == 0) {
172			device_set_desc(dev, t->ndis_name);
173			/* Create PDO for this device instance */
174			windrv_create_pdo(drv, dev);
175			return(0);
176		}
177		t++;
178	}
179
180	return(ENXIO);
181}
182
183/*
184 * Attach the interface. Allocate softc structures, do ifmedia
185 * setup and ethernet/BPF attach.
186 */
187static int
188ndis_attach_pccard(dev)
189	device_t		dev;
190{
191	struct ndis_softc	*sc;
192	int			unit, error = 0, rid;
193	struct ndis_pccard_type	*t;
194	int			devidx = 0;
195	const char		*prodstr, *vendstr;
196
197	sc = device_get_softc(dev);
198	unit = device_get_unit(dev);
199	sc->ndis_dev = dev;
200	resource_list_init(&sc->ndis_rl);
201
202	sc->ndis_io_rid = 0;
203	sc->ndis_res_io = bus_alloc_resource(dev,
204	    SYS_RES_IOPORT, &sc->ndis_io_rid,
205	    0, ~0, 1, RF_ACTIVE);
206	if (sc->ndis_res_io == NULL) {
207		device_printf(dev,
208		    "couldn't map iospace\n");
209		error = ENXIO;
210		goto fail;
211	}
212	sc->ndis_rescnt++;
213	resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, rid,
214	    rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
215	    rman_get_size(sc->ndis_res_io));
216
217	rid = 0;
218	sc->ndis_irq = bus_alloc_resource(dev,
219	    SYS_RES_IRQ, &rid, 0, ~0, 1,
220	    RF_SHAREABLE | RF_ACTIVE);
221	if (sc->ndis_irq == NULL) {
222		device_printf(dev,
223		    "couldn't map interrupt\n");
224		error = ENXIO;
225		goto fail;
226	}
227	sc->ndis_rescnt++;
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);
230
231	sc->ndis_iftype = PCMCIABus;
232
233	/* Figure out exactly which device we matched. */
234
235	t = ndis_devs;
236
237	error = pccard_get_product_str(dev, &prodstr);
238	if (error)
239		return(error);
240	error = pccard_get_vendor_str(dev, &vendstr);
241	if (error)
242		return(error);
243
244	while(t->ndis_name != NULL) {
245		if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
246		    ndis_strcasecmp(prodstr, t->ndis_did) == 0)
247			break;
248		t++;
249		devidx++;
250	}
251
252	sc->ndis_devidx = devidx;
253
254	error = ndis_attach(dev);
255
256fail:
257	return(error);
258}
259
260static struct resource_list *
261ndis_get_resource_list(dev, child)
262	device_t		dev;
263	device_t		child;
264{
265	struct ndis_softc	*sc;
266
267	sc = device_get_softc(dev);
268	return (&sc->ndis_rl);
269}
270
271#endif /* NDIS_PCI_DEV_TABLE */
272
273#define NDIS_AM_RID 3
274
275int
276ndis_alloc_amem(arg)
277	void			*arg;
278{
279	struct ndis_softc	*sc;
280	int			error, rid;
281
282	if (arg == NULL)
283		return(EINVAL);
284
285	sc = arg;
286	rid = NDIS_AM_RID;
287	sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY,
288	    &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
289
290	if (sc->ndis_res_am == NULL) {
291		device_printf(sc->ndis_dev,
292		    "failed to allocate attribute memory\n");
293		return(ENXIO);
294	}
295	sc->ndis_rescnt++;
296	resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
297	    rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
298	    rman_get_size(sc->ndis_res_am));
299
300	error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
301	    sc->ndis_dev, rid, 0, NULL);
302
303	if (error) {
304		device_printf(sc->ndis_dev,
305		    "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
306		return(error);
307	}
308
309	error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
310	    sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
311
312	if (error) {
313		device_printf(sc->ndis_dev,
314		    "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
315		return(error);
316	}
317
318	sc->ndis_am_rid = rid;
319
320	return(0);
321}
322
323void
324ndis_free_amem(arg)
325	void			*arg;
326{
327	struct ndis_softc	*sc;
328
329	if (arg == NULL)
330		return;
331
332	sc = arg;
333
334	if (sc->ndis_res_am != NULL)
335		bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
336		    sc->ndis_am_rid, sc->ndis_res_am);
337	resource_list_free(&sc->ndis_rl);
338
339	return;
340}
341