Home | History | Annotate | Line # | Download | only in usb
if_kue.c revision 1.42
      1 /*	$NetBSD: if_kue.c,v 1.42 2001/07/16 13:54:23 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_3COM, USB_PRODUCT_3COM_3C19250 },
    150 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
    151 	{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
    152 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
    153 	{ USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
    154 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
    155 	{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
    156 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
    157 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
    158 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
    159 	{ USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
    160 	{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
    161 	{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
    162 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
    163 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
    164 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
    165 	{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
    166 	{ 0, 0 }
    167 };
    168 
    169 USB_DECLARE_DRIVER(kue);
    170 
    171 Static int kue_tx_list_init(struct kue_softc *);
    172 Static int kue_rx_list_init(struct kue_softc *);
    173 Static int kue_newbuf(struct kue_softc *, struct kue_chain *,struct mbuf *);
    174 Static int kue_send(struct kue_softc *, struct mbuf *, int);
    175 Static int kue_open_pipes(struct kue_softc *);
    176 Static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    177 Static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    178 Static void kue_start(struct ifnet *);
    179 Static int kue_ioctl(struct ifnet *, u_long, caddr_t);
    180 Static void kue_init(void *);
    181 Static void kue_stop(struct kue_softc *);
    182 Static void kue_watchdog(struct ifnet *);
    183 
    184 Static void kue_setmulti(struct kue_softc *);
    185 Static void kue_reset(struct kue_softc *);
    186 
    187 Static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
    188 			   u_int16_t, void *, u_int32_t);
    189 Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
    190 Static int kue_load_fw(struct kue_softc *);
    191 
    192 Static usbd_status
    193 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
    194 {
    195 	usb_device_request_t	req;
    196 
    197 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    198 
    199 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    200 	req.bRequest = breq;
    201 	USETW(req.wValue, word);
    202 	USETW(req.wIndex, 0);
    203 	USETW(req.wLength, 0);
    204 
    205 	return (usbd_do_request(sc->kue_udev, &req, NULL));
    206 }
    207 
    208 Static usbd_status
    209 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
    210 	void *data, u_int32_t len)
    211 {
    212 	usb_device_request_t	req;
    213 
    214 	DPRINTFN(10,("%s: %s: enter, len=%d\n", USBDEVNAME(sc->kue_dev),
    215 		     __FUNCTION__, len));
    216 
    217 	if (rw == KUE_CTL_WRITE)
    218 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    219 	else
    220 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    221 
    222 	req.bRequest = breq;
    223 	USETW(req.wValue, val);
    224 	USETW(req.wIndex, 0);
    225 	USETW(req.wLength, len);
    226 
    227 	return (usbd_do_request(sc->kue_udev, &req, data));
    228 }
    229 
    230 Static int
    231 kue_load_fw(struct kue_softc *sc)
    232 {
    233 	usb_device_descriptor_t dd;
    234 	usbd_status		err;
    235 
    236 	DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    237 
    238 	/*
    239 	 * First, check if we even need to load the firmware.
    240 	 * If the device was still attached when the system was
    241 	 * rebooted, it may already have firmware loaded in it.
    242 	 * If this is the case, we don't need to do it again.
    243 	 * And in fact, if we try to load it again, we'll hang,
    244 	 * so we have to avoid this condition if we don't want
    245 	 * to look stupid.
    246 	 *
    247          * We can test this quickly by checking the bcdRevision
    248          * code. The NIC will return a different revision code if
    249          * it's probed while the firmware is still loaded and
    250          * running.
    251          */
    252 	if (usbd_get_device_desc(sc->kue_udev, &dd))
    253 		return (EIO);
    254         if (UGETW(dd.bcdDevice) == KUE_WARM_REV) {
    255 		printf("%s: warm boot, no firmware download\n",
    256 		       USBDEVNAME(sc->kue_dev));
    257 		return (0);
    258 	}
    259 
    260 	printf("%s: cold boot, downloading firmware\n",
    261 	       USBDEVNAME(sc->kue_dev));
    262 
    263 	/* Load code segment */
    264 	DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
    265 		    USBDEVNAME(sc->kue_dev)));
    266 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    267 	    0, (void *)kue_code_seg, sizeof(kue_code_seg));
    268 	if (err) {
    269 		printf("%s: failed to load code segment: %s\n",
    270 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    271 			return (EIO);
    272 	}
    273 
    274 	/* Load fixup segment */
    275 	DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
    276 		    USBDEVNAME(sc->kue_dev)));
    277 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    278 	    0, (void *)kue_fix_seg, sizeof(kue_fix_seg));
    279 	if (err) {
    280 		printf("%s: failed to load fixup segment: %s\n",
    281 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    282 			return (EIO);
    283 	}
    284 
    285 	/* Send trigger command. */
    286 	DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
    287 		    USBDEVNAME(sc->kue_dev)));
    288 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    289 	    0, (void *)kue_trig_seg, sizeof(kue_trig_seg));
    290 	if (err) {
    291 		printf("%s: failed to load trigger segment: %s\n",
    292 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    293 			return (EIO);
    294 	}
    295 
    296 	usbd_delay_ms(sc->kue_udev, 10);
    297 
    298 	/*
    299 	 * Reload device descriptor.
    300 	 * Why? The chip without the firmware loaded returns
    301 	 * one revision code. The chip with the firmware
    302 	 * loaded and running returns a *different* revision
    303 	 * code. This confuses the quirk mechanism, which is
    304 	 * dependent on the revision data.
    305 	 */
    306 	(void)usbd_reload_device_desc(sc->kue_udev);
    307 
    308 	DPRINTFN(1,("%s: %s: done\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    309 
    310 	/* Reset the adapter. */
    311 	kue_reset(sc);
    312 
    313 	return (0);
    314 }
    315 
    316 Static void
    317 kue_setmulti(struct kue_softc *sc)
    318 {
    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", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    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 #if defined (__NetBSD__)
    339 	ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
    340 #else
    341 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    342 #endif
    343 	while (enm != NULL) {
    344 		if (i == KUE_MCFILTCNT(sc) ||
    345 		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
    346 			ETHER_ADDR_LEN) != 0)
    347 			goto allmulti;
    348 
    349 		memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
    350 		ETHER_NEXT_MULTI(step, enm);
    351 		i++;
    352 	}
    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 	usbd_status		err;
    372 
    373 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    374 
    375 	err = usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1);
    376 	if (err)
    377 		printf("%s: reset failed\n", USBDEVNAME(sc->kue_dev));
    378 
    379 	/* Wait a little while for the chip to get its brains in order. */
    380 	usbd_delay_ms(sc->kue_udev, 10);
    381 }
    382 
    383 /*
    384  * Probe for a KLSI chip.
    385  */
    386 USB_MATCH(kue)
    387 {
    388 	USB_MATCH_START(kue, uaa);
    389 	const struct kue_type			*t;
    390 
    391 	DPRINTFN(25,("kue_match: enter\n"));
    392 
    393 	if (uaa->iface != NULL)
    394 		return (UMATCH_NONE);
    395 
    396 	for (t = kue_devs; t->kue_vid != 0; t++)
    397 		if (uaa->vendor == t->kue_vid && uaa->product == t->kue_did)
    398 			return (UMATCH_VENDOR_PRODUCT);
    399 
    400 	return (UMATCH_NONE);
    401 }
    402 
    403 /*
    404  * Attach the interface. Allocate softc structures, do
    405  * setup and ethernet/BPF attach.
    406  */
    407 USB_ATTACH(kue)
    408 {
    409 	USB_ATTACH_START(kue, sc, uaa);
    410 	char			devinfo[1024];
    411 	int			s;
    412 	struct ifnet		*ifp;
    413 	usbd_device_handle	dev = uaa->device;
    414 	usbd_interface_handle	iface;
    415 	usbd_status		err;
    416 	usb_interface_descriptor_t	*id;
    417 	usb_endpoint_descriptor_t	*ed;
    418 	int			i;
    419 
    420 	DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
    421 
    422 	usbd_devinfo(dev, 0, devinfo);
    423 	USB_ATTACH_SETUP;
    424 	printf("%s: %s\n", USBDEVNAME(sc->kue_dev), devinfo);
    425 
    426 	err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
    427 	if (err) {
    428 		printf("%s: setting config no failed\n",
    429 		    USBDEVNAME(sc->kue_dev));
    430 		USB_ATTACH_ERROR_RETURN;
    431 	}
    432 
    433 	sc->kue_udev = dev;
    434 	sc->kue_product = uaa->product;
    435 	sc->kue_vendor = uaa->vendor;
    436 
    437 	/* Load the firmware into the NIC. */
    438 	if (kue_load_fw(sc)) {
    439 		printf("%s: loading firmware failed\n",
    440 		    USBDEVNAME(sc->kue_dev));
    441 		USB_ATTACH_ERROR_RETURN;
    442 	}
    443 
    444 	err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
    445 	if (err) {
    446 		printf("%s: getting interface handle failed\n",
    447 		    USBDEVNAME(sc->kue_dev));
    448 		USB_ATTACH_ERROR_RETURN;
    449 	}
    450 
    451 	sc->kue_iface = iface;
    452 	id = usbd_get_interface_descriptor(iface);
    453 
    454 	/* Find endpoints. */
    455 	for (i = 0; i < id->bNumEndpoints; i++) {
    456 		ed = usbd_interface2endpoint_descriptor(iface, i);
    457 		if (ed == NULL) {
    458 			printf("%s: couldn't get ep %d\n",
    459 			    USBDEVNAME(sc->kue_dev), i);
    460 			USB_ATTACH_ERROR_RETURN;
    461 		}
    462 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    463 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    464 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
    465 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    466 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    467 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
    468 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    469 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    470 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
    471 		}
    472 	}
    473 
    474 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
    475 		printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev));
    476 		USB_ATTACH_ERROR_RETURN;
    477 	}
    478 
    479 	/* Read ethernet descriptor */
    480 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
    481 	    0, &sc->kue_desc, sizeof(sc->kue_desc));
    482 	if (err) {
    483 		printf("%s: could not read Ethernet descriptor\n",
    484 		    USBDEVNAME(sc->kue_dev));
    485 		USB_ATTACH_ERROR_RETURN;
    486 	}
    487 
    488 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
    489 	    M_USBDEV, M_NOWAIT);
    490 	if (sc->kue_mcfilters == NULL) {
    491 		printf("%s: no memory for multicast filter buffer\n",
    492 		    USBDEVNAME(sc->kue_dev));
    493 		USB_ATTACH_ERROR_RETURN;
    494 	}
    495 
    496 	s = splnet();
    497 
    498 	/*
    499 	 * A KLSI chip was detected. Inform the world.
    500 	 */
    501 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->kue_dev),
    502 	    ether_sprintf(sc->kue_desc.kue_macaddr));
    503 
    504 	/* Initialize interface info.*/
    505 	ifp = GET_IFP(sc);
    506 	ifp->if_softc = sc;
    507 	ifp->if_mtu = ETHERMTU;
    508 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    509 	ifp->if_ioctl = kue_ioctl;
    510 	ifp->if_start = kue_start;
    511 	ifp->if_watchdog = kue_watchdog;
    512 #if defined(__OpenBSD__)
    513 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    514 #endif
    515 	strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
    516 
    517 	IFQ_SET_READY(&ifp->if_snd);
    518 
    519 	/* Attach the interface. */
    520 	if_attach(ifp);
    521 	Ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
    522 #if NRND > 0
    523 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
    524 	    RND_TYPE_NET, 0);
    525 #endif
    526 
    527 	sc->kue_attached = 1;
    528 	splx(s);
    529 
    530 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
    531 			   USBDEV(sc->kue_dev));
    532 
    533 	USB_ATTACH_SUCCESS_RETURN;
    534 }
    535 
    536 USB_DETACH(kue)
    537 {
    538 	USB_DETACH_START(kue, sc);
    539 	struct ifnet		*ifp = GET_IFP(sc);
    540 	int			s;
    541 
    542 	s = splusb();		/* XXX why? */
    543 
    544 	if (sc->kue_mcfilters != NULL) {
    545 		free(sc->kue_mcfilters, M_USBDEV);
    546 		sc->kue_mcfilters = NULL;
    547 	}
    548 
    549 	if (!sc->kue_attached) {
    550 		/* Detached before attached finished, so just bail out. */
    551 		splx(s);
    552 		return (0);
    553 	}
    554 
    555 	if (ifp->if_flags & IFF_RUNNING)
    556 		kue_stop(sc);
    557 
    558 #if defined(__NetBSD__)
    559 #if NRND > 0
    560 	rnd_detach_source(&sc->rnd_source);
    561 #endif
    562 	ether_ifdetach(ifp);
    563 #endif /* __NetBSD__ */
    564 
    565 	if_detach(ifp);
    566 
    567 #ifdef DIAGNOSTIC
    568 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
    569 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
    570 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
    571 		printf("%s: detach has active endpoints\n",
    572 		       USBDEVNAME(sc->kue_dev));
    573 #endif
    574 
    575 	sc->kue_attached = 0;
    576 	splx(s);
    577 
    578 	return (0);
    579 }
    580 
    581 int
    582 kue_activate(device_ptr_t self, enum devact act)
    583 {
    584 	struct kue_softc *sc = (struct kue_softc *)self;
    585 
    586 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    587 
    588 	switch (act) {
    589 	case DVACT_ACTIVATE:
    590 		return (EOPNOTSUPP);
    591 		break;
    592 
    593 	case DVACT_DEACTIVATE:
    594 #if defined(__NetBSD__)
    595 		/* Deactivate the interface. */
    596 		if_deactivate(&sc->kue_ec.ec_if);
    597 #endif
    598 		sc->kue_dying = 1;
    599 		break;
    600 	}
    601 	return (0);
    602 }
    603 
    604 /*
    605  * Initialize an RX descriptor and attach an MBUF cluster.
    606  */
    607 Static int
    608 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
    609 {
    610 	struct mbuf		*m_new = NULL;
    611 
    612 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    613 
    614 	if (m == NULL) {
    615 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    616 		if (m_new == NULL) {
    617 			printf("%s: no memory for rx list "
    618 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
    619 			return (ENOBUFS);
    620 		}
    621 
    622 		MCLGET(m_new, M_DONTWAIT);
    623 		if (!(m_new->m_flags & M_EXT)) {
    624 			printf("%s: no memory for rx list "
    625 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
    626 			m_freem(m_new);
    627 			return (ENOBUFS);
    628 		}
    629 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    630 	} else {
    631 		m_new = m;
    632 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    633 		m_new->m_data = m_new->m_ext.ext_buf;
    634 	}
    635 
    636 	c->kue_mbuf = m_new;
    637 
    638 	return (0);
    639 }
    640 
    641 Static int
    642 kue_rx_list_init(struct kue_softc *sc)
    643 {
    644 	struct kue_cdata	*cd;
    645 	struct kue_chain	*c;
    646 	int			i;
    647 
    648 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    649 
    650 	cd = &sc->kue_cdata;
    651 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
    652 		c = &cd->kue_rx_chain[i];
    653 		c->kue_sc = sc;
    654 		c->kue_idx = i;
    655 		if (kue_newbuf(sc, c, NULL) == ENOBUFS)
    656 			return (ENOBUFS);
    657 		if (c->kue_xfer == NULL) {
    658 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    659 			if (c->kue_xfer == NULL)
    660 				return (ENOBUFS);
    661 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    662 			if (c->kue_buf == NULL)
    663 				return (ENOBUFS); /* XXX free xfer */
    664 		}
    665 	}
    666 
    667 	return (0);
    668 }
    669 
    670 Static int
    671 kue_tx_list_init(struct kue_softc *sc)
    672 {
    673 	struct kue_cdata	*cd;
    674 	struct kue_chain	*c;
    675 	int			i;
    676 
    677 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    678 
    679 	cd = &sc->kue_cdata;
    680 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
    681 		c = &cd->kue_tx_chain[i];
    682 		c->kue_sc = sc;
    683 		c->kue_idx = i;
    684 		c->kue_mbuf = NULL;
    685 		if (c->kue_xfer == NULL) {
    686 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    687 			if (c->kue_xfer == NULL)
    688 				return (ENOBUFS);
    689 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    690 			if (c->kue_buf == NULL)
    691 				return (ENOBUFS);
    692 		}
    693 	}
    694 
    695 	return (0);
    696 }
    697 
    698 /*
    699  * A frame has been uploaded: pass the resulting mbuf chain up to
    700  * the higher level protocols.
    701  */
    702 Static void
    703 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    704 {
    705 	struct kue_chain	*c = priv;
    706 	struct kue_softc	*sc = c->kue_sc;
    707 	struct ifnet		*ifp = GET_IFP(sc);
    708 	struct mbuf		*m;
    709 	int			total_len = 0;
    710 	int			s;
    711 
    712 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
    713 		     __FUNCTION__, status));
    714 
    715 	if (sc->kue_dying)
    716 		return;
    717 
    718 	if (!(ifp->if_flags & IFF_RUNNING))
    719 		return;
    720 
    721 	if (status != USBD_NORMAL_COMPLETION) {
    722 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    723 			return;
    724 		sc->kue_rx_errs++;
    725 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
    726 			printf("%s: %u usb errors on rx: %s\n",
    727 			    USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
    728 			    usbd_errstr(status));
    729 			sc->kue_rx_errs = 0;
    730 		}
    731 		if (status == USBD_STALLED)
    732 			usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
    733 		goto done;
    734 	}
    735 
    736 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    737 
    738 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
    739 		     __FUNCTION__, total_len,
    740 		     UGETW(mtod(c->kue_mbuf, u_int8_t *))));
    741 
    742 	if (total_len <= 1)
    743 		goto done;
    744 
    745 	m = c->kue_mbuf;
    746 	/* copy data to mbuf */
    747 	memcpy(mtod(m, char*), c->kue_buf, total_len);
    748 
    749 	/* No errors; receive the packet. */
    750 	total_len = UGETW(mtod(m, u_int8_t *));
    751 	m_adj(m, sizeof(u_int16_t));
    752 
    753 	if (total_len < sizeof(struct ether_header)) {
    754 		ifp->if_ierrors++;
    755 		goto done;
    756 	}
    757 
    758 	ifp->if_ipackets++;
    759 	m->m_pkthdr.len = m->m_len = total_len;
    760 
    761 	m->m_pkthdr.rcvif = ifp;
    762 
    763 	s = splnet();
    764 
    765 	/* XXX ugly */
    766 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
    767 		ifp->if_ierrors++;
    768 		goto done1;
    769 	}
    770 
    771 #if NBPFILTER > 0
    772 	/*
    773 	 * Handle BPF listeners. Let the BPF user see the packet, but
    774 	 * don't pass it up to the ether_input() layer unless it's
    775 	 * a broadcast packet, multicast packet, matches our ethernet
    776 	 * address or the interface is in promiscuous mode.
    777 	 */
    778 	if (ifp->if_bpf)
    779 		bpf_mtap(ifp->if_bpf, m);
    780 #endif
    781 
    782 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
    783 		    __FUNCTION__, m->m_len));
    784 	IF_INPUT(ifp, m);
    785  done1:
    786 	splx(s);
    787 
    788  done:
    789 
    790 	/* Setup new transfer. */
    791 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
    792 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    793 	    USBD_NO_TIMEOUT, kue_rxeof);
    794 	usbd_transfer(c->kue_xfer);
    795 
    796 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
    797 		    __FUNCTION__));
    798 }
    799 
    800 /*
    801  * A frame was downloaded to the chip. It's safe for us to clean up
    802  * the list buffers.
    803  */
    804 
    805 Static void
    806 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    807 {
    808 	struct kue_chain	*c = priv;
    809 	struct kue_softc	*sc = c->kue_sc;
    810 	struct ifnet		*ifp = GET_IFP(sc);
    811 	int			s;
    812 
    813 	if (sc->kue_dying)
    814 		return;
    815 
    816 	s = splnet();
    817 
    818 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
    819 		    __FUNCTION__, status));
    820 
    821 	ifp->if_timer = 0;
    822 	ifp->if_flags &= ~IFF_OACTIVE;
    823 
    824 	if (status != USBD_NORMAL_COMPLETION) {
    825 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    826 			splx(s);
    827 			return;
    828 		}
    829 		ifp->if_oerrors++;
    830 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
    831 		    usbd_errstr(status));
    832 		if (status == USBD_STALLED)
    833 			usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
    834 		splx(s);
    835 		return;
    836 	}
    837 
    838 	ifp->if_opackets++;
    839 
    840 	m_freem(c->kue_mbuf);
    841 	c->kue_mbuf = NULL;
    842 
    843 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    844 		kue_start(ifp);
    845 
    846 	splx(s);
    847 }
    848 
    849 Static int
    850 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
    851 {
    852 	int			total_len;
    853 	struct kue_chain	*c;
    854 	usbd_status		err;
    855 
    856 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    857 
    858 	c = &sc->kue_cdata.kue_tx_chain[idx];
    859 
    860 	/*
    861 	 * Copy the mbuf data into a contiguous buffer, leaving two
    862 	 * bytes at the beginning to hold the frame length.
    863 	 */
    864 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
    865 	c->kue_mbuf = m;
    866 
    867 	total_len = m->m_pkthdr.len + 2;
    868 	/* XXX what's this? */
    869 	total_len += 64 - (total_len % 64);
    870 
    871 	/* Frame length is specified in the first 2 bytes of the buffer. */
    872 	c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
    873 	c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
    874 
    875 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
    876 	    c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
    877 	    kue_txeof);
    878 
    879 	/* Transmit */
    880 	err = usbd_transfer(c->kue_xfer);
    881 	if (err != USBD_IN_PROGRESS) {
    882 		printf("%s: kue_send error=%s\n", USBDEVNAME(sc->kue_dev),
    883 		       usbd_errstr(err));
    884 		kue_stop(sc);
    885 		return (EIO);
    886 	}
    887 
    888 	sc->kue_cdata.kue_tx_cnt++;
    889 
    890 	return (0);
    891 }
    892 
    893 Static void
    894 kue_start(struct ifnet *ifp)
    895 {
    896 	struct kue_softc	*sc = ifp->if_softc;
    897 	struct mbuf		*m_head = NULL;
    898 
    899 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    900 
    901 	if (sc->kue_dying)
    902 		return;
    903 
    904 	if (ifp->if_flags & IFF_OACTIVE)
    905 		return;
    906 
    907 	IFQ_POLL(&ifp->if_snd, m_head);
    908 	if (m_head == NULL)
    909 		return;
    910 
    911 	if (kue_send(sc, m_head, 0)) {
    912 		ifp->if_flags |= IFF_OACTIVE;
    913 		return;
    914 	}
    915 
    916 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    917 
    918 #if NBPFILTER > 0
    919 	/*
    920 	 * If there's a BPF listener, bounce a copy of this frame
    921 	 * to him.
    922 	 */
    923 	if (ifp->if_bpf)
    924 		bpf_mtap(ifp->if_bpf, m_head);
    925 #endif
    926 
    927 	ifp->if_flags |= IFF_OACTIVE;
    928 
    929 	/*
    930 	 * Set a timeout in case the chip goes out to lunch.
    931 	 */
    932 	ifp->if_timer = 6;
    933 }
    934 
    935 Static void
    936 kue_init(void *xsc)
    937 {
    938 	struct kue_softc	*sc = xsc;
    939 	struct ifnet		*ifp = GET_IFP(sc);
    940 	int			s;
    941 	u_char			*eaddr;
    942 
    943 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    944 
    945 	if (ifp->if_flags & IFF_RUNNING)
    946 		return;
    947 
    948 	s = splnet();
    949 
    950 #if defined(__NetBSD__)
    951 	eaddr = LLADDR(ifp->if_sadl);
    952 #else
    953 	eaddr = sc->arpcom.ac_enaddr;
    954 #endif /* defined(__NetBSD__) */
    955 	/* Set MAC address */
    956 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
    957 
    958 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
    959 
    960 	 /* If we want promiscuous mode, set the allframes bit. */
    961 	if (ifp->if_flags & IFF_PROMISC)
    962 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
    963 
    964 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
    965 
    966 	/* I'm not sure how to tune these. */
    967 #if 0
    968 	/*
    969 	 * Leave this one alone for now; setting it
    970 	 * wrong causes lockups on some machines/controllers.
    971 	 */
    972 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
    973 #endif
    974 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
    975 
    976 	/* Init TX ring. */
    977 	if (kue_tx_list_init(sc) == ENOBUFS) {
    978 		printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
    979 		splx(s);
    980 		return;
    981 	}
    982 
    983 	/* Init RX ring. */
    984 	if (kue_rx_list_init(sc) == ENOBUFS) {
    985 		printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
    986 		splx(s);
    987 		return;
    988 	}
    989 
    990 	/* Load the multicast filter. */
    991 	kue_setmulti(sc);
    992 
    993 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
    994 		if (kue_open_pipes(sc)) {
    995 			splx(s);
    996 			return;
    997 		}
    998 	}
    999 
   1000 	ifp->if_flags |= IFF_RUNNING;
   1001 	ifp->if_flags &= ~IFF_OACTIVE;
   1002 
   1003 	splx(s);
   1004 }
   1005 
   1006 Static int
   1007 kue_open_pipes(struct kue_softc *sc)
   1008 {
   1009 	usbd_status		err;
   1010 	struct kue_chain	*c;
   1011 	int			i;
   1012 
   1013 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1014 
   1015 	/* Open RX and TX pipes. */
   1016 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
   1017 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
   1018 	if (err) {
   1019 		printf("%s: open rx pipe failed: %s\n",
   1020 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1021 		return (EIO);
   1022 	}
   1023 
   1024 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
   1025 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
   1026 	if (err) {
   1027 		printf("%s: open tx pipe failed: %s\n",
   1028 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1029 		return (EIO);
   1030 	}
   1031 
   1032 	/* Start up the receive pipe. */
   1033 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1034 		c = &sc->kue_cdata.kue_rx_chain[i];
   1035 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
   1036 		    c, c->kue_buf, KUE_BUFSZ,
   1037 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1038 		    kue_rxeof);
   1039 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
   1040 			    __FUNCTION__));
   1041 		usbd_transfer(c->kue_xfer);
   1042 	}
   1043 
   1044 	return (0);
   1045 }
   1046 
   1047 Static int
   1048 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   1049 {
   1050 	struct kue_softc	*sc = ifp->if_softc;
   1051 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1052 	struct ifreq		*ifr = (struct ifreq *)data;
   1053 	int			s, error = 0;
   1054 
   1055 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1056 
   1057 	if (sc->kue_dying)
   1058 		return (EIO);
   1059 
   1060 #ifdef DIAGNOSTIC
   1061 	if (!curproc) {
   1062 		printf("%s: no proc!!\n", USBDEVNAME(sc->kue_dev));
   1063 		return EIO;
   1064 	}
   1065 #endif
   1066 
   1067 	s = splnet();
   1068 
   1069 	switch(command) {
   1070 	case SIOCSIFADDR:
   1071 		ifp->if_flags |= IFF_UP;
   1072 		kue_init(sc);
   1073 
   1074 		switch (ifa->ifa_addr->sa_family) {
   1075 #ifdef INET
   1076 		case AF_INET:
   1077 #if defined(__NetBSD__)
   1078 			arp_ifinit(ifp, ifa);
   1079 #else
   1080 			arp_ifinit(&sc->arpcom, ifa);
   1081 #endif
   1082 			break;
   1083 #endif /* INET */
   1084 #ifdef NS
   1085 		case AF_NS:
   1086 		    {
   1087 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1088 
   1089 			if (ns_nullhost(*ina))
   1090 				ina->x_host = *(union ns_host *)
   1091 					LLADDR(ifp->if_sadl);
   1092 			else
   1093 				memcpy(LLADDR(ifp->if_sadl),
   1094 				       ina->x_host.c_host,
   1095 				       ifp->if_addrlen);
   1096 			break;
   1097 		    }
   1098 #endif /* NS */
   1099 		}
   1100 		break;
   1101 
   1102 	case SIOCSIFMTU:
   1103 		if (ifr->ifr_mtu > ETHERMTU)
   1104 			error = EINVAL;
   1105 		else
   1106 			ifp->if_mtu = ifr->ifr_mtu;
   1107 		break;
   1108 
   1109 	case SIOCSIFFLAGS:
   1110 		if (ifp->if_flags & IFF_UP) {
   1111 			if (ifp->if_flags & IFF_RUNNING &&
   1112 			    ifp->if_flags & IFF_PROMISC &&
   1113 			    !(sc->kue_if_flags & IFF_PROMISC)) {
   1114 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
   1115 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1116 				    sc->kue_rxfilt);
   1117 			} else if (ifp->if_flags & IFF_RUNNING &&
   1118 			    !(ifp->if_flags & IFF_PROMISC) &&
   1119 			    sc->kue_if_flags & IFF_PROMISC) {
   1120 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
   1121 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1122 				    sc->kue_rxfilt);
   1123 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1124 				kue_init(sc);
   1125 		} else {
   1126 			if (ifp->if_flags & IFF_RUNNING)
   1127 				kue_stop(sc);
   1128 		}
   1129 		sc->kue_if_flags = ifp->if_flags;
   1130 		error = 0;
   1131 		break;
   1132 	case SIOCADDMULTI:
   1133 	case SIOCDELMULTI:
   1134 		kue_setmulti(sc);
   1135 		error = 0;
   1136 		break;
   1137 	default:
   1138 		error = EINVAL;
   1139 		break;
   1140 	}
   1141 
   1142 	splx(s);
   1143 
   1144 	return (error);
   1145 }
   1146 
   1147 Static void
   1148 kue_watchdog(struct ifnet *ifp)
   1149 {
   1150 	struct kue_softc	*sc = ifp->if_softc;
   1151 	struct kue_chain	*c;
   1152 	usbd_status		stat;
   1153 	int			s;
   1154 
   1155 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1156 
   1157 	if (sc->kue_dying)
   1158 		return;
   1159 
   1160 	ifp->if_oerrors++;
   1161 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
   1162 
   1163 	s = splusb();
   1164 	c = &sc->kue_cdata.kue_tx_chain[0];
   1165 	usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
   1166 	kue_txeof(c->kue_xfer, c, stat);
   1167 
   1168 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1169 		kue_start(ifp);
   1170 	splx(s);
   1171 }
   1172 
   1173 /*
   1174  * Stop the adapter and free any mbufs allocated to the
   1175  * RX and TX lists.
   1176  */
   1177 Static void
   1178 kue_stop(struct kue_softc *sc)
   1179 {
   1180 	usbd_status		err;
   1181 	struct ifnet		*ifp;
   1182 	int			i;
   1183 
   1184 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1185 
   1186 	ifp = GET_IFP(sc);
   1187 	ifp->if_timer = 0;
   1188 
   1189 	/* Stop transfers. */
   1190 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
   1191 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1192 		if (err) {
   1193 			printf("%s: abort rx pipe failed: %s\n",
   1194 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1195 		}
   1196 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1197 		if (err) {
   1198 			printf("%s: close rx pipe failed: %s\n",
   1199 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1200 		}
   1201 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
   1202 	}
   1203 
   1204 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
   1205 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1206 		if (err) {
   1207 			printf("%s: abort tx pipe failed: %s\n",
   1208 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1209 		}
   1210 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1211 		if (err) {
   1212 			printf("%s: close tx pipe failed: %s\n",
   1213 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1214 		}
   1215 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
   1216 	}
   1217 
   1218 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
   1219 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1220 		if (err) {
   1221 			printf("%s: abort intr pipe failed: %s\n",
   1222 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1223 		}
   1224 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1225 		if (err) {
   1226 			printf("%s: close intr pipe failed: %s\n",
   1227 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1228 		}
   1229 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
   1230 	}
   1231 
   1232 	/* Free RX resources. */
   1233 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1234 		if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
   1235 			m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
   1236 			sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
   1237 		}
   1238 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
   1239 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
   1240 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
   1241 		}
   1242 	}
   1243 
   1244 	/* Free TX resources. */
   1245 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
   1246 		if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
   1247 			m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
   1248 			sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
   1249 		}
   1250 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
   1251 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
   1252 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
   1253 		}
   1254 	}
   1255 
   1256 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1257 }
   1258