1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 2003
5 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38#include <sys/ctype.h>
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/module.h>
43#include <sys/socket.h>
44#include <sys/queue.h>
45#include <sys/sysctl.h>
46
47#include <net/if.h>
48#include <net/if_var.h>
49#include <net/if_arp.h>
50#include <net/if_media.h>
51#include <net/ethernet.h>
52
53#include <machine/bus.h>
54#include <machine/resource.h>
55#include <sys/bus.h>
56#include <sys/rman.h>
57
58#include <dev/usb/usb.h>
59#include <dev/usb/usbdi.h>
60
61#include <net80211/ieee80211_var.h>
62
63#include <compat/ndis/pe_var.h>
64#include <compat/ndis/cfg_var.h>
65#include <compat/ndis/resource_var.h>
66#include <compat/ndis/ntoskrnl_var.h>
67#include <compat/ndis/ndis_var.h>
68#include <dev/if_ndis/if_ndisvar.h>
69
70#include <dev/pccard/pccardvar.h>
71#include "card_if.h"
72
73MODULE_DEPEND(ndis, pccard, 1, 1, 1);
74
75static int ndis_probe_pccard	(device_t);
76static int ndis_attach_pccard	(device_t);
77static int ndis_detach_pccard	(device_t);
78static struct resource_list *ndis_get_resource_list
79				(device_t, device_t);
80static int ndis_devcompare	(interface_type,
81				 struct ndis_pccard_type *, device_t);
82extern int ndisdrv_modevent	(module_t, int, void *);
83extern int ndis_attach		(device_t);
84extern int ndis_shutdown	(device_t);
85extern int ndis_detach		(device_t);
86extern int ndis_suspend		(device_t);
87extern int ndis_resume		(device_t);
88
89extern unsigned char drv_data[];
90
91static device_method_t ndis_methods[] = {
92	/* Device interface */
93	DEVMETHOD(device_probe,		ndis_probe_pccard),
94	DEVMETHOD(device_attach,	ndis_attach_pccard),
95	DEVMETHOD(device_detach,	ndis_detach_pccard),
96	DEVMETHOD(device_shutdown,	ndis_shutdown),
97	DEVMETHOD(device_suspend,	ndis_suspend),
98	DEVMETHOD(device_resume,	ndis_resume),
99
100	/* Bus interface. */
101
102	/*
103	 * This is an awful kludge, but we need it becase pccard
104	 * does not implement a bus_get_resource_list() method.
105	 */
106
107	DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
108
109	{ 0, 0 }
110};
111
112static driver_t ndis_driver = {
113	"ndis",
114	ndis_methods,
115	sizeof(struct ndis_softc)
116};
117
118static devclass_t ndis_devclass;
119
120DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
121
122static int
123ndis_devcompare(bustype, t, dev)
124	interface_type		bustype;
125	struct ndis_pccard_type	*t;
126	device_t		dev;
127{
128	const char		*prodstr, *vendstr;
129	int			error;
130
131	if (bustype != PCMCIABus)
132		return(FALSE);
133
134	error = pccard_get_product_str(dev, &prodstr);
135	if (error)
136		return(FALSE);
137	error = pccard_get_vendor_str(dev, &vendstr);
138	if (error)
139		return(FALSE);
140
141	while(t->ndis_name != NULL) {
142		if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
143		    strcasecmp(prodstr, t->ndis_did) == 0) {
144			device_set_desc(dev, t->ndis_name);
145			return(TRUE);
146		}
147		t++;
148	}
149
150	return(FALSE);
151}
152
153/*
154 * Probe for an NDIS device. Check the PCI vendor and device
155 * IDs against our list and return a device name if we find a match.
156 */
157static int
158ndis_probe_pccard(dev)
159	device_t		dev;
160{
161	driver_object		*drv;
162	struct drvdb_ent	*db;
163
164	drv = windrv_lookup(0, "PCCARD Bus");
165	if (drv == NULL)
166		return(ENXIO);
167
168	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
169
170	if (db != NULL) {
171		/* Create PDO for this device instance */
172		windrv_create_pdo(drv, dev);
173		return(0);
174	}
175
176	return(ENXIO);
177}
178
179#define NDIS_AM_RID 3
180
181static int
182ndis_alloc_amem(struct ndis_softc *sc)
183{
184	int error, rid;
185
186	rid = NDIS_AM_RID;
187	sc->ndis_res_am = bus_alloc_resource_anywhere(sc->ndis_dev,
188	    SYS_RES_MEMORY, &rid, 0x1000, RF_ACTIVE);
189
190	if (sc->ndis_res_am == NULL) {
191		device_printf(sc->ndis_dev,
192		    "failed to allocate attribute memory\n");
193		return(ENXIO);
194	}
195	sc->ndis_rescnt++;
196	resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
197	    rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
198	    rman_get_size(sc->ndis_res_am));
199
200	error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
201	    sc->ndis_dev, rid, 0, NULL);
202
203	if (error) {
204		device_printf(sc->ndis_dev,
205		    "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
206		return(error);
207	}
208
209	error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
210	    sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
211
212	if (error) {
213		device_printf(sc->ndis_dev,
214		    "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
215		return(error);
216	}
217
218	sc->ndis_am_rid = rid;
219
220	return(0);
221}
222
223/*
224 * Attach the interface. Allocate softc structures, do ifmedia
225 * setup and ethernet/BPF attach.
226 */
227static int
228ndis_attach_pccard(dev)
229	device_t		dev;
230{
231	struct ndis_softc	*sc;
232	int			unit, error = 0, rid;
233	struct ndis_pccard_type	*t;
234	int			devidx = 0;
235	const char		*prodstr, *vendstr;
236	struct drvdb_ent	*db;
237
238	sc = device_get_softc(dev);
239	unit = device_get_unit(dev);
240	sc->ndis_dev = dev;
241
242	db = windrv_match((matchfuncptr)ndis_devcompare, dev);
243	if (db == NULL)
244		return (ENXIO);
245	sc->ndis_dobj = db->windrv_object;
246	sc->ndis_regvals = db->windrv_regvals;
247	resource_list_init(&sc->ndis_rl);
248
249	sc->ndis_io_rid = 0;
250	sc->ndis_res_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
251	    &sc->ndis_io_rid, RF_ACTIVE);
252	if (sc->ndis_res_io == NULL) {
253		device_printf(dev,
254		    "couldn't map iospace\n");
255		error = ENXIO;
256		goto fail;
257	}
258	sc->ndis_rescnt++;
259	resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, sc->ndis_io_rid,
260	    rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
261	    rman_get_size(sc->ndis_res_io));
262
263	rid = 0;
264	sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
265	    RF_SHAREABLE | RF_ACTIVE);
266	if (sc->ndis_irq == NULL) {
267		device_printf(dev,
268		    "couldn't map interrupt\n");
269		error = ENXIO;
270		goto fail;
271	}
272	sc->ndis_rescnt++;
273	resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
274	    rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
275
276	sc->ndis_iftype = PCMCIABus;
277
278	/* Figure out exactly which device we matched. */
279
280	t = db->windrv_devlist;
281
282	error = pccard_get_product_str(dev, &prodstr);
283	if (error)
284		return(error);
285	error = pccard_get_vendor_str(dev, &vendstr);
286	if (error)
287		return(error);
288
289	while(t->ndis_name != NULL) {
290		if (strcasecmp(vendstr, t->ndis_vid) == 0 &&
291		    strcasecmp(prodstr, t->ndis_did) == 0)
292			break;
293		t++;
294		devidx++;
295	}
296
297	sc->ndis_devidx = devidx;
298
299	error = ndis_alloc_amem(sc);
300	if (error) {
301		device_printf(dev, "failed to allocate attribute memory\n");
302		goto fail;
303	}
304
305	error = ndis_attach(dev);
306	if (error == 0)
307		gone_in_dev(dev, 13, "ndis removed");
308
309fail:
310	return(error);
311}
312
313static int
314ndis_detach_pccard(device_t dev)
315{
316	struct ndis_softc *sc = device_get_softc(dev);
317
318	(void) ndis_detach(dev);
319
320	if (sc->ndis_res_am != NULL)
321		bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
322		    sc->ndis_am_rid, sc->ndis_res_am);
323	resource_list_free(&sc->ndis_rl);
324
325	return (0);
326}
327
328static struct resource_list *
329ndis_get_resource_list(dev, child)
330	device_t		dev;
331	device_t		child;
332{
333	struct ndis_softc	*sc;
334
335	sc = device_get_softc(dev);
336	return (&sc->ndis_rl);
337}
338