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