89c76d26fb5d0ecb156b4a322450c530327bef2a
[dragonfly.git] / sys / dev / netif / ndis / if_ndis_pccard.c
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  * $FreeBSD: src/sys/dev/if_ndis/if_ndis_pccard.c,v 1.6 2004/07/11 00:19:30 wpaul Exp $
33  */
34
35 #include <sys/ctype.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/module.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/bus.h>
45 #include <sys/rman.h>
46
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/if_media.h>
50
51 #include <netproto/802_11/ieee80211_var.h>
52
53 #include <emulation/ndis/regcall.h>
54 #include <emulation/ndis/pe_var.h>
55 #include <emulation/ndis/resource_var.h>
56 #include <emulation/ndis/ntoskrnl_var.h>
57 #include <emulation/ndis/ndis_var.h>
58 #include <emulation/ndis/cfg_var.h>
59 #include "if_ndisvar.h"
60
61 #include <bus/pccard/pccardvar.h>
62 #include "card_if.h"
63
64 #include "ndis_driver_data.h"
65
66 #ifdef NDIS_PCMCIA_DEV_TABLE 
67
68 MODULE_DEPEND(ndis, pccard, 1, 1, 1);
69 MODULE_DEPEND(ndis, wlan, 1, 1, 1);
70 MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
71
72 /*
73  * Various supported device vendors/types and their names.
74  * These are defined in the ndis_driver_data.h file.
75  */
76 static struct ndis_pccard_type ndis_devs[] = {
77 #ifdef NDIS_PCMCIA_DEV_TABLE
78         NDIS_PCMCIA_DEV_TABLE
79 #endif
80         { NULL, NULL, NULL }
81 };
82
83 static int ndis_probe_pccard    (device_t);
84 static int ndis_attach_pccard   (device_t);
85 static struct resource_list *ndis_get_resource_list
86                                 (device_t, device_t);
87 extern int ndis_attach          (device_t);
88 extern int ndis_shutdown        (device_t);
89 extern int ndis_detach          (device_t);
90 extern int ndis_suspend         (device_t);
91 extern int ndis_resume          (device_t);
92
93 static device_method_t ndis_methods[] = {
94         /* Device interface */
95         DEVMETHOD(device_probe,         ndis_probe_pccard),
96         DEVMETHOD(device_attach,        ndis_attach_pccard),
97         DEVMETHOD(device_detach,        ndis_detach),
98         DEVMETHOD(device_shutdown,      ndis_shutdown),
99         DEVMETHOD(device_suspend,       ndis_suspend),
100         DEVMETHOD(device_resume,        ndis_resume),
101
102         /* Bus interface. */
103
104         /*
105          * This is an awful kludge, but we need it becase pccard
106          * does not implement a bus_get_resource_list() method.
107          */
108
109         DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
110
111         { 0, 0 }
112 };
113
114 static driver_t ndis_driver = {
115 #ifdef NDIS_DEVNAME
116         NDIS_DEVNAME,
117 #else
118         "ndis",
119 #endif
120         ndis_methods,
121         sizeof(struct ndis_softc)
122 };
123
124 static devclass_t ndis_devclass;
125
126 #ifdef NDIS_MODNAME
127 #define NDIS_MODNAME_OVERRIDE_PCMCIA(x)                                 \
128         DRIVER_MODULE(x, pccard, ndis_driver, ndis_devclass, NULL, NULL)
129 NDIS_MODNAME_OVERRIDE_PCMCIA(NDIS_MODNAME);
130 #else
131 DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, NULL, NULL);
132 #endif
133
134 /*
135  * Probe for an NDIS device. Check the PCI vendor and device
136  * IDs against our list and return a device name if we find a match.
137  */
138 static int
139 ndis_probe_pccard(device_t dev)
140 {
141         struct ndis_pccard_type *t;
142         const char              *prodstr, *vendstr;
143         int                     error;
144
145         t = ndis_devs;
146
147         prodstr = pccard_get_product_str(dev);
148         vendstr = pccard_get_vendor_str(dev);
149
150         while(t->ndis_name != NULL) {
151                 if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
152                     ndis_strcasecmp(prodstr, t->ndis_did) == 0) {
153                         device_set_desc(dev, t->ndis_name);
154                         return(0);
155                 }
156                 t++;
157         }
158
159         return(ENXIO);
160 }
161
162 /*
163  * Attach the interface. Allocate softc structures, do ifmedia
164  * setup and ethernet/BPF attach.
165  */
166 static int
167 ndis_attach_pccard(device_t dev)
168 {
169         struct ndis_softc       *sc;
170         int                     unit, error = 0, rid;
171         struct ndis_pccard_type *t;
172         int                     devidx = 0;
173         const char              *prodstr, *vendstr;
174
175         sc = device_get_softc(dev);
176         unit = device_get_unit(dev);
177         sc->ndis_dev = dev;
178         resource_list_init(&sc->ndis_rl);
179
180         sc->ndis_io_rid = 0;
181         sc->ndis_res_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
182             &sc->ndis_io_rid, RF_ACTIVE);
183         if (sc->ndis_res_io == NULL) {
184                 device_printf(dev,
185                     "couldn't map iospace\n");
186                 error = ENXIO;
187                 goto fail;
188         }
189         sc->ndis_rescnt++;
190         resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, rid,
191             rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
192             rman_get_size(sc->ndis_res_io));
193
194         rid = 0;
195         sc->ndis_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
196             RF_SHAREABLE | RF_ACTIVE);
197         if (sc->ndis_irq == NULL) {
198                 device_printf(dev,
199                     "couldn't map interrupt\n");
200                 error = ENXIO;
201                 goto fail;
202         }
203         sc->ndis_rescnt++;
204         resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
205             rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
206
207         sc->ndis_iftype = PCMCIABus;
208
209         /* Figure out exactly which device we matched. */
210
211         t = ndis_devs;
212
213         error = pccard_get_product_str(dev, &prodstr);
214         if (error)
215                 return(error);
216         error = pccard_get_vendor_str(dev, &vendstr);
217         if (error)
218                 return(error);
219
220         while(t->ndis_name != NULL) {
221                 if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
222                     ndis_strcasecmp(prodstr, t->ndis_did) == 0)
223                         break;
224                 t++;
225                 devidx++;
226         }
227
228         sc->ndis_devidx = devidx;
229
230         error = ndis_attach(dev);
231
232 fail:
233         return(error);
234 }
235
236 static struct resource_list *
237 ndis_get_resource_list(device_t dev, device_t child)
238 {
239         struct ndis_softc       *sc;
240
241         sc = device_get_softc(dev);
242         return (&sc->ndis_rl);
243 }
244
245 #endif /* NDIS_PCI_DEV_TABLE */
246
247 #define NDIS_AM_RID 3
248
249 int
250 ndis_alloc_amem(void *arg)
251 {
252         struct ndis_softc       *sc;
253         int                     error, rid;
254
255         if (arg == NULL)
256                 return(EINVAL);
257
258         sc = arg;
259         rid = NDIS_AM_RID;
260         sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY,
261             &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
262
263         if (sc->ndis_res_am == NULL) {
264                 device_printf(sc->ndis_dev,
265                     "failed to allocate attribute memory\n");
266                 return(ENXIO);
267         }
268         sc->ndis_rescnt++;
269         resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
270             rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
271             rman_get_size(sc->ndis_res_am));
272
273         error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
274             sc->ndis_dev, rid, 0, NULL);
275
276         if (error) {
277                 device_printf(sc->ndis_dev,
278                     "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
279                 return(error);
280         }
281
282         error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
283             sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
284
285         if (error) {
286                 device_printf(sc->ndis_dev,
287                     "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
288                 return(error);
289         }
290
291         sc->ndis_am_rid = rid;
292
293         return(0);
294 }
295
296 void
297 ndis_free_amem(void *arg)
298 {
299         struct ndis_softc       *sc;
300
301         if (arg == NULL)
302                 return;
303
304         sc = arg;
305
306         if (sc->ndis_res_am != NULL)
307                 bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
308                     sc->ndis_am_rid, sc->ndis_res_am);
309         resource_list_free(&sc->ndis_rl);
310
311         return;
312 }