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