Home | History | Annotate | Line # | Download | only in usb
if_kue.c revision 1.16
      1 /*	$NetBSD: if_kue.c,v 1.16 2000/03/15 22:40:30 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 			USB_ATTACH_ERROR_RETURN;
    610 		}
    611 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    612 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    613 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
    614 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    615 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    616 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
    617 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    618 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    619 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
    620 		}
    621 	}
    622 
    623 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
    624 		printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev));
    625 		USB_ATTACH_ERROR_RETURN;
    626 	}
    627 
    628 	/* Read ethernet descriptor */
    629 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
    630 	    0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
    631 	if (err) {
    632 		printf("%s: could not read Ethernet descriptor\n",
    633 		    USBDEVNAME(sc->kue_dev));
    634 		USB_ATTACH_ERROR_RETURN;
    635 	}
    636 
    637 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
    638 	    M_USBDEV, M_NOWAIT);
    639 	if (sc->kue_mcfilters == NULL) {
    640 		printf("%s: no memory for multicast filter buffer\n",
    641 		    USBDEVNAME(sc->kue_dev));
    642 		USB_ATTACH_ERROR_RETURN;
    643 	}
    644 
    645 	sc->kue_xfer_flags = USBD_NO_TSLEEP;
    646 
    647 	s = splimp();
    648 
    649 	/*
    650 	 * A KLSI chip was detected. Inform the world.
    651 	 */
    652 #if defined(__FreeBSD__)
    653 	printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->kue_dev),
    654 	    sc->kue_desc.kue_macaddr, ":");
    655 
    656 	bcopy(sc->kue_desc.kue_macaddr,
    657 	    (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
    658 
    659 	ifp = GET_IFP(sc);
    660 	ifp->if_softc = sc;
    661 	ifp->if_unit = sc->kue_unit;
    662 	ifp->if_name = "kue";
    663 	ifp->if_mtu = ETHERMTU;
    664 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    665 	ifp->if_ioctl = kue_ioctl;
    666 	ifp->if_output = ether_output;
    667 	ifp->if_start = kue_start;
    668 	ifp->if_watchdog = kue_watchdog;
    669 	ifp->if_init = kue_init;
    670 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    671 
    672 	kue_qdat.ifp = ifp;
    673 	kue_qdat.if_rxstart = kue_rxstart;
    674 
    675 	/*
    676 	 * Call MI attach routines.
    677 	 */
    678 	if_attach(ifp);
    679 	ether_ifattach(ifp);
    680 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    681 	usb_register_netisr();
    682 
    683 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    684 
    685 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->kue_dev),
    686 	    ether_sprintf(sc->kue_desc.kue_macaddr));
    687 
    688 	/* Initialize interface info.*/
    689 	ifp = GET_IFP(sc);
    690 	ifp->if_softc = sc;
    691 	ifp->if_mtu = ETHERMTU;
    692 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    693 	ifp->if_ioctl = kue_ioctl;
    694 	ifp->if_start = kue_start;
    695 	ifp->if_watchdog = kue_watchdog;
    696 	strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
    697 
    698 	/* Attach the interface. */
    699 	if_attach(ifp);
    700 	ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
    701 
    702 #if NBPFILTER > 0
    703 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    704 		  sizeof(struct ether_header));
    705 #endif
    706 #if NRND > 0
    707 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
    708 	    RND_TYPE_NET, 0);
    709 #endif
    710 
    711 #endif /* __NetBSD__ */
    712 	sc->kue_attached = 1;
    713 	splx(s);
    714 
    715 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
    716 			   USBDEV(sc->kue_dev));
    717 
    718 	USB_ATTACH_SUCCESS_RETURN;
    719 }
    720 
    721 USB_DETACH(kue)
    722 {
    723 	USB_DETACH_START(kue, sc);
    724 	struct ifnet		*ifp = GET_IFP(sc);
    725 	int			s;
    726 
    727 	s = splusb();		/* XXX why? */
    728 
    729 	if (sc->kue_mcfilters != NULL) {
    730 		free(sc->kue_mcfilters, M_USBDEV);
    731 		sc->kue_mcfilters = NULL;
    732 	}
    733 
    734 	if (!sc->kue_attached) {
    735 		/* Detached before attached finished, so just bail out. */
    736 		splx(s);
    737 		return (0);
    738 	}
    739 
    740 	if (ifp->if_flags & IFF_RUNNING)
    741 		kue_stop(sc);
    742 
    743 #if defined(__NetBSD__)
    744 #if NRND > 0
    745 	rnd_detach_source(&sc->rnd_source);
    746 #endif
    747 #if NBPFILTER > 0
    748 	bpfdetach(ifp);
    749 #endif
    750 	ether_ifdetach(ifp);
    751 #endif /* __NetBSD__ */
    752 
    753 	if_detach(ifp);
    754 
    755 #ifdef DIAGNOSTIC
    756 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
    757 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
    758 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
    759 		printf("%s: detach has active endpoints\n",
    760 		       USBDEVNAME(sc->kue_dev));
    761 #endif
    762 
    763 	sc->kue_attached = 0;
    764 	splx(s);
    765 
    766 	return (0);
    767 }
    768 
    769 #if defined(__NetBSD__) || defined(__OpenBSD__)
    770 int
    771 kue_activate(self, act)
    772 	device_ptr_t self;
    773 	enum devact act;
    774 {
    775 	struct kue_softc *sc = (struct kue_softc *)self;
    776 
    777 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    778 
    779 	switch (act) {
    780 	case DVACT_ACTIVATE:
    781 		return (EOPNOTSUPP);
    782 		break;
    783 
    784 	case DVACT_DEACTIVATE:
    785 		/* Deactivate the interface. */
    786 		if_deactivate(&sc->kue_ec.ec_if);
    787 		sc->kue_dying = 1;
    788 		break;
    789 	}
    790 	return (0);
    791 }
    792 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    793 
    794 /*
    795  * Initialize an RX descriptor and attach an MBUF cluster.
    796  */
    797 static int
    798 kue_newbuf(sc, c, m)
    799 	struct kue_softc	*sc;
    800 	struct kue_chain	*c;
    801 	struct mbuf		*m;
    802 {
    803 	struct mbuf		*m_new = NULL;
    804 
    805 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    806 
    807 	if (m == NULL) {
    808 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    809 		if (m_new == NULL) {
    810 			printf("%s: no memory for rx list "
    811 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
    812 			return (ENOBUFS);
    813 		}
    814 
    815 		MCLGET(m_new, M_DONTWAIT);
    816 		if (!(m_new->m_flags & M_EXT)) {
    817 			printf("%s: no memory for rx list "
    818 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
    819 			m_freem(m_new);
    820 			return (ENOBUFS);
    821 		}
    822 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    823 	} else {
    824 		m_new = m;
    825 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    826 		m_new->m_data = m_new->m_ext.ext_buf;
    827 	}
    828 
    829 	c->kue_mbuf = m_new;
    830 
    831 	return (0);
    832 }
    833 
    834 static int
    835 kue_rx_list_init(sc)
    836 	struct kue_softc	*sc;
    837 {
    838 	struct kue_cdata	*cd;
    839 	struct kue_chain	*c;
    840 	int			i;
    841 
    842 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    843 
    844 	cd = &sc->kue_cdata;
    845 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
    846 		c = &cd->kue_rx_chain[i];
    847 		c->kue_sc = sc;
    848 		c->kue_idx = i;
    849 		if (kue_newbuf(sc, c, NULL) == ENOBUFS)
    850 			return (ENOBUFS);
    851 		if (c->kue_xfer == NULL) {
    852 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    853 			if (c->kue_xfer == NULL)
    854 				return (ENOBUFS);
    855 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    856 			if (c->kue_buf == NULL)
    857 				return (ENOBUFS); /* XXX free xfer */
    858 		}
    859 	}
    860 
    861 	return (0);
    862 }
    863 
    864 static int
    865 kue_tx_list_init(sc)
    866 	struct kue_softc	*sc;
    867 {
    868 	struct kue_cdata	*cd;
    869 	struct kue_chain	*c;
    870 	int			i;
    871 
    872 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    873 
    874 	cd = &sc->kue_cdata;
    875 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
    876 		c = &cd->kue_tx_chain[i];
    877 		c->kue_sc = sc;
    878 		c->kue_idx = i;
    879 		c->kue_mbuf = NULL;
    880 		if (c->kue_xfer == NULL) {
    881 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    882 			if (c->kue_xfer == NULL)
    883 				return (ENOBUFS);
    884 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    885 			if (c->kue_buf == NULL)
    886 				return (ENOBUFS);
    887 		}
    888 	}
    889 
    890 	return (0);
    891 }
    892 
    893 #ifdef __FreeBSD__
    894 static void
    895 kue_rxstart(ifp)
    896 	struct ifnet		*ifp;
    897 {
    898 	struct kue_softc	*sc;
    899 	struct kue_chain	*c;
    900 
    901 	sc = ifp->if_softc;
    902 	c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
    903 
    904 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
    905 		ifp->if_ierrors++;
    906 		return;
    907 	}
    908 
    909 	/* Setup new transfer. */
    910 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
    911 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    912 	    USBD_NO_TIMEOUT, kue_rxeof);
    913 	usbd_transfer(c->kue_xfer);
    914 }
    915 #endif
    916 
    917 /*
    918  * A frame has been uploaded: pass the resulting mbuf chain up to
    919  * the higher level protocols.
    920  */
    921 static void
    922 kue_rxeof(xfer, priv, status)
    923 	usbd_xfer_handle	xfer;
    924 	usbd_private_handle	priv;
    925 	usbd_status		status;
    926 {
    927 	struct kue_chain	*c = priv;
    928 	struct kue_softc	*sc = c->kue_sc;
    929 	struct ifnet		*ifp = GET_IFP(sc);
    930 	struct mbuf		*m;
    931 	int			total_len = 0;
    932 #if defined(__NetBSD__) || defined(__OpenBSD__)
    933 	int			s;
    934 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    935 
    936 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
    937 		     __FUNCTION__, status));
    938 
    939 	if (sc->kue_dying)
    940 		return;
    941 
    942 	if (!(ifp->if_flags & IFF_RUNNING))
    943 		return;
    944 
    945 	if (status != USBD_NORMAL_COMPLETION) {
    946 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    947 			return;
    948 		sc->kue_rx_errs++;
    949 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
    950 			printf("%s: %u usb errors on rx: %s\n",
    951 			    USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
    952 			    usbd_errstr(status));
    953 			sc->kue_rx_errs = 0;
    954 		}
    955 		if (status == USBD_STALLED)
    956 			usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
    957 		goto done;
    958 	}
    959 
    960 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    961 
    962 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
    963 		     __FUNCTION__, total_len,
    964 		     UGETW(mtod(c->kue_mbuf, u_int8_t *))));
    965 
    966 	m = c->kue_mbuf;
    967 	if (total_len <= 1)
    968 		goto done;
    969 
    970 	/* copy data to mbuf */
    971 	memcpy(mtod(m, char*), c->kue_buf, total_len);
    972 
    973 	/* No errors; receive the packet. */
    974 	total_len = UGETW(mtod(m, u_int8_t *));
    975 	m_adj(m, sizeof(u_int16_t));
    976 
    977 	if (total_len < sizeof(struct ether_header)) {
    978 		ifp->if_ierrors++;
    979 		goto done;
    980 	}
    981 
    982 	ifp->if_ipackets++;
    983 	m->m_pkthdr.len = m->m_len = total_len;
    984 
    985 #if defined(__FreeBSD__)
    986 	m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
    987 	/* Put the packet on the special USB input queue. */
    988 	usb_ether_input(m);
    989 
    990 	return;
    991 
    992 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    993 	m->m_pkthdr.rcvif = ifp;
    994 
    995 	s = splimp();
    996 
    997 	/* XXX ugly */
    998 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
    999 		ifp->if_ierrors++;
   1000 		goto done1;
   1001 	}
   1002 
   1003 #if NBPFILTER > 0
   1004 	/*
   1005 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1006 	 * don't pass it up to the ether_input() layer unless it's
   1007 	 * a broadcast packet, multicast packet, matches our ethernet
   1008 	 * address or the interface is in promiscuous mode.
   1009 	 */
   1010 	if (ifp->if_bpf) {
   1011 		struct ether_header *eh = mtod(m, struct ether_header *);
   1012 		BPF_MTAP(ifp, m);
   1013 		if ((ifp->if_flags & IFF_PROMISC) &&
   1014 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
   1015 			   ETHER_ADDR_LEN) &&
   1016 		    !(eh->ether_dhost[0] & 1)) {
   1017 			m_freem(m);
   1018 			goto done1;
   1019 		}
   1020 	}
   1021 #endif
   1022 
   1023 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
   1024 		    __FUNCTION__, m->m_len));
   1025 	(*ifp->if_input)(ifp, m);
   1026  done1:
   1027 	splx(s);
   1028 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1029 
   1030  done:
   1031 
   1032 	/* Setup new transfer. */
   1033 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
   1034 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1035 	    USBD_NO_TIMEOUT, kue_rxeof);
   1036 	usbd_transfer(c->kue_xfer);
   1037 
   1038 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
   1039 		    __FUNCTION__));
   1040 }
   1041 
   1042 /*
   1043  * A frame was downloaded to the chip. It's safe for us to clean up
   1044  * the list buffers.
   1045  */
   1046 
   1047 static void
   1048 kue_txeof(xfer, priv, status)
   1049 	usbd_xfer_handle	xfer;
   1050 	usbd_private_handle	priv;
   1051 	usbd_status		status;
   1052 {
   1053 	struct kue_chain	*c = priv;
   1054 	struct kue_softc	*sc = c->kue_sc;
   1055 	struct ifnet		*ifp = GET_IFP(sc);
   1056 	int			s;
   1057 
   1058 	if (sc->kue_dying)
   1059 		return;
   1060 
   1061 	s = splimp();
   1062 
   1063 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
   1064 		    __FUNCTION__, status));
   1065 
   1066 	ifp->if_timer = 0;
   1067 	ifp->if_flags &= ~IFF_OACTIVE;
   1068 
   1069 	if (status != USBD_NORMAL_COMPLETION) {
   1070 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1071 			splx(s);
   1072 			return;
   1073 		}
   1074 		ifp->if_oerrors++;
   1075 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
   1076 		    usbd_errstr(status));
   1077 		if (status == USBD_STALLED)
   1078 			usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
   1079 		splx(s);
   1080 		return;
   1081 	}
   1082 
   1083 	ifp->if_opackets++;
   1084 
   1085 #if defined(__FreeBSD__)
   1086 	c->kue_mbuf->m_pkthdr.rcvif = ifp;
   1087 	usb_tx_done(c->kue_mbuf);
   1088 	c->kue_mbuf = NULL;
   1089 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1090 	m_freem(c->kue_mbuf);
   1091 	c->kue_mbuf = NULL;
   1092 
   1093 	if (ifp->if_snd.ifq_head != NULL)
   1094 		kue_start(ifp);
   1095 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1096 
   1097 	splx(s);
   1098 }
   1099 
   1100 static int
   1101 kue_send(sc, m, idx)
   1102 	struct kue_softc	*sc;
   1103 	struct mbuf		*m;
   1104 	int			idx;
   1105 {
   1106 	int			total_len;
   1107 	struct kue_chain	*c;
   1108 	usbd_status		err;
   1109 
   1110 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1111 
   1112 	c = &sc->kue_cdata.kue_tx_chain[idx];
   1113 
   1114 	/*
   1115 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1116 	 * bytes at the beginning to hold the frame length.
   1117 	 */
   1118 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
   1119 	c->kue_mbuf = m;
   1120 
   1121 	total_len = m->m_pkthdr.len + 2;
   1122 	/* XXX what's this? */
   1123 	total_len += 64 - (total_len % 64);
   1124 
   1125 	/* Frame length is specified in the first 2 bytes of the buffer. */
   1126 	c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1127 	c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1128 
   1129 	/* XXX 10000 */
   1130 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
   1131 	    c, c->kue_buf, total_len, USBD_NO_COPY, 10000, kue_txeof);
   1132 
   1133 	/* Transmit */
   1134 	err = usbd_transfer(c->kue_xfer);
   1135 	if (err != USBD_IN_PROGRESS) {
   1136 		kue_stop(sc);
   1137 		return (EIO);
   1138 	}
   1139 
   1140 	sc->kue_cdata.kue_tx_cnt++;
   1141 
   1142 	return (0);
   1143 }
   1144 
   1145 static void
   1146 kue_start(ifp)
   1147 	struct ifnet		*ifp;
   1148 {
   1149 	struct kue_softc	*sc = ifp->if_softc;
   1150 	struct mbuf		*m_head = NULL;
   1151 
   1152 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1153 
   1154 	if (sc->kue_dying)
   1155 		return;
   1156 
   1157 	if (ifp->if_flags & IFF_OACTIVE)
   1158 		return;
   1159 
   1160 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1161 	if (m_head == NULL)
   1162 		return;
   1163 
   1164 	if (kue_send(sc, m_head, 0)) {
   1165 		IF_PREPEND(&ifp->if_snd, m_head);
   1166 		ifp->if_flags |= IFF_OACTIVE;
   1167 		return;
   1168 	}
   1169 
   1170 #if NBPFILTER > 0
   1171 	/*
   1172 	 * If there's a BPF listener, bounce a copy of this frame
   1173 	 * to him.
   1174 	 */
   1175 	if (ifp->if_bpf)
   1176 		BPF_MTAP(ifp, m_head);
   1177 #endif
   1178 
   1179 	ifp->if_flags |= IFF_OACTIVE;
   1180 
   1181 	/*
   1182 	 * Set a timeout in case the chip goes out to lunch.
   1183 	 */
   1184 	ifp->if_timer = 5;
   1185 }
   1186 
   1187 static void
   1188 kue_init(xsc)
   1189 	void			*xsc;
   1190 {
   1191 	struct kue_softc	*sc = xsc;
   1192 	struct ifnet		*ifp = GET_IFP(sc);
   1193 	int			s;
   1194 	u_char			*eaddr;
   1195 
   1196 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1197 
   1198 	if (ifp->if_flags & IFF_RUNNING)
   1199 		return;
   1200 
   1201 	s = splimp();
   1202 
   1203 #if defined(__FreeBSD__)
   1204 	eaddr = sc->arpcom.ac_enaddr;
   1205 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1206 	eaddr = LLADDR(ifp->if_sadl);
   1207 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1208 	/* Set MAC address */
   1209 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
   1210 
   1211 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
   1212 
   1213 	 /* If we want promiscuous mode, set the allframes bit. */
   1214 	if (ifp->if_flags & IFF_PROMISC)
   1215 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
   1216 
   1217 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
   1218 
   1219 	/* I'm not sure how to tune these. */
   1220 #if 0
   1221 	/*
   1222 	 * Leave this one alone for now; setting it
   1223 	 * wrong causes lockups on some machines/controllers.
   1224 	 */
   1225 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
   1226 #endif
   1227 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
   1228 
   1229 	/* Init TX ring. */
   1230 	if (kue_tx_list_init(sc) == ENOBUFS) {
   1231 		printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
   1232 		splx(s);
   1233 		return;
   1234 	}
   1235 
   1236 	/* Init RX ring. */
   1237 	if (kue_rx_list_init(sc) == ENOBUFS) {
   1238 		printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
   1239 		splx(s);
   1240 		return;
   1241 	}
   1242 
   1243 	/* Load the multicast filter. */
   1244 	kue_setmulti(sc);
   1245 
   1246 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
   1247 		if (kue_open_pipes(sc)) {
   1248 			splx(s);
   1249 			return;
   1250 		}
   1251 	}
   1252 
   1253 	ifp->if_flags |= IFF_RUNNING;
   1254 	ifp->if_flags &= ~IFF_OACTIVE;
   1255 
   1256 	splx(s);
   1257 }
   1258 
   1259 static int
   1260 kue_open_pipes(sc)
   1261 	struct kue_softc	*sc;
   1262 {
   1263 	usbd_status		err;
   1264 	struct kue_chain	*c;
   1265 	int			i;
   1266 
   1267 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1268 
   1269 	/* Open RX and TX pipes. */
   1270 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
   1271 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
   1272 	if (err) {
   1273 		printf("%s: open rx pipe failed: %s\n",
   1274 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1275 		return (EIO);
   1276 	}
   1277 
   1278 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
   1279 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
   1280 	if (err) {
   1281 		printf("%s: open tx pipe failed: %s\n",
   1282 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1283 		return (EIO);
   1284 	}
   1285 
   1286 	/* Start up the receive pipe. */
   1287 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1288 		c = &sc->kue_cdata.kue_rx_chain[i];
   1289 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
   1290 		    c, c->kue_buf, KUE_BUFSZ,
   1291 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1292 		    kue_rxeof);
   1293 		usbd_transfer(c->kue_xfer);
   1294 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
   1295 			    __FUNCTION__));
   1296 	}
   1297 
   1298 	return (0);
   1299 }
   1300 
   1301 static int
   1302 kue_ioctl(ifp, command, data)
   1303 	struct ifnet		*ifp;
   1304 	u_long			command;
   1305 	caddr_t			data;
   1306 {
   1307 	struct kue_softc	*sc = ifp->if_softc;
   1308 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1309 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1310 	struct ifreq		*ifr = (struct ifreq *)data;
   1311 #endif
   1312 	int			s, error = 0;
   1313 
   1314 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1315 
   1316 	if (sc->kue_dying)
   1317 		return (EIO);
   1318 
   1319 	s = splimp();
   1320 
   1321 	switch(command) {
   1322 #if defined(__FreeBSD__)
   1323 	case SIOCSIFADDR:
   1324 	case SIOCGIFADDR:
   1325 	case SIOCSIFMTU:
   1326 		error = ether_ioctl(ifp, command, data);
   1327 		break;
   1328 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1329 	case SIOCSIFADDR:
   1330 		ifp->if_flags |= IFF_UP;
   1331 		kue_init(sc);
   1332 
   1333 		switch (ifa->ifa_addr->sa_family) {
   1334 #ifdef INET
   1335 		case AF_INET:
   1336 			arp_ifinit(ifp, ifa);
   1337 			break;
   1338 #endif /* INET */
   1339 #ifdef NS
   1340 		case AF_NS:
   1341 		    {
   1342 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1343 
   1344 			if (ns_nullhost(*ina))
   1345 				ina->x_host = *(union ns_host *)
   1346 					LLADDR(ifp->if_sadl);
   1347 			else
   1348 				memcpy(LLADDR(ifp->if_sadl),
   1349 				       ina->x_host.c_host,
   1350 				       ifp->if_addrlen);
   1351 			break;
   1352 		    }
   1353 #endif /* NS */
   1354 		}
   1355 		break;
   1356 
   1357 	case SIOCSIFMTU:
   1358 		if (ifr->ifr_mtu > ETHERMTU)
   1359 			error = EINVAL;
   1360 		else
   1361 			ifp->if_mtu = ifr->ifr_mtu;
   1362 		break;
   1363 
   1364 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1365 
   1366 	case SIOCSIFFLAGS:
   1367 		if (ifp->if_flags & IFF_UP) {
   1368 			if (ifp->if_flags & IFF_RUNNING &&
   1369 			    ifp->if_flags & IFF_PROMISC &&
   1370 			    !(sc->kue_if_flags & IFF_PROMISC)) {
   1371 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
   1372 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1373 				    sc->kue_rxfilt);
   1374 			} else if (ifp->if_flags & IFF_RUNNING &&
   1375 			    !(ifp->if_flags & IFF_PROMISC) &&
   1376 			    sc->kue_if_flags & IFF_PROMISC) {
   1377 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
   1378 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1379 				    sc->kue_rxfilt);
   1380 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1381 				kue_init(sc);
   1382 		} else {
   1383 			if (ifp->if_flags & IFF_RUNNING)
   1384 				kue_stop(sc);
   1385 		}
   1386 		sc->kue_if_flags = ifp->if_flags;
   1387 		error = 0;
   1388 		break;
   1389 	case SIOCADDMULTI:
   1390 	case SIOCDELMULTI:
   1391 		kue_setmulti(sc);
   1392 		error = 0;
   1393 		break;
   1394 	default:
   1395 		error = EINVAL;
   1396 		break;
   1397 	}
   1398 
   1399 	splx(s);
   1400 
   1401 	return (error);
   1402 }
   1403 
   1404 static void
   1405 kue_watchdog(ifp)
   1406 	struct ifnet		*ifp;
   1407 {
   1408 	struct kue_softc	*sc = ifp->if_softc;
   1409 
   1410 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1411 
   1412 	if (sc->kue_dying)
   1413 		return;
   1414 
   1415 	ifp->if_oerrors++;
   1416 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
   1417 
   1418 	/*
   1419 	 * The polling business is a kludge to avoid allowing the
   1420 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1421 	 * kill us since the watchdog routine is invoked from
   1422 	 * interrupt context.
   1423 	 */
   1424 	usbd_set_polling(sc->kue_udev, 1);
   1425 	kue_stop(sc);
   1426 	kue_init(sc);
   1427 	usbd_set_polling(sc->kue_udev, 0);
   1428 
   1429 	if (ifp->if_snd.ifq_head != NULL)
   1430 		kue_start(ifp);
   1431 }
   1432 
   1433 /*
   1434  * Stop the adapter and free any mbufs allocated to the
   1435  * RX and TX lists.
   1436  */
   1437 static void
   1438 kue_stop(sc)
   1439 	struct kue_softc	*sc;
   1440 {
   1441 	usbd_status		err;
   1442 	struct ifnet		*ifp;
   1443 	int			i;
   1444 
   1445 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1446 
   1447 	ifp = GET_IFP(sc);
   1448 	ifp->if_timer = 0;
   1449 
   1450 	/* Stop transfers. */
   1451 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
   1452 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1453 		if (err) {
   1454 			printf("%s: abort rx pipe failed: %s\n",
   1455 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1456 		}
   1457 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1458 		if (err) {
   1459 			printf("%s: close rx pipe failed: %s\n",
   1460 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1461 		}
   1462 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
   1463 	}
   1464 
   1465 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
   1466 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1467 		if (err) {
   1468 			printf("%s: abort tx pipe failed: %s\n",
   1469 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1470 		}
   1471 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1472 		if (err) {
   1473 			printf("%s: close tx pipe failed: %s\n",
   1474 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1475 		}
   1476 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
   1477 	}
   1478 
   1479 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
   1480 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1481 		if (err) {
   1482 			printf("%s: abort intr pipe failed: %s\n",
   1483 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1484 		}
   1485 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1486 		if (err) {
   1487 			printf("%s: close intr pipe failed: %s\n",
   1488 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1489 		}
   1490 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
   1491 	}
   1492 
   1493 	/* Free RX resources. */
   1494 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1495 		if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
   1496 			m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
   1497 			sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
   1498 		}
   1499 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
   1500 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
   1501 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
   1502 		}
   1503 	}
   1504 
   1505 	/* Free TX resources. */
   1506 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
   1507 		if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
   1508 			m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
   1509 			sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
   1510 		}
   1511 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
   1512 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
   1513 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
   1514 		}
   1515 	}
   1516 
   1517 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1518 }
   1519 
   1520 #ifdef __FreeBSD__
   1521 /*
   1522  * Stop all chip I/O so that the kernel's probe routines don't
   1523  * get confused by errant DMAs when rebooting.
   1524  */
   1525 static void
   1526 kue_shutdown(dev)
   1527 	device_t		dev;
   1528 {
   1529 	struct kue_softc	*sc;
   1530 
   1531 	sc = device_get_softc(dev);
   1532 
   1533 	kue_stop(sc);
   1534 }
   1535 #endif
   1536