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