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