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