]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/if_ndis/if_ndis_usb.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / if_ndis / if_ndis_usb.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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50
51 #include <net/bpf.h>
52
53 #include <sys/bus.h>
54 #include <machine/bus.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57
58 #include <net80211/ieee80211_var.h>
59
60 #include <compat/ndis/pe_var.h>
61 #include <compat/ndis/cfg_var.h>
62 #include <compat/ndis/resource_var.h>
63 #include <compat/ndis/ntoskrnl_var.h>
64 #include <compat/ndis/ndis_var.h>
65 #include <compat/ndis/usbd_var.h>
66 #include <dev/if_ndis/if_ndisvar.h>
67
68 SYSCTL_NODE(_hw, OID_AUTO, ndisusb, CTLFLAG_RD, 0, "NDIS USB driver parameters");
69
70 MODULE_DEPEND(ndis, usb, 1, 1, 1);
71
72 static device_probe_t ndisusb_match;
73 static device_attach_t ndisusb_attach;
74 static device_detach_t ndisusb_detach;
75 static bus_get_resource_list_t ndis_get_resource_list;
76
77 extern int ndisdrv_modevent     (module_t, int, void *);
78 extern int ndis_attach          (device_t);
79 extern int ndis_shutdown        (device_t);
80 extern int ndis_detach          (device_t);
81 extern int ndis_suspend         (device_t);
82 extern int ndis_resume          (device_t);
83
84 extern unsigned char drv_data[];
85
86 static device_method_t ndis_methods[] = {
87         /* Device interface */
88         DEVMETHOD(device_probe,         ndisusb_match),
89         DEVMETHOD(device_attach,        ndisusb_attach),
90         DEVMETHOD(device_detach,        ndisusb_detach),
91         DEVMETHOD(device_shutdown,      ndis_shutdown),
92
93         /* bus interface */
94         DEVMETHOD(bus_print_child,      bus_generic_print_child),
95         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
96         DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
97
98         { 0, 0 }
99 };
100
101 static driver_t ndis_driver = {
102         "ndis",
103         ndis_methods,
104         sizeof(struct ndis_softc)
105 };
106
107 static devclass_t ndis_devclass;
108
109 DRIVER_MODULE(ndis, uhub, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
110
111 static int
112 ndisusb_devcompare(interface_type bustype, struct ndis_usb_type *t, device_t dev)
113 {
114         struct usb_attach_arg *uaa;
115
116         if (bustype != PNPBus)
117                 return (FALSE);
118
119         uaa = device_get_ivars(dev);
120
121         while (t->ndis_name != NULL) {
122                 if ((uaa->info.idVendor == t->ndis_vid) &&
123                     (uaa->info.idProduct == t->ndis_did)) {
124                         device_set_desc(dev, t->ndis_name);
125                         return (TRUE);
126                 }
127                 t++;
128         }
129
130         return (FALSE);
131 }
132
133 static int
134 ndisusb_match(device_t self)
135 {
136         struct drvdb_ent *db;
137         struct usb_attach_arg *uaa = device_get_ivars(self);
138
139         if (uaa->usb_mode != USB_MODE_HOST)
140                 return (ENXIO);
141         if (uaa->info.bConfigIndex != NDISUSB_CONFIG_NO)
142                 return (ENXIO);
143         if (uaa->info.bIfaceIndex != NDISUSB_IFACE_INDEX)
144                 return (ENXIO);
145
146         if (windrv_lookup(0, "USB Bus") == NULL)
147                 return (ENXIO);
148
149         db = windrv_match((matchfuncptr)ndisusb_devcompare, self);
150         if (db == NULL)
151                 return (ENXIO);
152         uaa->driver_ivar = db;
153
154         return (0);
155 }
156
157 static int
158 ndisusb_attach(device_t self)
159 {
160         const struct drvdb_ent  *db;
161         struct ndisusb_softc *dummy = device_get_softc(self);
162         struct usb_attach_arg *uaa = device_get_ivars(self);
163         struct ndis_softc       *sc;
164         struct ndis_usb_type    *t;
165         driver_object           *drv;
166         int                     devidx = 0;
167
168         device_set_usb_desc(self);
169         db = uaa->driver_ivar;
170         sc = (struct ndis_softc *)dummy;
171         sc->ndis_dev = self;
172         mtx_init(&sc->ndisusb_mtx, "NDIS USB", MTX_NETWORK_LOCK, MTX_DEF);
173         sc->ndis_dobj = db->windrv_object;
174         sc->ndis_regvals = db->windrv_regvals;
175         sc->ndis_iftype = PNPBus;
176         sc->ndisusb_dev = uaa->device;
177
178         /* Create PDO for this device instance */
179
180         drv = windrv_lookup(0, "USB Bus");
181         windrv_create_pdo(drv, self);
182
183         /* Figure out exactly which device we matched. */
184
185         t = db->windrv_devlist;
186
187         while (t->ndis_name != NULL) {
188                 if ((uaa->info.idVendor == t->ndis_vid) &&
189                     (uaa->info.idProduct == t->ndis_did)) {
190                         sc->ndis_devidx = devidx;
191                         break;
192                 }
193                 t++;
194                 devidx++;
195         }
196
197         if (ndis_attach(self) != 0)
198                 return ENXIO;
199
200         return 0;
201 }
202
203 static int
204 ndisusb_detach(device_t self)
205 {
206         int i;
207         struct ndis_softc       *sc = device_get_softc(self);
208         struct ndisusb_ep       *ne;
209
210         sc->ndisusb_status |= NDISUSB_STATUS_DETACH;
211
212         ndis_pnpevent_nic(self, NDIS_PNP_EVENT_SURPRISE_REMOVED);
213
214         if (sc->ndisusb_status & NDISUSB_STATUS_SETUP_EP) {
215                 usbd_transfer_unsetup(sc->ndisusb_dread_ep.ne_xfer, 1);
216                 usbd_transfer_unsetup(sc->ndisusb_dwrite_ep.ne_xfer, 1);
217         }
218         for (i = 0; i < NDISUSB_ENDPT_MAX; i++) {
219                 ne = &sc->ndisusb_ep[i];
220                 usbd_transfer_unsetup(ne->ne_xfer, 1);
221         }
222
223         (void)ndis_detach(self);
224
225         mtx_destroy(&sc->ndisusb_mtx);
226         return (0);
227 }
228
229 static struct resource_list *
230 ndis_get_resource_list(device_t dev, device_t child)
231 {
232         struct ndis_softc       *sc;
233
234         sc = device_get_softc(dev);
235         return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
236 }