if_ndis_pccard.c revision 127281
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 127281 2004-03-21 19:56:41Z 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/socket.h>
41#include <sys/queue.h>
42#include <sys/sysctl.h>
43
44#include <net/if.h>
45#include <net/if_arp.h>
46#include <net/if_media.h>
47
48#include <machine/bus.h>
49#include <machine/resource.h>
50#include <sys/bus.h>
51#include <sys/rman.h>
52
53#include <net80211/ieee80211_var.h>
54
55#include <compat/ndis/pe_var.h>
56#include <compat/ndis/resource_var.h>
57#include <compat/ndis/ntoskrnl_var.h>
58#include <compat/ndis/ndis_var.h>
59#include <compat/ndis/cfg_var.h>
60#include <dev/if_ndis/if_ndisvar.h>
61
62#include <dev/pccard/pccardvar.h>
63#include "card_if.h"
64
65#include "ndis_driver_data.h"
66
67#ifdef NDIS_PCMCIA_DEV_TABLE
68
69MODULE_DEPEND(ndis, pccard, 1, 1, 1);
70MODULE_DEPEND(ndis, ether, 1, 1, 1);
71MODULE_DEPEND(ndis, wlan, 1, 1, 1);
72MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
73
74/*
75 * Various supported device vendors/types and their names.
76 * These are defined in the ndis_driver_data.h file.
77 */
78static struct ndis_pccard_type ndis_devs[] = {
79#ifdef NDIS_PCMCIA_DEV_TABLE
80	NDIS_PCMCIA_DEV_TABLE
81#endif
82	{ NULL, NULL, NULL }
83};
84
85static int ndis_probe_pccard	(device_t);
86static int ndis_attach_pccard	(device_t);
87extern int ndis_attach		(device_t);
88extern int ndis_shutdown	(device_t);
89extern int ndis_detach		(device_t);
90extern int ndis_suspend		(device_t);
91extern int ndis_resume		(device_t);
92
93static int my_strcasecmp	(const char *, const char *, int);
94
95extern struct mtx_pool *ndis_mtxpool;
96
97static device_method_t ndis_methods[] = {
98	/* Device interface */
99	DEVMETHOD(device_probe,		ndis_probe_pccard),
100	DEVMETHOD(device_attach,	ndis_attach_pccard),
101	DEVMETHOD(device_detach,	ndis_detach),
102	DEVMETHOD(device_shutdown,	ndis_shutdown),
103	DEVMETHOD(device_suspend,	ndis_suspend),
104	DEVMETHOD(device_resume,	ndis_resume),
105
106	{ 0, 0 }
107};
108
109static driver_t ndis_driver = {
110#ifdef NDIS_DEVNAME
111	NDIS_DEVNAME,
112#else
113	"ndis",
114#endif
115	ndis_methods,
116	sizeof(struct ndis_softc)
117};
118
119static devclass_t ndis_devclass;
120
121#ifdef NDIS_MODNAME
122#define NDIS_MODNAME_OVERRIDE_PCMCIA(x)					\
123	DRIVER_MODULE(x, pccard, ndis_driver, ndis_devclass, 0, 0)
124NDIS_MODNAME_OVERRIDE_PCMCIA(NDIS_MODNAME);
125#else
126DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, 0, 0);
127#endif
128
129static int my_strcasecmp(s1, s2, len)
130	const char		*s1;
131	const char		*s2;
132	int			len;
133{
134	int			i;
135
136	for (i = 0; i < len; i++) {
137		if (toupper(s1[i]) != toupper(s2[i]))
138			return(0);
139	}
140
141	return(1);
142}
143
144
145/*
146 * Probe for an NDIS device. Check the PCI vendor and device
147 * IDs against our list and return a device name if we find a match.
148 */
149static int
150ndis_probe_pccard(dev)
151	device_t		dev;
152{
153	struct ndis_pccard_type	*t;
154	const char		*prodstr, *vendstr;
155	int			error;
156
157	t = ndis_devs;
158
159	error = pccard_get_product_str(dev, &prodstr);
160	if (error)
161		return(error);
162	error = pccard_get_vendor_str(dev, &vendstr);
163	if (error)
164		return(error);
165
166	while(t->ndis_name != NULL) {
167		if (my_strcasecmp(vendstr, t->ndis_vid, strlen(vendstr)) &&
168		    my_strcasecmp(prodstr, t->ndis_did, strlen(prodstr))) {
169			device_set_desc(dev, t->ndis_name);
170			return(0);
171		}
172		t++;
173	}
174
175	return(ENXIO);
176}
177
178/*
179 * Attach the interface. Allocate softc structures, do ifmedia
180 * setup and ethernet/BPF attach.
181 */
182static int
183ndis_attach_pccard(dev)
184	device_t		dev;
185{
186	struct ndis_softc	*sc;
187	int			unit, error = 0, rid;
188	struct ndis_pccard_type	*t;
189	int			devidx = 0;
190	const char		*prodstr, *vendstr;
191
192	sc = device_get_softc(dev);
193	unit = device_get_unit(dev);
194	sc->ndis_dev = dev;
195
196	sc->ndis_io_rid = 0;
197	sc->ndis_res_io = bus_alloc_resource(dev,
198	    SYS_RES_IOPORT, &sc->ndis_io_rid,
199	    0, ~0, 1, RF_ACTIVE);
200	if (sc->ndis_res_io == NULL) {
201		device_printf(dev,
202		    "couldn't map iospace\n");
203		error = ENXIO;
204		goto fail;
205	}
206	sc->ndis_rescnt++;
207
208	rid = 0;
209	sc->ndis_irq = bus_alloc_resource(dev,
210	    SYS_RES_IRQ, &rid, 0, ~0, 1,
211	    RF_SHAREABLE | RF_ACTIVE);
212	if (sc->ndis_irq == NULL) {
213		device_printf(dev,
214		    "couldn't map interrupt\n");
215		error = ENXIO;
216		goto fail;
217	}
218	sc->ndis_rescnt++;
219
220	sc->ndis_iftype = PCMCIABus;
221
222	/* Figure out exactly which device we matched. */
223
224	t = ndis_devs;
225
226	error = pccard_get_product_str(dev, &prodstr);
227	if (error)
228		return(error);
229	error = pccard_get_vendor_str(dev, &vendstr);
230	if (error)
231		return(error);
232
233	while(t->ndis_name != NULL) {
234		if (my_strcasecmp(vendstr, t->ndis_vid, strlen(vendstr)) &&
235		    my_strcasecmp(prodstr, t->ndis_did, strlen(prodstr)))
236			break;
237		t++;
238		devidx++;
239	}
240
241	sc->ndis_devidx = devidx;
242
243	error = ndis_attach(dev);
244
245fail:
246	return(error);
247}
248
249#endif /* NDIS_PCI_DEV_TABLE */
250
251#define NDIS_AM_RID 3
252
253int
254ndis_alloc_amem(arg)
255	void			*arg;
256{
257	struct ndis_softc	*sc;
258	int			error, rid;
259
260	if (arg == NULL)
261		return(EINVAL);
262
263	sc = arg;
264	rid = NDIS_AM_RID;
265	sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY,
266	    &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
267
268	if (sc->ndis_res_am == NULL) {
269		device_printf(sc->ndis_dev,
270		    "failed to allocate attribute memory\n");
271		return(ENXIO);
272	}
273
274	error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
275	    sc->ndis_dev, rid, 0, NULL);
276
277	if (error) {
278		device_printf(sc->ndis_dev,
279		    "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
280		return(error);
281	}
282
283	error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
284	    sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
285
286	if (error) {
287		device_printf(sc->ndis_dev,
288		    "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
289		return(error);
290	}
291
292	return(0);
293}
294