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