if_ndis_pccard.c revision 145485
1227652Sgrehan/*-
2227652Sgrehan * Copyright (c) 2003
3227652Sgrehan *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4227652Sgrehan *
5227652Sgrehan * Redistribution and use in source and binary forms, with or without
6227652Sgrehan * modification, are permitted provided that the following conditions
7227652Sgrehan * are met:
8227652Sgrehan * 1. Redistributions of source code must retain the above copyright
9227652Sgrehan *    notice, this list of conditions and the following disclaimer.
10227652Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11227652Sgrehan *    notice, this list of conditions and the following disclaimer in the
12227652Sgrehan *    documentation and/or other materials provided with the distribution.
13227652Sgrehan * 3. All advertising materials mentioning features or use of this software
14227652Sgrehan *    must display the following acknowledgement:
15227652Sgrehan *	This product includes software developed by Bill Paul.
16227652Sgrehan * 4. Neither the name of the author nor the names of any co-contributors
17227652Sgrehan *    may be used to endorse or promote products derived from this software
18227652Sgrehan *    without specific prior written permission.
19227652Sgrehan *
20227652Sgrehan * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21227652Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22227652Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23227652Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24227652Sgrehan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25227652Sgrehan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26227652Sgrehan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27252703Sbryanv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28227652Sgrehan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29227652Sgrehan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30227652Sgrehan * THE POSSIBILITY OF SUCH DAMAGE.
31227652Sgrehan */
32227652Sgrehan
33227652Sgrehan#include <sys/cdefs.h>
34227652Sgrehan__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis_pccard.c 145485 2005-04-24 20:21:22Z wpaul $");
35227652Sgrehan
36227652Sgrehan#include <sys/ctype.h>
37227652Sgrehan#include <sys/param.h>
38227652Sgrehan#include <sys/systm.h>
39227652Sgrehan#include <sys/kernel.h>
40227652Sgrehan#include <sys/module.h>
41227652Sgrehan#include <sys/socket.h>
42227652Sgrehan#include <sys/queue.h>
43227652Sgrehan#include <sys/sysctl.h>
44227652Sgrehan
45227652Sgrehan#include <net/if.h>
46227652Sgrehan#include <net/if_arp.h>
47227652Sgrehan#include <net/if_media.h>
48227652Sgrehan
49227652Sgrehan#include <machine/bus.h>
50227652Sgrehan#include <machine/resource.h>
51227652Sgrehan#include <sys/bus.h>
52227652Sgrehan#include <sys/rman.h>
53227652Sgrehan
54227652Sgrehan#include <net80211/ieee80211_var.h>
55227652Sgrehan
56252703Sbryanv#include <compat/ndis/pe_var.h>
57227652Sgrehan#include <compat/ndis/cfg_var.h>
58252703Sbryanv#include <compat/ndis/resource_var.h>
59252703Sbryanv#include <compat/ndis/ntoskrnl_var.h>
60252703Sbryanv#include <compat/ndis/ndis_var.h>
61252703Sbryanv#include <dev/if_ndis/if_ndisvar.h>
62230450Sbrueffer
63252703Sbryanv#include <dev/pccard/pccardvar.h>
64252703Sbryanv#include "card_if.h"
65252703Sbryanv
66252703SbryanvMODULE_DEPEND(ndis, pccard, 1, 1, 1);
67252703Sbryanv
68252703Sbryanvstatic int ndis_probe_pccard	(device_t);
69252703Sbryanvstatic int ndis_attach_pccard	(device_t);
70252703Sbryanvstatic struct resource_list *ndis_get_resource_list
71227652Sgrehan				(device_t, device_t);
72252703Sbryanvstatic int ndis_devcompare	(struct ndis_pccard_type *, device_t);
73252703Sbryanvextern int ndisdrv_modevent	(module_t, int, void *);
74252703Sbryanvextern int ndis_attach		(device_t);
75252703Sbryanvextern int ndis_shutdown	(device_t);
76252703Sbryanvextern int ndis_detach		(device_t);
77252703Sbryanvextern int ndis_suspend		(device_t);
78252703Sbryanvextern int ndis_resume		(device_t);
79252703Sbryanv
80252703Sbryanvextern unsigned char drv_data[];
81252703Sbryanv
82252703Sbryanvstatic device_method_t ndis_methods[] = {
83252703Sbryanv	/* Device interface */
84227652Sgrehan	DEVMETHOD(device_probe,		ndis_probe_pccard),
85227652Sgrehan	DEVMETHOD(device_attach,	ndis_attach_pccard),
86227652Sgrehan	DEVMETHOD(device_detach,	ndis_detach),
87227652Sgrehan	DEVMETHOD(device_shutdown,	ndis_shutdown),
88227652Sgrehan	DEVMETHOD(device_suspend,	ndis_suspend),
89227652Sgrehan	DEVMETHOD(device_resume,	ndis_resume),
90252707Sbryanv
91227652Sgrehan	/* Bus interface. */
92227652Sgrehan
93	/*
94	 * This is an awful kludge, but we need it becase pccard
95	 * does not implement a bus_get_resource_list() method.
96	 */
97
98	DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
99
100	{ 0, 0 }
101};
102
103static driver_t ndis_driver = {
104	"ndis",
105	ndis_methods,
106	sizeof(struct ndis_softc)
107};
108
109static devclass_t ndis_devclass;
110
111DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
112
113static int
114ndis_devcompare(t, dev)
115	struct ndis_pccard_type	*t;
116	device_t		dev;
117{
118	const char		*prodstr, *vendstr;
119	int			error;
120
121	error = pccard_get_product_str(dev, &prodstr);
122	if (error)
123		return(FALSE);
124	error = pccard_get_vendor_str(dev, &vendstr);
125	if (error)
126		return(FALSE);
127
128	while(t->ndis_name != NULL) {
129		if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
130		    ndis_strcasecmp(prodstr, t->ndis_did) == 0) {
131			device_set_desc(dev, t->ndis_name);
132			return(TRUE);
133		}
134		t++;
135	}
136
137	return(FALSE);
138}
139
140/*
141 * Probe for an NDIS device. Check the PCI vendor and device
142 * IDs against our list and return a device name if we find a match.
143 */
144static int
145ndis_probe_pccard(dev)
146	device_t		dev;
147{
148	driver_object		*drv;
149	struct drvdb_ent	*db;
150
151	drv = windrv_lookup(0, "PCCARD Bus");
152	if (drv == NULL)
153		return(ENXIO);
154
155	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
156
157	if (db != NULL) {
158		/* Create PDO for this device instance */
159		windrv_create_pdo(drv, dev);
160		return(0);
161	}
162
163	return(ENXIO);
164}
165
166/*
167 * Attach the interface. Allocate softc structures, do ifmedia
168 * setup and ethernet/BPF attach.
169 */
170static int
171ndis_attach_pccard(dev)
172	device_t		dev;
173{
174	struct ndis_softc	*sc;
175	int			unit, error = 0, rid;
176	struct ndis_pccard_type	*t;
177	int			devidx = 0;
178	const char		*prodstr, *vendstr;
179	struct drvdb_ent	*db;
180
181	sc = device_get_softc(dev);
182	unit = device_get_unit(dev);
183	sc->ndis_dev = dev;
184
185	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
186	if (db == NULL)
187		return (ENXIO);
188
189	resource_list_init(&sc->ndis_rl);
190
191	sc->ndis_io_rid = 0;
192	sc->ndis_res_io = bus_alloc_resource(dev,
193	    SYS_RES_IOPORT, &sc->ndis_io_rid,
194	    0, ~0, 1, RF_ACTIVE);
195	if (sc->ndis_res_io == NULL) {
196		device_printf(dev,
197		    "couldn't map iospace\n");
198		error = ENXIO;
199		goto fail;
200	}
201	sc->ndis_rescnt++;
202	resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, rid,
203	    rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
204	    rman_get_size(sc->ndis_res_io));
205
206	rid = 0;
207	sc->ndis_irq = bus_alloc_resource(dev,
208	    SYS_RES_IRQ, &rid, 0, ~0, 1,
209	    RF_SHAREABLE | RF_ACTIVE);
210	if (sc->ndis_irq == NULL) {
211		device_printf(dev,
212		    "couldn't map interrupt\n");
213		error = ENXIO;
214		goto fail;
215	}
216	sc->ndis_rescnt++;
217	resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
218	    rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
219
220	sc->ndis_iftype = PCMCIABus;
221
222	/* Figure out exactly which device we matched. */
223
224	t = db->windrv_devlist;
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 (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
235		    ndis_strcasecmp(prodstr, t->ndis_did) == 0)
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
249static struct resource_list *
250ndis_get_resource_list(dev, child)
251	device_t		dev;
252	device_t		child;
253{
254	struct ndis_softc	*sc;
255
256	sc = device_get_softc(dev);
257	return (&sc->ndis_rl);
258}
259
260#define NDIS_AM_RID 3
261
262int
263ndis_alloc_amem(arg)
264	void			*arg;
265{
266	struct ndis_softc	*sc;
267	int			error, rid;
268
269	if (arg == NULL)
270		return(EINVAL);
271
272	sc = arg;
273	rid = NDIS_AM_RID;
274	sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY,
275	    &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
276
277	if (sc->ndis_res_am == NULL) {
278		device_printf(sc->ndis_dev,
279		    "failed to allocate attribute memory\n");
280		return(ENXIO);
281	}
282	sc->ndis_rescnt++;
283	resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
284	    rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
285	    rman_get_size(sc->ndis_res_am));
286
287	error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
288	    sc->ndis_dev, rid, 0, NULL);
289
290	if (error) {
291		device_printf(sc->ndis_dev,
292		    "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
293		return(error);
294	}
295
296	error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
297	    sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
298
299	if (error) {
300		device_printf(sc->ndis_dev,
301		    "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
302		return(error);
303	}
304
305	sc->ndis_am_rid = rid;
306
307	return(0);
308}
309
310void
311ndis_free_amem(arg)
312	void			*arg;
313{
314	struct ndis_softc	*sc;
315
316	if (arg == NULL)
317		return;
318
319	sc = arg;
320
321	if (sc->ndis_res_am != NULL)
322		bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
323		    sc->ndis_am_rid, sc->ndis_res_am);
324	resource_list_free(&sc->ndis_rl);
325
326	return;
327}
328