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