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