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