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