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