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