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