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