Sync ndis(4) and tools with FreeBSD and hook it all back into the build.
[dragonfly.git] / usr.sbin / ndiscvt / windrv_stub.c
1 /*-
2  * Copyright (c) 2005
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/usr.sbin/ndiscvt/windrv_stub.c,v 1.3 2008/12/27 08:03:32 weongyo Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/conf.h>
40
41 #include <sys/bus.h>
42
43 #define NDIS_REGVALS
44
45 struct ndis_cfg {
46         char                    *nc_cfgkey;
47         char                    *nc_cfgdesc;
48         char                    nc_val[256];
49         int                     nc_idx;
50 };
51
52 typedef struct ndis_cfg ndis_cfg;
53
54 #include "windrv.h"
55
56 struct ndis_pci_type {
57         uint16_t                ndis_vid;
58         uint16_t                ndis_did;
59         uint32_t                ndis_subsys;
60         char                    *ndis_name;
61 };
62
63 struct ndis_pccard_type {
64         const char              *ndis_vid;
65         const char              *ndis_did;
66         char                    *ndis_name;
67 };
68
69 struct ndis_usb_type {
70         uint16_t                ndis_vid;
71         uint16_t                ndis_did;
72         char                    *ndis_name;
73 };
74
75 #ifdef NDIS_PCI_DEV_TABLE
76 static struct ndis_pci_type ndis_devs_pci[] = {
77         NDIS_PCI_DEV_TABLE
78         { 0, 0, 0, NULL }
79 };
80 #endif
81
82 #ifdef NDIS_PCMCIA_DEV_TABLE
83 static struct ndis_pccard_type ndis_devs_pccard[] = {
84         NDIS_PCMCIA_DEV_TABLE
85         { NULL, NULL, NULL }
86 };
87 #endif
88
89 #ifdef NDIS_USB_DEV_TABLE
90 static struct ndis_usb_type ndis_devs_usb[] = {
91         NDIS_USB_DEV_TABLE
92         { 0, 0, NULL }
93 };
94 #endif
95
96 enum interface_type {
97         InterfaceTypeUndefined = -1,
98         Internal,
99         Isa,
100         Eisa,
101         MicroChannel,
102         TurboChannel,
103         PCIBus,
104         VMEBus,
105         NuBus,
106         PCMCIABus,
107         CBus,
108         MPIBus,
109         MPSABus,
110         ProcessorInternal,
111         InternalPowerBus,
112         PNPISABus,
113         PNPBus,
114         MaximumInterfaceType
115 };
116
117 typedef enum interface_type interface_type;
118
119 /*
120  * XXX
121  * Ordinarily, device_probe_desc is defined in device_if.h, which
122  * is created from device_if.m. The problem is, the latter file
123  * is only available if you have the kernel source code installed,
124  * and not all users choose to install it. I'd like to let people
125  * load Windows driver modules with the minimal amount of hassle
126  * and dependencies. <sys/bus.h> wants both device_if.h and bus_if.h
127  * to be defined, but it turns out the only thing we really need
128  * to get this module compiled is device_probe_desc, so we define
129  * that here, and let the build script create empty copies of
130  * device_if.h and bus_if.h to make the compiler happy.
131  */
132
133 extern struct kobjop_desc device_probe_desc;
134 typedef int device_probe_t(device_t dev);
135
136 extern int windrv_load(module_t, vm_offset_t, size_t,
137         interface_type, void *, void *);
138 extern int windrv_unload(module_t, vm_offset_t, size_t);
139
140 #ifndef DRV_DATA_START
141 #define DRV_DATA_START UNDEF_START
142 #endif
143
144 #ifndef DRV_DATA_END
145 #define DRV_DATA_END UNDEF_END
146 #endif
147
148 #ifndef DRV_NAME
149 #define DRV_NAME UNDEF_NAME
150 #endif
151
152 extern uint8_t DRV_DATA_START;
153 extern uint8_t DRV_DATA_END;
154
155 /*
156  * The following is stub code that makes it look as though we want
157  * to be a child device of all the buses that our supported devices
158  * might want to attach to. Our probe routine always fails. The
159  * reason we need this code is so that loading an ELF-ified Windows
160  * driver module will trigger a bus reprobe.
161  */
162
163 #define MODULE_DECL(x)                          \
164         MODULE_DEPEND(x, ndis, 1, 1, 1);        \
165         MODULE_DEPEND(x, if_ndis, 1, 1, 1)
166
167 MODULE_DECL(DRV_NAME);
168
169 static int windrv_probe(device_t);
170 static int windrv_modevent(module_t, int, void *);
171 static int windrv_loaded = 0;
172
173 static device_method_t windrv_methods[] = {
174         /* Device interface */
175         DEVMETHOD(device_probe,         windrv_probe),
176
177         { 0, 0 }
178 };
179
180 static driver_t windrv_driver = {
181         "windrv_stub",
182         windrv_methods,
183         0
184 };
185
186 static devclass_t windrv_devclass;
187
188 #define DRIVER_DECL(x)                                  \
189         DRIVER_MODULE(x, pci, windrv_driver,            \
190             windrv_devclass, windrv_modevent, NULL);    \
191         DRIVER_MODULE(x, cardbus, windrv_driver,        \
192             windrv_devclass, windrv_modevent, NULL);    \
193         DRIVER_MODULE(x, pccard, windrv_driver,         \
194             windrv_devclass, windrv_modevent, NULL);    \
195         DRIVER_MODULE(x, uhub, windrv_driver,           \
196             windrv_devclass, windrv_modevent, NULL);    \
197         MODULE_VERSION(x, 1)
198
199 DRIVER_DECL(DRV_NAME);
200
201 static int
202 windrv_probe(device_t dev)
203 {
204         return (ENXIO);
205 }
206
207 static int
208 windrv_modevent(module_t mod, int cmd, void *arg)
209 {
210         int                     drv_data_len;
211         int                     error = 0;
212         vm_offset_t             drv_data_start;
213         vm_offset_t             drv_data_end;
214
215         drv_data_start = (vm_offset_t)&DRV_DATA_START;
216         drv_data_end = (vm_offset_t)&DRV_DATA_END;
217
218         drv_data_len = drv_data_end - drv_data_start;
219         switch (cmd) {
220         case MOD_LOAD:
221                 windrv_loaded++;
222                 if (windrv_loaded > 1)
223                         break;
224 #ifdef NDIS_PCI_DEV_TABLE
225                 windrv_load(mod, drv_data_start, drv_data_len, PCIBus,
226                     ndis_devs_pci, &ndis_regvals);
227 #endif
228 #ifdef NDIS_PCMCIA_DEV_TABLE
229                 windrv_load(mod, drv_data_start, drv_data_len, PCMCIABus,
230                     ndis_devs_pccard, &ndis_regvals);
231 #endif
232 #ifdef NDIS_USB_DEV_TABLE
233                 windrv_load(mod, drv_data_start, drv_data_len, PNPBus,
234                    ndis_devs_usb, &ndis_regvals);
235 #endif
236                 break;
237         case MOD_UNLOAD:
238                 windrv_loaded--;
239                 if (windrv_loaded > 0)
240                         break;
241 #ifdef NDIS_PCI_DEV_TABLE
242                 windrv_unload(mod, drv_data_start, drv_data_len);
243 #endif
244 #ifdef NDIS_PCMCIA_DEV_TABLE
245                 windrv_unload(mod, drv_data_start, drv_data_len);
246 #endif
247 #ifdef NDIS_USB_DEV_TABLE
248                 windrv_unload(mod, drv_data_start, drv_data_len);
249 #endif
250                 break;
251         case MOD_SHUTDOWN:
252                 break;
253         default:
254                 error = EINVAL;
255                 break;
256         }
257
258         return (error);
259 }