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