5dccea140c07e350e7def6dd1353b3a2c7e315fd
[dragonfly.git] / sys / dev / netif / 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  * $FreeBSD: src/sys/dev/if_ndis/if_ndis_usb.c,v 1.10 2008/12/27 08:03:32 weongyo Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sockio.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.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/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 <bus/usb/usb.h>
55 #include <bus/usb/usbdi.h>
56 #include <bus/usb/usbdi_util.h>
57 #include <bus/usb/usbdivar.h>
58
59 #include <netproto/802_11/ieee80211_var.h>
60
61 #include <emulation/ndis/pe_var.h>
62 #include <emulation/ndis/cfg_var.h>
63 #include <emulation/ndis/resource_var.h>
64 #include <emulation/ndis/ntoskrnl_var.h>
65 #include <emulation/ndis/ndis_var.h>
66 #include <emulation/ndis/usbd_var.h>
67 #include <dev/netif/ndis/if_ndisvar.h>
68
69 SYSCTL_NODE(_hw, OID_AUTO, ndisusb, CTLFLAG_RD, 0, "NDIS USB driver parameters");
70
71 MODULE_DEPEND(if_ndis, usb, 1, 1, 1);
72
73 static device_probe_t ndisusb_match;
74 static device_attach_t ndisusb_attach;
75 static device_detach_t ndisusb_detach;
76 static bus_get_resource_list_t ndis_get_resource_list;
77
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,         ndisusb_match),
90         DEVMETHOD(device_attach,        ndisusb_attach),
91         DEVMETHOD(device_detach,        ndisusb_detach),
92         DEVMETHOD(device_shutdown,      ndis_shutdown),
93
94         /* bus interface */
95         DEVMETHOD(bus_print_child,      bus_generic_print_child),
96         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
97         DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
98
99         DEVMETHOD_END
100 };
101
102 static driver_t ndis_driver = {
103         "ndis",
104         ndis_methods,
105         sizeof(struct ndis_softc)
106 };
107
108 static devclass_t ndis_devclass;
109
110 DRIVER_MODULE(if_ndis, uhub, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
111
112 static int
113 ndisusb_devcompare(interface_type bustype, struct ndis_usb_type *t, device_t dev)
114 {
115         struct usb_attach_arg *uaa;
116
117         if (bustype != PNPBus)
118                 return (FALSE);
119
120         uaa = device_get_ivars(dev);
121
122         while (t->ndis_name != NULL) {
123                 if ((uaa->vendor == t->ndis_vid) &&
124                     (uaa->product == t->ndis_did)) {
125                         device_set_desc(dev, t->ndis_name);
126                         return (TRUE);
127                 }
128                 t++;
129         }
130
131         return (FALSE);
132 }
133
134 static int
135 ndisusb_match(device_t self)
136 {
137         struct drvdb_ent *db;
138         struct usb_attach_arg *uaa = device_get_ivars(self);
139
140         if (windrv_lookup(0, "USB Bus") == NULL)
141                 return (UMATCH_NONE);
142
143         if (uaa->iface != NULL)
144                 return (UMATCH_NONE);
145
146         db = windrv_match((matchfuncptr)ndisusb_devcompare, self);
147         if (db == NULL)
148                 return (UMATCH_NONE);
149
150         return (UMATCH_VENDOR_PRODUCT);
151 }
152
153 static int
154 ndisusb_attach(device_t self)
155 {
156         struct drvdb_ent        *db;
157         struct ndisusb_softc *dummy = device_get_softc(self);
158         struct usb_attach_arg *uaa = device_get_ivars(self);
159         struct ndis_softc       *sc;
160         struct ndis_usb_type    *t;
161         driver_object           *drv;
162         int                     devidx = 0;
163         usbd_status             status;
164
165         wlan_serialize_enter();
166         sc = (struct ndis_softc *)dummy;
167
168         if (uaa->device == NULL) {
169                 wlan_serialize_exit();
170                 return ENXIO;
171         }
172
173         db = windrv_match((matchfuncptr)ndisusb_devcompare, self);
174         if (db == NULL) {
175                 wlan_serialize_exit();
176                 return (ENXIO);
177         }
178
179         sc->ndis_dev = self;
180         sc->ndis_dobj = db->windrv_object;
181         sc->ndis_regvals = db->windrv_regvals;
182         sc->ndis_iftype = PNPBus;
183
184         /* Create PDO for this device instance */
185
186         drv = windrv_lookup(0, "USB Bus");
187         windrv_create_pdo(drv, self);
188
189         status = usbd_set_config_no(uaa->device, NDISUSB_CONFIG_NO, 0);
190         if (status != USBD_NORMAL_COMPLETION) {
191                 device_printf(self, "setting config no failed\n");
192                 wlan_serialize_exit();
193                 return (ENXIO);
194         }
195
196         /* Figure out exactly which device we matched. */
197
198         t = db->windrv_devlist;
199
200         while (t->ndis_name != NULL) {
201                 if ((uaa->vendor == t->ndis_vid) &&
202                     (uaa->product == t->ndis_did)) {
203                         sc->ndis_devidx = devidx;
204                         break;
205                 }
206                 t++;
207                 devidx++;
208         }
209
210         if (ndis_attach(self) != 0) {
211                 wlan_serialize_exit();
212                 return ENXIO;
213         }
214
215         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, uaa->device, self);
216
217         wlan_serialize_exit();
218         return 0;
219 }
220
221 static int
222 ndisusb_detach(device_t self)
223 {
224         int i, error;
225         struct ndis_softc       *sc = device_get_softc(self);
226         struct usb_attach_arg *uaa = device_get_ivars(self);
227
228         wlan_serialize_enter();
229         sc->ndisusb_status |= NDISUSB_STATUS_DETACH;
230
231         for (i = 0; i < NDISUSB_ENDPT_MAX; i++) {
232                 if (sc->ndisusb_ep[i] == NULL)
233                         continue;
234
235                 usbd_abort_pipe(sc->ndisusb_ep[i]);
236                 usbd_close_pipe(sc->ndisusb_ep[i]);
237                 sc->ndisusb_ep[i] = NULL;
238         }
239
240         if (sc->ndisusb_iin_buf != NULL) {
241                 kfree(sc->ndisusb_iin_buf, M_USBDEV);
242                 sc->ndisusb_iin_buf = NULL;
243         }
244
245         ndis_pnpevent_nic(self, NDIS_PNP_EVENT_SURPRISE_REMOVED);
246
247         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, uaa->device, self);
248
249         error = ndis_detach(self);
250
251         wlan_serialize_exit();
252         return error;
253 }
254
255 static struct resource_list *
256 ndis_get_resource_list(device_t dev, device_t child)
257 {
258         struct ndis_softc       *sc;
259
260         sc = device_get_softc(dev);
261         return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
262 }