Home | History | Annotate | Line # | Download | only in usb
if_kue.c revision 1.82
      1 /*	$NetBSD: if_kue.c,v 1.82 2015/04/13 16:33:25 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999, 2000
      5  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
     35  */
     36 
     37 /*
     38  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
     39  *
     40  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     41  * Electrical Engineering Department
     42  * Columbia University, New York City
     43  */
     44 
     45 /*
     46  * The KLSI USB to ethernet adapter chip contains an USB serial interface,
     47  * ethernet MAC and embedded microcontroller (called the QT Engine).
     48  * The chip must have firmware loaded into it before it will operate.
     49  * Packets are passed between the chip and host via bulk transfers.
     50  * There is an interrupt endpoint mentioned in the software spec, however
     51  * it's currently unused. This device is 10Mbps half-duplex only, hence
     52  * there is no media selection logic. The MAC supports a 128 entry
     53  * multicast filter, though the exact size of the filter can depend
     54  * on the firmware. Curiously, while the software spec describes various
     55  * ethernet statistics counters, my sample adapter and firmware combination
     56  * claims not to support any statistics counters at all.
     57  *
     58  * Note that once we load the firmware in the device, we have to be
     59  * careful not to load it again: if you restart your computer but
     60  * leave the adapter attached to the USB controller, it may remain
     61  * powered on and retain its firmware. In this case, we don't need
     62  * to load the firmware a second time.
     63  *
     64  * Special thanks to Rob Furr for providing an ADS Technologies
     65  * adapter for development and testing. No monkeys were harmed during
     66  * the development of this driver.
     67  */
     68 
     69 /*
     70  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.82 2015/04/13 16:33:25 riastradh Exp $");
     75 
     76 #ifdef _KERNEL_OPT
     77 #include "opt_inet.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/sockio.h>
     83 #include <sys/mbuf.h>
     84 #include <sys/malloc.h>
     85 #include <sys/kernel.h>
     86 #include <sys/socket.h>
     87 #include <sys/device.h>
     88 #include <sys/proc.h>
     89 #include <sys/rndsource.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_arp.h>
     93 #include <net/if_dl.h>
     94 #include <net/bpf.h>
     95 #include <net/if_ether.h>
     96 
     97 #ifdef INET
     98 #include <netinet/in.h>
     99 #include <netinet/if_inarp.h>
    100 #endif
    101 
    102 #include <dev/usb/usb.h>
    103 #include <dev/usb/usbdi.h>
    104 #include <dev/usb/usbdi_util.h>
    105 #include <dev/usb/usbdivar.h>
    106 #include <dev/usb/usbdevs.h>
    107 
    108 #include <dev/usb/if_kuereg.h>
    109 #include <dev/usb/kue_fw.h>
    110 
    111 #ifdef KUE_DEBUG
    112 #define DPRINTF(x)	if (kuedebug) printf x
    113 #define DPRINTFN(n,x)	if (kuedebug >= (n)) printf x
    114 int	kuedebug = 0;
    115 #else
    116 #define DPRINTF(x)
    117 #define DPRINTFN(n,x)
    118 #endif
    119 
    120 /*
    121  * Various supported device vendors/products.
    122  */
    123 static const struct usb_devno kue_devs[] = {
    124 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
    125 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
    126 	{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
    127 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
    128 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
    129 	{ USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
    130 	{ USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
    131 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
    132 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
    133 	{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
    134 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
    135 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
    136 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
    137 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
    138 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
    139 	{ USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
    140 	{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
    141 	{ USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
    142 	{ USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
    143 	{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
    144 	{ USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
    145 	{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
    146 	{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
    147 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
    148 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
    149 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
    150 	{ USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
    151 	{ USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
    152 	{ USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
    153 	{ USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
    154 	{ USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
    155 	{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
    156 };
    157 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p))
    158 
    159 int kue_match(device_t, cfdata_t, void *);
    160 void kue_attach(device_t, device_t, void *);
    161 int kue_detach(device_t, int);
    162 int kue_activate(device_t, enum devact);
    163 extern struct cfdriver kue_cd;
    164 CFATTACH_DECL_NEW(kue, sizeof(struct kue_softc), kue_match, kue_attach,
    165     kue_detach, kue_activate);
    166 
    167 static int kue_tx_list_init(struct kue_softc *);
    168 static int kue_rx_list_init(struct kue_softc *);
    169 static int kue_send(struct kue_softc *, struct mbuf *, int);
    170 static int kue_open_pipes(struct kue_softc *);
    171 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    172 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    173 static void kue_start(struct ifnet *);
    174 static int kue_ioctl(struct ifnet *, u_long, void *);
    175 static void kue_init(void *);
    176 static void kue_stop(struct kue_softc *);
    177 static void kue_watchdog(struct ifnet *);
    178 
    179 static void kue_setmulti(struct kue_softc *);
    180 static void kue_reset(struct kue_softc *);
    181 
    182 static usbd_status kue_ctl(struct kue_softc *, int, uint8_t,
    183 			   uint16_t, void *, uint32_t);
    184 static usbd_status kue_setword(struct kue_softc *, uint8_t, uint16_t);
    185 static int kue_load_fw(struct kue_softc *);
    186 
    187 static usbd_status
    188 kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
    189 {
    190 	usb_device_request_t	req;
    191 
    192 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
    193 
    194 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    195 	req.bRequest = breq;
    196 	USETW(req.wValue, word);
    197 	USETW(req.wIndex, 0);
    198 	USETW(req.wLength, 0);
    199 
    200 	return (usbd_do_request(sc->kue_udev, &req, NULL));
    201 }
    202 
    203 static usbd_status
    204 kue_ctl(struct kue_softc *sc, int rw, uint8_t breq, uint16_t val,
    205 	void *data, uint32_t len)
    206 {
    207 	usb_device_request_t	req;
    208 
    209 	DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(sc->kue_dev),
    210 		     __func__, len));
    211 
    212 	if (rw == KUE_CTL_WRITE)
    213 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    214 	else
    215 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    216 
    217 	req.bRequest = breq;
    218 	USETW(req.wValue, val);
    219 	USETW(req.wIndex, 0);
    220 	USETW(req.wLength, len);
    221 
    222 	return (usbd_do_request(sc->kue_udev, &req, data));
    223 }
    224 
    225 static int
    226 kue_load_fw(struct kue_softc *sc)
    227 {
    228 	usb_device_descriptor_t dd;
    229 	usbd_status		err;
    230 
    231 	DPRINTFN(1,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
    232 
    233 	/*
    234 	 * First, check if we even need to load the firmware.
    235 	 * If the device was still attached when the system was
    236 	 * rebooted, it may already have firmware loaded in it.
    237 	 * If this is the case, we don't need to do it again.
    238 	 * And in fact, if we try to load it again, we'll hang,
    239 	 * so we have to avoid this condition if we don't want
    240 	 * to look stupid.
    241 	 *
    242 	 * We can test this quickly by checking the bcdRevision
    243 	 * code. The NIC will return a different revision code if
    244 	 * it's probed while the firmware is still loaded and
    245 	 * running.
    246 	 */
    247 	if (usbd_get_device_desc(sc->kue_udev, &dd))
    248 		return (EIO);
    249 	if (UGETW(dd.bcdDevice) == KUE_WARM_REV) {
    250 		printf("%s: warm boot, no firmware download\n",
    251 		       device_xname(sc->kue_dev));
    252 		return (0);
    253 	}
    254 
    255 	printf("%s: cold boot, downloading firmware\n",
    256 	       device_xname(sc->kue_dev));
    257 
    258 	/* Load code segment */
    259 	DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
    260 		    device_xname(sc->kue_dev)));
    261 	/*XXXUNCONST*/
    262 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    263 	    0, __UNCONST(kue_code_seg), sizeof(kue_code_seg));
    264 	if (err) {
    265 		printf("%s: failed to load code segment: %s\n",
    266 		    device_xname(sc->kue_dev), usbd_errstr(err));
    267 			return (EIO);
    268 	}
    269 
    270 	/* Load fixup segment */
    271 	DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
    272 		    device_xname(sc->kue_dev)));
    273 	/*XXXUNCONST*/
    274 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    275 	    0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg));
    276 	if (err) {
    277 		printf("%s: failed to load fixup segment: %s\n",
    278 		    device_xname(sc->kue_dev), usbd_errstr(err));
    279 			return (EIO);
    280 	}
    281 
    282 	/* Send trigger command. */
    283 	DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
    284 		    device_xname(sc->kue_dev)));
    285 	/*XXXUNCONST*/
    286 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    287 	    0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg));
    288 	if (err) {
    289 		printf("%s: failed to load trigger segment: %s\n",
    290 		    device_xname(sc->kue_dev), usbd_errstr(err));
    291 			return (EIO);
    292 	}
    293 
    294 	usbd_delay_ms(sc->kue_udev, 10);
    295 
    296 	/*
    297 	 * Reload device descriptor.
    298 	 * Why? The chip without the firmware loaded returns
    299 	 * one revision code. The chip with the firmware
    300 	 * loaded and running returns a *different* revision
    301 	 * code. This confuses the quirk mechanism, which is
    302 	 * dependent on the revision data.
    303 	 */
    304 	(void)usbd_reload_device_desc(sc->kue_udev);
    305 
    306 	DPRINTFN(1,("%s: %s: done\n", device_xname(sc->kue_dev), __func__));
    307 
    308 	/* Reset the adapter. */
    309 	kue_reset(sc);
    310 
    311 	return (0);
    312 }
    313 
    314 static void
    315 kue_setmulti(struct kue_softc *sc)
    316 {
    317 	struct ifnet		*ifp = GET_IFP(sc);
    318 	struct ether_multi	*enm;
    319 	struct ether_multistep	step;
    320 	int			i;
    321 
    322 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
    323 
    324 	if (ifp->if_flags & IFF_PROMISC) {
    325 allmulti:
    326 		ifp->if_flags |= IFF_ALLMULTI;
    327 		sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
    328 		sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
    329 		kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
    330 		return;
    331 	}
    332 
    333 	sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
    334 
    335 	i = 0;
    336 	ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
    337 	while (enm != NULL) {
    338 		if (i == KUE_MCFILTCNT(sc) ||
    339 		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
    340 			ETHER_ADDR_LEN) != 0)
    341 			goto allmulti;
    342 
    343 		memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
    344 		ETHER_NEXT_MULTI(step, enm);
    345 		i++;
    346 	}
    347 
    348 	ifp->if_flags &= ~IFF_ALLMULTI;
    349 
    350 	sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
    351 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
    352 	    i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
    353 
    354 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
    355 }
    356 
    357 /*
    358  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
    359  * done after the firmware is loaded into the adapter in order to
    360  * bring it into proper operation.
    361  */
    362 static void
    363 kue_reset(struct kue_softc *sc)
    364 {
    365 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
    366 
    367 	if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) ||
    368 	    usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
    369 					 &sc->kue_iface))
    370 		printf("%s: reset failed\n", device_xname(sc->kue_dev));
    371 
    372 	/* Wait a little while for the chip to get its brains in order. */
    373 	usbd_delay_ms(sc->kue_udev, 10);
    374 }
    375 
    376 /*
    377  * Probe for a KLSI chip.
    378  */
    379 int
    380 kue_match(device_t parent, cfdata_t match, void *aux)
    381 {
    382 	struct usb_attach_arg *uaa = aux;
    383 
    384 	DPRINTFN(25,("kue_match: enter\n"));
    385 
    386 	return (kue_lookup(uaa->vendor, uaa->product) != NULL ?
    387 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    388 }
    389 
    390 /*
    391  * Attach the interface. Allocate softc structures, do
    392  * setup and ethernet/BPF attach.
    393  */
    394 void
    395 kue_attach(device_t parent, device_t self, void *aux)
    396 {
    397 	struct kue_softc *sc = device_private(self);
    398 	struct usb_attach_arg *uaa = aux;
    399 	char			*devinfop;
    400 	int			s;
    401 	struct ifnet		*ifp;
    402 	usbd_device_handle	dev = uaa->device;
    403 	usbd_interface_handle	iface;
    404 	usbd_status		err;
    405 	usb_interface_descriptor_t	*id;
    406 	usb_endpoint_descriptor_t	*ed;
    407 	int			i;
    408 
    409 	DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
    410 
    411 	sc->kue_dev = self;
    412 
    413 	aprint_naive("\n");
    414 	aprint_normal("\n");
    415 
    416 	devinfop = usbd_devinfo_alloc(dev, 0);
    417 	aprint_normal_dev(self, "%s\n", devinfop);
    418 	usbd_devinfo_free(devinfop);
    419 
    420 	err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
    421 	if (err) {
    422 		aprint_error_dev(self, "failed to set configuration"
    423 		    ", err=%s\n", usbd_errstr(err));
    424 		return;
    425 	}
    426 
    427 	sc->kue_udev = dev;
    428 	sc->kue_product = uaa->product;
    429 	sc->kue_vendor = uaa->vendor;
    430 
    431 	/* Load the firmware into the NIC. */
    432 	if (kue_load_fw(sc)) {
    433 		aprint_error_dev(self, "loading firmware failed\n");
    434 		return;
    435 	}
    436 
    437 	err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
    438 	if (err) {
    439 		aprint_error_dev(self, "getting interface handle failed\n");
    440 		return;
    441 	}
    442 
    443 	sc->kue_iface = iface;
    444 	id = usbd_get_interface_descriptor(iface);
    445 
    446 	/* Find endpoints. */
    447 	for (i = 0; i < id->bNumEndpoints; i++) {
    448 		ed = usbd_interface2endpoint_descriptor(iface, i);
    449 		if (ed == NULL) {
    450 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    451 			return;
    452 		}
    453 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    454 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    455 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
    456 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    457 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    458 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
    459 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    460 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    461 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
    462 		}
    463 	}
    464 
    465 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
    466 		aprint_error_dev(self, "missing endpoint\n");
    467 		return;
    468 	}
    469 
    470 	/* Read ethernet descriptor */
    471 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
    472 	    0, &sc->kue_desc, sizeof(sc->kue_desc));
    473 	if (err) {
    474 		aprint_error_dev(self, "could not read Ethernet descriptor\n");
    475 		return;
    476 	}
    477 
    478 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
    479 	    M_USBDEV, M_NOWAIT);
    480 	if (sc->kue_mcfilters == NULL) {
    481 		aprint_error_dev(self,
    482 		    "no memory for multicast filter buffer\n");
    483 		return;
    484 	}
    485 
    486 	s = splnet();
    487 
    488 	/*
    489 	 * A KLSI chip was detected. Inform the world.
    490 	 */
    491 	aprint_normal_dev(self, "Ethernet address %s\n",
    492 	    ether_sprintf(sc->kue_desc.kue_macaddr));
    493 
    494 	/* Initialize interface info.*/
    495 	ifp = GET_IFP(sc);
    496 	ifp->if_softc = sc;
    497 	ifp->if_mtu = ETHERMTU;
    498 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    499 	ifp->if_ioctl = kue_ioctl;
    500 	ifp->if_start = kue_start;
    501 	ifp->if_watchdog = kue_watchdog;
    502 	strncpy(ifp->if_xname, device_xname(sc->kue_dev), IFNAMSIZ);
    503 
    504 	IFQ_SET_READY(&ifp->if_snd);
    505 
    506 	/* Attach the interface. */
    507 	if_attach(ifp);
    508 	ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
    509 	rnd_attach_source(&sc->rnd_source, device_xname(sc->kue_dev),
    510 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    511 
    512 	sc->kue_attached = true;
    513 	splx(s);
    514 
    515 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
    516 			   sc->kue_dev);
    517 
    518 	return;
    519 }
    520 
    521 int
    522 kue_detach(device_t self, int flags)
    523 {
    524 	struct kue_softc *sc = device_private(self);
    525 	struct ifnet		*ifp = GET_IFP(sc);
    526 	int			s;
    527 
    528 	s = splusb();		/* XXX why? */
    529 
    530 	if (sc->kue_mcfilters != NULL) {
    531 		free(sc->kue_mcfilters, M_USBDEV);
    532 		sc->kue_mcfilters = NULL;
    533 	}
    534 
    535 	if (!sc->kue_attached) {
    536 		/* Detached before attached finished, so just bail out. */
    537 		splx(s);
    538 		return (0);
    539 	}
    540 
    541 	if (ifp->if_flags & IFF_RUNNING)
    542 		kue_stop(sc);
    543 
    544 	rnd_detach_source(&sc->rnd_source);
    545 	ether_ifdetach(ifp);
    546 
    547 	if_detach(ifp);
    548 
    549 #ifdef DIAGNOSTIC
    550 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
    551 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
    552 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
    553 		aprint_debug_dev(self, "detach has active endpoints\n");
    554 #endif
    555 
    556 	sc->kue_attached = false;
    557 	splx(s);
    558 
    559 	return (0);
    560 }
    561 
    562 int
    563 kue_activate(device_t self, enum devact act)
    564 {
    565 	struct kue_softc *sc = device_private(self);
    566 
    567 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
    568 
    569 	switch (act) {
    570 	case DVACT_DEACTIVATE:
    571 		/* Deactivate the interface. */
    572 		if_deactivate(&sc->kue_ec.ec_if);
    573 		sc->kue_dying = true;
    574 		return 0;
    575 	default:
    576 		return EOPNOTSUPP;
    577 	}
    578 }
    579 
    580 static int
    581 kue_rx_list_init(struct kue_softc *sc)
    582 {
    583 	struct kue_cdata	*cd;
    584 	struct kue_chain	*c;
    585 	int			i;
    586 
    587 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
    588 
    589 	cd = &sc->kue_cdata;
    590 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
    591 		c = &cd->kue_rx_chain[i];
    592 		c->kue_sc = sc;
    593 		c->kue_idx = i;
    594 		if (c->kue_xfer == NULL) {
    595 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    596 			if (c->kue_xfer == NULL)
    597 				return (ENOBUFS);
    598 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    599 			if (c->kue_buf == NULL)
    600 				return (ENOBUFS); /* XXX free xfer */
    601 		}
    602 	}
    603 
    604 	return (0);
    605 }
    606 
    607 static int
    608 kue_tx_list_init(struct kue_softc *sc)
    609 {
    610 	struct kue_cdata	*cd;
    611 	struct kue_chain	*c;
    612 	int			i;
    613 
    614 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
    615 
    616 	cd = &sc->kue_cdata;
    617 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
    618 		c = &cd->kue_tx_chain[i];
    619 		c->kue_sc = sc;
    620 		c->kue_idx = i;
    621 		if (c->kue_xfer == NULL) {
    622 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    623 			if (c->kue_xfer == NULL)
    624 				return (ENOBUFS);
    625 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    626 			if (c->kue_buf == NULL)
    627 				return (ENOBUFS);
    628 		}
    629 	}
    630 
    631 	return (0);
    632 }
    633 
    634 /*
    635  * A frame has been uploaded: pass the resulting mbuf chain up to
    636  * the higher level protocols.
    637  */
    638 static void
    639 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    640 {
    641 	struct kue_chain	*c = priv;
    642 	struct kue_softc	*sc = c->kue_sc;
    643 	struct ifnet		*ifp = GET_IFP(sc);
    644 	struct mbuf		*m;
    645 	int			total_len, pktlen;
    646 	int			s;
    647 
    648 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev),
    649 		     __func__, status));
    650 
    651 	if (sc->kue_dying)
    652 		return;
    653 
    654 	if (!(ifp->if_flags & IFF_RUNNING))
    655 		return;
    656 
    657 	if (status != USBD_NORMAL_COMPLETION) {
    658 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    659 			return;
    660 		sc->kue_rx_errs++;
    661 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
    662 			printf("%s: %u usb errors on rx: %s\n",
    663 			    device_xname(sc->kue_dev), sc->kue_rx_errs,
    664 			    usbd_errstr(status));
    665 			sc->kue_rx_errs = 0;
    666 		}
    667 		if (status == USBD_STALLED)
    668 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
    669 		goto done;
    670 	}
    671 
    672 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    673 
    674 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", device_xname(sc->kue_dev),
    675 		     __func__, total_len,
    676 		     le16dec(c->kue_buf)));
    677 
    678 	if (total_len <= 1)
    679 		goto done;
    680 
    681 	pktlen = le16dec(c->kue_buf);
    682 	if (pktlen > total_len - 2)
    683 		pktlen = total_len - 2;
    684 
    685 	if (pktlen < ETHER_MIN_LEN - ETHER_CRC_LEN ||
    686 	    pktlen > MCLBYTES - ETHER_ALIGN) {
    687 		ifp->if_ierrors++;
    688 		goto done;
    689 	}
    690 
    691 	/* No errors; receive the packet. */
    692 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    693 	if (m == NULL) {
    694 		ifp->if_ierrors++;
    695 		goto done;
    696 	}
    697 	if (pktlen > MHLEN - ETHER_ALIGN) {
    698 		MCLGET(m, M_DONTWAIT);
    699 		if ((m->m_flags & M_EXT) == 0) {
    700 			m_freem(m);
    701 			ifp->if_ierrors++;
    702 			goto done;
    703 		}
    704 	}
    705 	m->m_data += ETHER_ALIGN;
    706 
    707 	/* copy data to mbuf */
    708 	memcpy(mtod(m, uint8_t *), c->kue_buf + 2, pktlen);
    709 
    710 	ifp->if_ipackets++;
    711 	m->m_pkthdr.len = m->m_len = pktlen;
    712 	m->m_pkthdr.rcvif = ifp;
    713 
    714 	s = splnet();
    715 
    716 	/*
    717 	 * Handle BPF listeners. Let the BPF user see the packet, but
    718 	 * don't pass it up to the ether_input() layer unless it's
    719 	 * a broadcast packet, multicast packet, matches our ethernet
    720 	 * address or the interface is in promiscuous mode.
    721 	 */
    722 	bpf_mtap(ifp, m);
    723 
    724 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->kue_dev),
    725 		    __func__, m->m_len));
    726 	(*ifp->if_input)(ifp, m);
    727 
    728 	splx(s);
    729 
    730  done:
    731 
    732 	/* Setup new transfer. */
    733 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
    734 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    735 	    USBD_NO_TIMEOUT, kue_rxeof);
    736 	usbd_transfer(c->kue_xfer);
    737 
    738 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->kue_dev),
    739 		    __func__));
    740 }
    741 
    742 /*
    743  * A frame was downloaded to the chip. It's safe for us to clean up
    744  * the list buffers.
    745  */
    746 
    747 static void
    748 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    749     usbd_status status)
    750 {
    751 	struct kue_chain	*c = priv;
    752 	struct kue_softc	*sc = c->kue_sc;
    753 	struct ifnet		*ifp = GET_IFP(sc);
    754 	int			s;
    755 
    756 	if (sc->kue_dying)
    757 		return;
    758 
    759 	s = splnet();
    760 
    761 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev),
    762 		    __func__, status));
    763 
    764 	ifp->if_timer = 0;
    765 	ifp->if_flags &= ~IFF_OACTIVE;
    766 
    767 	if (status != USBD_NORMAL_COMPLETION) {
    768 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    769 			splx(s);
    770 			return;
    771 		}
    772 		ifp->if_oerrors++;
    773 		printf("%s: usb error on tx: %s\n", device_xname(sc->kue_dev),
    774 		    usbd_errstr(status));
    775 		if (status == USBD_STALLED)
    776 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
    777 		splx(s);
    778 		return;
    779 	}
    780 
    781 	ifp->if_opackets++;
    782 
    783 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    784 		kue_start(ifp);
    785 
    786 	splx(s);
    787 }
    788 
    789 static int
    790 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
    791 {
    792 	int			total_len;
    793 	struct kue_chain	*c;
    794 	usbd_status		err;
    795 
    796 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
    797 
    798 	c = &sc->kue_cdata.kue_tx_chain[idx];
    799 
    800 	/* Frame length is specified in the first 2 bytes of the buffer. */
    801 	le16enc(c->kue_buf, (uint16_t)m->m_pkthdr.len);
    802 
    803 	/*
    804 	 * Copy the mbuf data into a contiguous buffer, leaving two
    805 	 * bytes at the beginning to hold the frame length.
    806 	 */
    807 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
    808 
    809 	total_len = 2 + m->m_pkthdr.len;
    810 	total_len = roundup2(total_len, 64);
    811 
    812 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
    813 	    c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
    814 	    kue_txeof);
    815 
    816 	/* Transmit */
    817 	err = usbd_transfer(c->kue_xfer);
    818 	if (err != USBD_IN_PROGRESS) {
    819 		printf("%s: kue_send error=%s\n", device_xname(sc->kue_dev),
    820 		       usbd_errstr(err));
    821 		kue_stop(sc);
    822 		return (EIO);
    823 	}
    824 
    825 	sc->kue_cdata.kue_tx_cnt++;
    826 
    827 	return (0);
    828 }
    829 
    830 static void
    831 kue_start(struct ifnet *ifp)
    832 {
    833 	struct kue_softc	*sc = ifp->if_softc;
    834 	struct mbuf		*m;
    835 
    836 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
    837 
    838 	if (sc->kue_dying)
    839 		return;
    840 
    841 	if (ifp->if_flags & IFF_OACTIVE)
    842 		return;
    843 
    844 	IFQ_POLL(&ifp->if_snd, m);
    845 	if (m == NULL)
    846 		return;
    847 
    848 	if (kue_send(sc, m, 0)) {
    849 		ifp->if_flags |= IFF_OACTIVE;
    850 		return;
    851 	}
    852 
    853 	IFQ_DEQUEUE(&ifp->if_snd, m);
    854 
    855 	/*
    856 	 * If there's a BPF listener, bounce a copy of this frame
    857 	 * to him.
    858 	 */
    859 	bpf_mtap(ifp, m);
    860 	m_freem(m);
    861 
    862 	ifp->if_flags |= IFF_OACTIVE;
    863 
    864 	/*
    865 	 * Set a timeout in case the chip goes out to lunch.
    866 	 */
    867 	ifp->if_timer = 6;
    868 }
    869 
    870 static void
    871 kue_init(void *xsc)
    872 {
    873 	struct kue_softc	*sc = xsc;
    874 	struct ifnet		*ifp = GET_IFP(sc);
    875 	int			s;
    876 	uint8_t			eaddr[ETHER_ADDR_LEN];
    877 
    878 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
    879 
    880 	if (ifp->if_flags & IFF_RUNNING)
    881 		return;
    882 
    883 	s = splnet();
    884 
    885 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
    886 	/* Set MAC address */
    887 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
    888 
    889 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
    890 
    891 	 /* If we want promiscuous mode, set the allframes bit. */
    892 	if (ifp->if_flags & IFF_PROMISC)
    893 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
    894 
    895 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
    896 
    897 	/* I'm not sure how to tune these. */
    898 #if 0
    899 	/*
    900 	 * Leave this one alone for now; setting it
    901 	 * wrong causes lockups on some machines/controllers.
    902 	 */
    903 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
    904 #endif
    905 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
    906 
    907 	/* Init TX ring. */
    908 	if (kue_tx_list_init(sc) == ENOBUFS) {
    909 		printf("%s: tx list init failed\n", device_xname(sc->kue_dev));
    910 		splx(s);
    911 		return;
    912 	}
    913 
    914 	/* Init RX ring. */
    915 	if (kue_rx_list_init(sc) == ENOBUFS) {
    916 		printf("%s: rx list init failed\n", device_xname(sc->kue_dev));
    917 		splx(s);
    918 		return;
    919 	}
    920 
    921 	/* Load the multicast filter. */
    922 	kue_setmulti(sc);
    923 
    924 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
    925 		if (kue_open_pipes(sc)) {
    926 			splx(s);
    927 			return;
    928 		}
    929 	}
    930 
    931 	ifp->if_flags |= IFF_RUNNING;
    932 	ifp->if_flags &= ~IFF_OACTIVE;
    933 
    934 	splx(s);
    935 }
    936 
    937 static int
    938 kue_open_pipes(struct kue_softc *sc)
    939 {
    940 	usbd_status		err;
    941 	struct kue_chain	*c;
    942 	int			i;
    943 
    944 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
    945 
    946 	/* Open RX and TX pipes. */
    947 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
    948 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
    949 	if (err) {
    950 		printf("%s: open rx pipe failed: %s\n",
    951 		    device_xname(sc->kue_dev), usbd_errstr(err));
    952 		return (EIO);
    953 	}
    954 
    955 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
    956 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
    957 	if (err) {
    958 		printf("%s: open tx pipe failed: %s\n",
    959 		    device_xname(sc->kue_dev), usbd_errstr(err));
    960 		return (EIO);
    961 	}
    962 
    963 	/* Start up the receive pipe. */
    964 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
    965 		c = &sc->kue_cdata.kue_rx_chain[i];
    966 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
    967 		    c, c->kue_buf, KUE_BUFSZ,
    968 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
    969 		    kue_rxeof);
    970 		DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->kue_dev),
    971 			    __func__));
    972 		usbd_transfer(c->kue_xfer);
    973 	}
    974 
    975 	return (0);
    976 }
    977 
    978 static int
    979 kue_ioctl(struct ifnet *ifp, u_long command, void *data)
    980 {
    981 	struct kue_softc	*sc = ifp->if_softc;
    982 	struct ifaddr 		*ifa = (struct ifaddr *)data;
    983 	struct ifreq		*ifr = (struct ifreq *)data;
    984 	int			s, error = 0;
    985 
    986 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
    987 
    988 	if (sc->kue_dying)
    989 		return (EIO);
    990 
    991 	s = splnet();
    992 
    993 	switch(command) {
    994 	case SIOCINITIFADDR:
    995 		ifp->if_flags |= IFF_UP;
    996 		kue_init(sc);
    997 
    998 		switch (ifa->ifa_addr->sa_family) {
    999 #ifdef INET
   1000 		case AF_INET:
   1001 			arp_ifinit(ifp, ifa);
   1002 			break;
   1003 #endif /* INET */
   1004 		}
   1005 		break;
   1006 
   1007 	case SIOCSIFMTU:
   1008 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1009 			error = EINVAL;
   1010 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
   1011 			error = 0;
   1012 		break;
   1013 
   1014 	case SIOCSIFFLAGS:
   1015 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   1016 			break;
   1017 		if (ifp->if_flags & IFF_UP) {
   1018 			if (ifp->if_flags & IFF_RUNNING &&
   1019 			    ifp->if_flags & IFF_PROMISC &&
   1020 			    !(sc->kue_if_flags & IFF_PROMISC)) {
   1021 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
   1022 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1023 				    sc->kue_rxfilt);
   1024 			} else if (ifp->if_flags & IFF_RUNNING &&
   1025 			    !(ifp->if_flags & IFF_PROMISC) &&
   1026 			    sc->kue_if_flags & IFF_PROMISC) {
   1027 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
   1028 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1029 				    sc->kue_rxfilt);
   1030 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1031 				kue_init(sc);
   1032 		} else {
   1033 			if (ifp->if_flags & IFF_RUNNING)
   1034 				kue_stop(sc);
   1035 		}
   1036 		sc->kue_if_flags = ifp->if_flags;
   1037 		error = 0;
   1038 		break;
   1039 	case SIOCADDMULTI:
   1040 	case SIOCDELMULTI:
   1041 		error = ether_ioctl(ifp, command, data);
   1042 		if (error == ENETRESET) {
   1043 			if (ifp->if_flags & IFF_RUNNING)
   1044 				kue_setmulti(sc);
   1045 			error = 0;
   1046 		}
   1047 		break;
   1048 	default:
   1049 		error = ether_ioctl(ifp, command, data);
   1050 		break;
   1051 	}
   1052 
   1053 	splx(s);
   1054 
   1055 	return (error);
   1056 }
   1057 
   1058 static void
   1059 kue_watchdog(struct ifnet *ifp)
   1060 {
   1061 	struct kue_softc	*sc = ifp->if_softc;
   1062 	struct kue_chain	*c;
   1063 	usbd_status		stat;
   1064 	int			s;
   1065 
   1066 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
   1067 
   1068 	if (sc->kue_dying)
   1069 		return;
   1070 
   1071 	ifp->if_oerrors++;
   1072 	printf("%s: watchdog timeout\n", device_xname(sc->kue_dev));
   1073 
   1074 	s = splusb();
   1075 	c = &sc->kue_cdata.kue_tx_chain[0];
   1076 	usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
   1077 	kue_txeof(c->kue_xfer, c, stat);
   1078 
   1079 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1080 		kue_start(ifp);
   1081 	splx(s);
   1082 }
   1083 
   1084 /*
   1085  * Stop the adapter and free any mbufs allocated to the
   1086  * RX and TX lists.
   1087  */
   1088 static void
   1089 kue_stop(struct kue_softc *sc)
   1090 {
   1091 	usbd_status		err;
   1092 	struct ifnet		*ifp;
   1093 	int			i;
   1094 
   1095 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
   1096 
   1097 	ifp = GET_IFP(sc);
   1098 	ifp->if_timer = 0;
   1099 
   1100 	/* Stop transfers. */
   1101 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
   1102 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1103 		if (err) {
   1104 			printf("%s: abort rx pipe failed: %s\n",
   1105 			    device_xname(sc->kue_dev), usbd_errstr(err));
   1106 		}
   1107 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1108 		if (err) {
   1109 			printf("%s: close rx pipe failed: %s\n",
   1110 			    device_xname(sc->kue_dev), usbd_errstr(err));
   1111 		}
   1112 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
   1113 	}
   1114 
   1115 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
   1116 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1117 		if (err) {
   1118 			printf("%s: abort tx pipe failed: %s\n",
   1119 			    device_xname(sc->kue_dev), usbd_errstr(err));
   1120 		}
   1121 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1122 		if (err) {
   1123 			printf("%s: close tx pipe failed: %s\n",
   1124 			    device_xname(sc->kue_dev), usbd_errstr(err));
   1125 		}
   1126 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
   1127 	}
   1128 
   1129 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
   1130 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1131 		if (err) {
   1132 			printf("%s: abort intr pipe failed: %s\n",
   1133 			    device_xname(sc->kue_dev), usbd_errstr(err));
   1134 		}
   1135 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1136 		if (err) {
   1137 			printf("%s: close intr pipe failed: %s\n",
   1138 			    device_xname(sc->kue_dev), usbd_errstr(err));
   1139 		}
   1140 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
   1141 	}
   1142 
   1143 	/* Free RX resources. */
   1144 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1145 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
   1146 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
   1147 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
   1148 		}
   1149 	}
   1150 
   1151 	/* Free TX resources. */
   1152 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
   1153 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
   1154 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
   1155 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
   1156 		}
   1157 	}
   1158 
   1159 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1160 }
   1161