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