if_ndis_usb.c revision 192984
142680Smsmith/*-
242680Smsmith * Copyright (c) 2005
342680Smsmith *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
442680Smsmith *
542680Smsmith * Redistribution and use in source and binary forms, with or without
642680Smsmith * modification, are permitted provided that the following conditions
742680Smsmith * are met:
842680Smsmith * 1. Redistributions of source code must retain the above copyright
942680Smsmith *    notice, this list of conditions and the following disclaimer.
1042680Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1142680Smsmith *    notice, this list of conditions and the following disclaimer in the
1242680Smsmith *    documentation and/or other materials provided with the distribution.
1342680Smsmith * 3. All advertising materials mentioning features or use of this software
1442680Smsmith *    must display the following acknowledgement:
1542680Smsmith *      This product includes software developed by Bill Paul.
1642680Smsmith * 4. Neither the name of the author nor the names of any co-contributors
1742680Smsmith *    may be used to endorse or promote products derived from this software
1842680Smsmith *    without specific prior written permission.
1942680Smsmith *
2042680Smsmith * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2142680Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2242680Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2342680Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2442680Smsmith * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2542680Smsmith * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2642680Smsmith * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2742680Smsmith * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2842680Smsmith * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2942680Smsmith * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3042680Smsmith * THE POSSIBILITY OF SUCH DAMAGE.
3142680Smsmith */
3242680Smsmith
3352757Sphk#include <sys/cdefs.h>
3452757Sphk__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis_usb.c 192984 2009-05-28 17:36:36Z thompsa $");
3542680Smsmith
3642680Smsmith#include <sys/param.h>
37116182Sobrien#include <sys/systm.h>
38116182Sobrien#include <sys/sockio.h>
39116182Sobrien#include <sys/module.h>
4042680Smsmith#include <sys/malloc.h>
4142680Smsmith#include <sys/kernel.h>
4252843Sphk#include <sys/socket.h>
43114216Skan#include <sys/sysctl.h>
4442680Smsmith
4542680Smsmith#include <net/if.h>
4642680Smsmith#include <net/if_arp.h>
4742680Smsmith#include <net/ethernet.h>
4842680Smsmith#include <net/if_dl.h>
4942680Smsmith#include <net/if_media.h>
5042680Smsmith
5142680Smsmith#include <net/bpf.h>
5242680Smsmith
5342680Smsmith#include <sys/bus.h>
5442680Smsmith#include <machine/bus.h>
5542680Smsmith#include <dev/usb/usb.h>
5642680Smsmith#include <dev/usb/usb_core.h>
5742680Smsmith
5842680Smsmith#include <net80211/ieee80211_var.h>
5942680Smsmith
6042680Smsmith#include <compat/ndis/pe_var.h>
6142680Smsmith#include <compat/ndis/cfg_var.h>
62231697Sken#include <compat/ndis/resource_var.h>
6342680Smsmith#include <compat/ndis/ntoskrnl_var.h>
6442680Smsmith#include <compat/ndis/ndis_var.h>
6542680Smsmith#include <compat/ndis/usbd_var.h>
6642680Smsmith#include <dev/if_ndis/if_ndisvar.h>
6742680Smsmith
6842680SmsmithSYSCTL_NODE(_hw, OID_AUTO, ndisusb, CTLFLAG_RD, 0, "NDIS USB driver parameters");
6942680Smsmith
7042680SmsmithMODULE_DEPEND(ndis, usb, 1, 1, 1);
7142680Smsmith
7242680Smsmithstatic device_probe_t ndisusb_match;
7342680Smsmithstatic device_attach_t ndisusb_attach;
7442680Smsmithstatic device_detach_t ndisusb_detach;
7542680Smsmithstatic bus_get_resource_list_t ndis_get_resource_list;
7642680Smsmith
7742680Smsmithextern int ndisdrv_modevent     (module_t, int, void *);
7842680Smsmithextern int ndis_attach          (device_t);
7942680Smsmithextern int ndis_shutdown        (device_t);
8042680Smsmithextern int ndis_detach          (device_t);
8142680Smsmithextern int ndis_suspend         (device_t);
8242680Smsmithextern int ndis_resume          (device_t);
8342680Smsmith
8442680Smsmithextern unsigned char drv_data[];
8553648Sarchie
8642680Smsmithstatic device_method_t ndis_methods[] = {
8743300Sdillon        /* Device interface */
8842680Smsmith	DEVMETHOD(device_probe,		ndisusb_match),
8942680Smsmith	DEVMETHOD(device_attach,	ndisusb_attach),
9042680Smsmith	DEVMETHOD(device_detach,	ndisusb_detach),
9142680Smsmith	DEVMETHOD(device_shutdown,	ndis_shutdown),
9242680Smsmith
9342680Smsmith        /* bus interface */
9442680Smsmith	DEVMETHOD(bus_print_child,	bus_generic_print_child),
9542680Smsmith	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
9642680Smsmith	DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
9742680Smsmith
9842680Smsmith	{ 0, 0 }
9942680Smsmith};
10042680Smsmith
10142680Smsmithstatic driver_t ndis_driver = {
10242680Smsmith	"ndis",
10342680Smsmith	ndis_methods,
10442680Smsmith	sizeof(struct ndis_softc)
10543300Sdillon};
10642680Smsmith
10742680Smsmithstatic devclass_t ndis_devclass;
10842680Smsmith
10942680SmsmithDRIVER_MODULE(ndis, uhub, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
11042680Smsmith
11142680Smsmithstatic int
11242680Smsmithndisusb_devcompare(interface_type bustype, struct ndis_usb_type *t, device_t dev)
11342680Smsmith{
11442680Smsmith	struct usb_attach_arg *uaa;
11542680Smsmith
11642680Smsmith	if (bustype != PNPBus)
11742680Smsmith		return (FALSE);
11842680Smsmith
11942680Smsmith	uaa = device_get_ivars(dev);
12042680Smsmith
12142680Smsmith	while (t->ndis_name != NULL) {
12242680Smsmith		if ((uaa->info.idVendor == t->ndis_vid) &&
12342680Smsmith		    (uaa->info.idProduct == t->ndis_did)) {
12442680Smsmith			device_set_desc(dev, t->ndis_name);
12542680Smsmith			return (TRUE);
12642680Smsmith		}
12742680Smsmith		t++;
12842680Smsmith	}
12942680Smsmith
13042680Smsmith	return (FALSE);
13142680Smsmith}
13242680Smsmith
13342680Smsmithstatic int
13442680Smsmithndisusb_match(device_t self)
13542680Smsmith{
13642680Smsmith	struct drvdb_ent *db;
13742680Smsmith	struct usb_attach_arg *uaa = device_get_ivars(self);
13842680Smsmith
13942680Smsmith	if (uaa->usb_mode != USB_MODE_HOST)
14042680Smsmith		return (ENXIO);
14142680Smsmith	if (uaa->info.bConfigIndex != NDISUSB_CONFIG_NO)
14242680Smsmith		return (ENXIO);
14342680Smsmith	if (uaa->info.bIfaceIndex != NDISUSB_IFACE_INDEX)
14442680Smsmith		return (ENXIO);
14542680Smsmith
14642680Smsmith	if (windrv_lookup(0, "USB Bus") == NULL)
14742680Smsmith		return (ENXIO);
14842680Smsmith
14942680Smsmith	db = windrv_match((matchfuncptr)ndisusb_devcompare, self);
15042680Smsmith	if (db == NULL)
15142680Smsmith		return (ENXIO);
15242680Smsmith	uaa->driver_info = db;
15342680Smsmith
15442680Smsmith	return (0);
15542680Smsmith}
15642680Smsmith
15742680Smsmithstatic int
15842680Smsmithndisusb_attach(device_t self)
15942680Smsmith{
16042680Smsmith	const struct drvdb_ent	*db;
16142680Smsmith	struct ndisusb_softc *dummy = device_get_softc(self);
16242680Smsmith	struct usb_attach_arg *uaa = device_get_ivars(self);
16342680Smsmith	struct ndis_softc	*sc;
164231697Sken	struct ndis_usb_type	*t;
165231697Sken	driver_object		*drv;
166231697Sken	int			devidx = 0;
167231697Sken
168231697Sken	db = uaa->driver_info;
169231697Sken	sc = (struct ndis_softc *)dummy;
17042680Smsmith	sc->ndis_dev = self;
17142680Smsmith	mtx_init(&sc->ndisusb_mtx, "NDIS USB", MTX_NETWORK_LOCK, MTX_DEF);
17242680Smsmith	sc->ndis_dobj = db->windrv_object;
17342680Smsmith	sc->ndis_regvals = db->windrv_regvals;
17442680Smsmith	sc->ndis_iftype = PNPBus;
175231697Sken	sc->ndisusb_dev = uaa->device;
176231697Sken
177231697Sken	/* Create PDO for this device instance */
178231697Sken
179231697Sken	drv = windrv_lookup(0, "USB Bus");
180231697Sken	windrv_create_pdo(drv, self);
18142680Smsmith
18242680Smsmith	/* Figure out exactly which device we matched. */
18342680Smsmith
18442680Smsmith	t = db->windrv_devlist;
18542680Smsmith
18642680Smsmith	while (t->ndis_name != NULL) {
18742680Smsmith		if ((uaa->info.idVendor == t->ndis_vid) &&
18842680Smsmith		    (uaa->info.idProduct == t->ndis_did)) {
18942680Smsmith			sc->ndis_devidx = devidx;
19042680Smsmith			break;
19142680Smsmith		}
19242680Smsmith		t++;
19342680Smsmith		devidx++;
19442680Smsmith	}
19542680Smsmith
19642680Smsmith	if (ndis_attach(self) != 0)
19742680Smsmith		return ENXIO;
19842680Smsmith
19942680Smsmith	return 0;
20042680Smsmith}
20142680Smsmith
20242680Smsmithstatic int
20342680Smsmithndisusb_detach(device_t self)
20442680Smsmith{
20542680Smsmith	int i;
20642680Smsmith	struct ndis_softc       *sc = device_get_softc(self);
20742680Smsmith	struct ndisusb_ep	*ne;;
20842680Smsmith
20942680Smsmith	sc->ndisusb_status |= NDISUSB_STATUS_DETACH;
21042680Smsmith
21142680Smsmith	ndis_pnpevent_nic(self, NDIS_PNP_EVENT_SURPRISE_REMOVED);
21242680Smsmith
21342680Smsmith	if (sc->ndisusb_status & NDISUSB_STATUS_SETUP_EP) {
21442680Smsmith		usb2_transfer_unsetup(sc->ndisusb_dread_ep.ne_xfer, 1);
21542680Smsmith		usb2_transfer_unsetup(sc->ndisusb_dwrite_ep.ne_xfer, 1);
21642680Smsmith	}
21742680Smsmith	for (i = 0; i < NDISUSB_ENDPT_MAX; i++) {
21842680Smsmith		ne = &sc->ndisusb_ep[i];
21942680Smsmith		usb2_transfer_unsetup(ne->ne_xfer, 1);
22042680Smsmith	}
22142680Smsmith
22242680Smsmith	(void)ndis_detach(self);
22342680Smsmith
22442680Smsmith	mtx_destroy(&sc->ndisusb_mtx);
22542680Smsmith	return (0);
22642680Smsmith}
22742680Smsmith
22842680Smsmithstatic struct resource_list *
22942680Smsmithndis_get_resource_list(device_t dev, device_t child)
23042680Smsmith{
23142680Smsmith	struct ndis_softc       *sc;
23242680Smsmith
23342680Smsmith	sc = device_get_softc(dev);
23442680Smsmith	return (BUS_GET_RESOURCE_LIST(device_get_parent(sc->ndis_dev), dev));
23542680Smsmith}
23642680Smsmith