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