Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.74
      1 /*	$NetBSD: if_cue.c,v 1.74 2016/11/25 12:56:29 skrll 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_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
     34  */
     35 
     36 /*
     37  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
     38  * adapters and others.
     39  *
     40  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     41  * Electrical Engineering Department
     42  * Columbia University, New York City
     43  */
     44 
     45 /*
     46  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
     47  * RX filter uses a 512-bit multicast hash table, single perfect entry
     48  * for the station address, and promiscuous mode. Unlike the ADMtek
     49  * and KLSI chips, the CATC ASIC supports read and write combining
     50  * mode where multiple packets can be transfered using a single bulk
     51  * transaction, which helps performance a great deal.
     52  */
     53 
     54 /*
     55  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __KERNEL_RCSID(0, "$NetBSD: if_cue.c,v 1.74 2016/11/25 12:56:29 skrll Exp $");
     60 
     61 #ifdef _KERNEL_OPT
     62 #include "opt_inet.h"
     63 #include "opt_usb.h"
     64 #endif
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/callout.h>
     69 #include <sys/sockio.h>
     70 #include <sys/mbuf.h>
     71 #include <sys/kernel.h>
     72 #include <sys/socket.h>
     73 #include <sys/bus.h>
     74 #include <sys/device.h>
     75 
     76 #include <net/if.h>
     77 #include <net/if_arp.h>
     78 #include <net/if_dl.h>
     79 #include <net/bpf.h>
     80 #include <net/if_ether.h>
     81 
     82 #ifdef INET
     83 #include <netinet/in.h>
     84 #include <netinet/if_inarp.h>
     85 #endif
     86 
     87 #include <dev/usb/usb.h>
     88 #include <dev/usb/usbdi.h>
     89 #include <dev/usb/usbdi_util.h>
     90 #include <dev/usb/usbdivar.h>
     91 #include <dev/usb/usbdevs.h>
     92 
     93 #include <dev/usb/if_cuereg.h>
     94 
     95 #ifdef CUE_DEBUG
     96 #define DPRINTF(x)	if (cuedebug) printf x
     97 #define DPRINTFN(n,x)	if (cuedebug >= (n)) printf x
     98 int	cuedebug = 0;
     99 #else
    100 #define DPRINTF(x)
    101 #define DPRINTFN(n,x)
    102 #endif
    103 
    104 /*
    105  * Various supported device vendors/products.
    106  */
    107 Static struct usb_devno cue_devs[] = {
    108 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
    109 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
    110 	{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
    111 	/* Belkin F5U111 adapter covered by NETMATE entry */
    112 };
    113 #define cue_lookup(v, p) (usb_lookup(cue_devs, v, p))
    114 
    115 int cue_match(device_t, cfdata_t, void *);
    116 void cue_attach(device_t, device_t, void *);
    117 int cue_detach(device_t, int);
    118 int cue_activate(device_t, enum devact);
    119 extern struct cfdriver cue_cd;
    120 CFATTACH_DECL_NEW(cue, sizeof(struct cue_softc), cue_match, cue_attach,
    121     cue_detach, cue_activate);
    122 
    123 Static int cue_open_pipes(struct cue_softc *);
    124 Static int cue_tx_list_init(struct cue_softc *);
    125 Static int cue_rx_list_init(struct cue_softc *);
    126 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
    127 Static int cue_send(struct cue_softc *, struct mbuf *, int);
    128 Static void cue_rxeof(struct usbd_xfer *, void *, usbd_status);
    129 Static void cue_txeof(struct usbd_xfer *, void *, usbd_status);
    130 Static void cue_tick(void *);
    131 Static void cue_tick_task(void *);
    132 Static void cue_start(struct ifnet *);
    133 Static int cue_ioctl(struct ifnet *, u_long, void *);
    134 Static void cue_init(void *);
    135 Static void cue_stop(struct cue_softc *);
    136 Static void cue_watchdog(struct ifnet *);
    137 
    138 Static void cue_setmulti(struct cue_softc *);
    139 Static uint32_t cue_crc(const char *);
    140 Static void cue_reset(struct cue_softc *);
    141 
    142 Static int cue_csr_read_1(struct cue_softc *, int);
    143 Static int cue_csr_write_1(struct cue_softc *, int, int);
    144 Static int cue_csr_read_2(struct cue_softc *, int);
    145 #if 0
    146 Static int cue_csr_write_2(struct cue_softc *, int, int);
    147 #endif
    148 Static int cue_mem(struct cue_softc *, int, int, void *, int);
    149 Static int cue_getmac(struct cue_softc *, void *);
    150 
    151 #define CUE_SETBIT(sc, reg, x)				\
    152 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
    153 
    154 #define CUE_CLRBIT(sc, reg, x)				\
    155 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
    156 
    157 Static int
    158 cue_csr_read_1(struct cue_softc	*sc, int reg)
    159 {
    160 	usb_device_request_t	req;
    161 	usbd_status		err;
    162 	uint8_t			val = 0;
    163 
    164 	if (sc->cue_dying)
    165 		return 0;
    166 
    167 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    168 	req.bRequest = CUE_CMD_READREG;
    169 	USETW(req.wValue, 0);
    170 	USETW(req.wIndex, reg);
    171 	USETW(req.wLength, 1);
    172 
    173 	err = usbd_do_request(sc->cue_udev, &req, &val);
    174 
    175 	if (err) {
    176 		DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n",
    177 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    178 		return 0;
    179 	}
    180 
    181 	DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n",
    182 	    device_xname(sc->cue_dev), reg, val));
    183 
    184 	return val;
    185 }
    186 
    187 Static int
    188 cue_csr_read_2(struct cue_softc	*sc, int reg)
    189 {
    190 	usb_device_request_t	req;
    191 	usbd_status		err;
    192 	uWord			val;
    193 
    194 	if (sc->cue_dying)
    195 		return 0;
    196 
    197 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    198 	req.bRequest = CUE_CMD_READREG;
    199 	USETW(req.wValue, 0);
    200 	USETW(req.wIndex, reg);
    201 	USETW(req.wLength, 2);
    202 
    203 	err = usbd_do_request(sc->cue_udev, &req, &val);
    204 
    205 	DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n",
    206 	    device_xname(sc->cue_dev), reg, UGETW(val)));
    207 
    208 	if (err) {
    209 		DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
    210 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    211 		return 0;
    212 	}
    213 
    214 	return UGETW(val);
    215 }
    216 
    217 Static int
    218 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
    219 {
    220 	usb_device_request_t	req;
    221 	usbd_status		err;
    222 
    223 	if (sc->cue_dying)
    224 		return 0;
    225 
    226 	DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n",
    227 	    device_xname(sc->cue_dev), reg, val));
    228 
    229 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    230 	req.bRequest = CUE_CMD_WRITEREG;
    231 	USETW(req.wValue, val);
    232 	USETW(req.wIndex, reg);
    233 	USETW(req.wLength, 0);
    234 
    235 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    236 
    237 	if (err) {
    238 		DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n",
    239 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    240 		return -1;
    241 	}
    242 
    243 	DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n",
    244 	    device_xname(sc->cue_dev), reg, cue_csr_read_1(sc, reg)));
    245 
    246 	return 0;
    247 }
    248 
    249 #if 0
    250 Static int
    251 cue_csr_write_2(struct cue_softc *sc, int reg, int aval)
    252 {
    253 	usb_device_request_t	req;
    254 	usbd_status		err;
    255 	uWord			val;
    256 	int			s;
    257 
    258 	if (sc->cue_dying)
    259 		return 0;
    260 
    261 	DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n",
    262 	    device_xname(sc->cue_dev), reg, aval));
    263 
    264 	USETW(val, aval);
    265 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    266 	req.bRequest = CUE_CMD_WRITEREG;
    267 	USETW(req.wValue, val);
    268 	USETW(req.wIndex, reg);
    269 	USETW(req.wLength, 0);
    270 
    271 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    272 
    273 	if (err) {
    274 		DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n",
    275 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    276 		return -1;
    277 	}
    278 
    279 	return 0;
    280 }
    281 #endif
    282 
    283 Static int
    284 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
    285 {
    286 	usb_device_request_t	req;
    287 	usbd_status		err;
    288 
    289 	DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n",
    290 	    device_xname(sc->cue_dev), cmd, addr, len));
    291 
    292 	if (cmd == CUE_CMD_READSRAM)
    293 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    294 	else
    295 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    296 	req.bRequest = cmd;
    297 	USETW(req.wValue, 0);
    298 	USETW(req.wIndex, addr);
    299 	USETW(req.wLength, len);
    300 
    301 	err = usbd_do_request(sc->cue_udev, &req, buf);
    302 
    303 	if (err) {
    304 		DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n",
    305 		    device_xname(sc->cue_dev), addr, usbd_errstr(err)));
    306 		return -1;
    307 	}
    308 
    309 	return 0;
    310 }
    311 
    312 Static int
    313 cue_getmac(struct cue_softc *sc, void *buf)
    314 {
    315 	usb_device_request_t	req;
    316 	usbd_status		err;
    317 
    318 	DPRINTFN(10,("%s: cue_getmac\n", device_xname(sc->cue_dev)));
    319 
    320 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    321 	req.bRequest = CUE_CMD_GET_MACADDR;
    322 	USETW(req.wValue, 0);
    323 	USETW(req.wIndex, 0);
    324 	USETW(req.wLength, ETHER_ADDR_LEN);
    325 
    326 	err = usbd_do_request(sc->cue_udev, &req, buf);
    327 
    328 	if (err) {
    329 		printf("%s: read MAC address failed\n",
    330 		    device_xname(sc->cue_dev));
    331 		return -1;
    332 	}
    333 
    334 	return 0;
    335 }
    336 
    337 #define CUE_POLY	0xEDB88320
    338 #define CUE_BITS	9
    339 
    340 Static uint32_t
    341 cue_crc(const char *addr)
    342 {
    343 	uint32_t		idx, bit, data, crc;
    344 
    345 	/* Compute CRC for the address value. */
    346 	crc = 0xFFFFFFFF; /* initial value */
    347 
    348 	for (idx = 0; idx < 6; idx++) {
    349 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    350 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
    351 	}
    352 
    353 	return crc & ((1 << CUE_BITS) - 1);
    354 }
    355 
    356 Static void
    357 cue_setmulti(struct cue_softc *sc)
    358 {
    359 	struct ifnet		*ifp;
    360 	struct ether_multi	*enm;
    361 	struct ether_multistep	step;
    362 	uint32_t		h, i;
    363 
    364 	ifp = GET_IFP(sc);
    365 
    366 	DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
    367 	    device_xname(sc->cue_dev), ifp->if_flags));
    368 
    369 	if (ifp->if_flags & IFF_PROMISC) {
    370 allmulti:
    371 		ifp->if_flags |= IFF_ALLMULTI;
    372 		for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
    373 			sc->cue_mctab[i] = 0xFF;
    374 		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
    375 		    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
    376 		return;
    377 	}
    378 
    379 	/* first, zot all the existing hash bits */
    380 	for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
    381 		sc->cue_mctab[i] = 0;
    382 
    383 	/* now program new ones */
    384 	ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
    385 	while (enm != NULL) {
    386 		if (memcmp(enm->enm_addrlo,
    387 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
    388 			goto allmulti;
    389 
    390 		h = cue_crc(enm->enm_addrlo);
    391 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
    392 		ETHER_NEXT_MULTI(step, enm);
    393 	}
    394 
    395 	ifp->if_flags &= ~IFF_ALLMULTI;
    396 
    397 	/*
    398 	 * Also include the broadcast address in the filter
    399 	 * so we can receive broadcast frames.
    400 	 */
    401 	if (ifp->if_flags & IFF_BROADCAST) {
    402 		h = cue_crc(etherbroadcastaddr);
    403 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
    404 	}
    405 
    406 	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
    407 	    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
    408 }
    409 
    410 Static void
    411 cue_reset(struct cue_softc *sc)
    412 {
    413 	usb_device_request_t	req;
    414 	usbd_status		err;
    415 
    416 	DPRINTFN(2,("%s: cue_reset\n", device_xname(sc->cue_dev)));
    417 
    418 	if (sc->cue_dying)
    419 		return;
    420 
    421 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    422 	req.bRequest = CUE_CMD_RESET;
    423 	USETW(req.wValue, 0);
    424 	USETW(req.wIndex, 0);
    425 	USETW(req.wLength, 0);
    426 
    427 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    428 
    429 	if (err)
    430 		printf("%s: reset failed\n", device_xname(sc->cue_dev));
    431 
    432 	/* Wait a little while for the chip to get its brains in order. */
    433 	usbd_delay_ms(sc->cue_udev, 1);
    434 }
    435 
    436 /*
    437  * Probe for a CATC chip.
    438  */
    439 int
    440 cue_match(device_t parent, cfdata_t match, void *aux)
    441 {
    442 	struct usb_attach_arg *uaa = aux;
    443 
    444 	return cue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    445 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    446 }
    447 
    448 /*
    449  * Attach the interface. Allocate softc structures, do ifmedia
    450  * setup and ethernet/BPF attach.
    451  */
    452 void
    453 cue_attach(device_t parent, device_t self, void *aux)
    454 {
    455 	struct cue_softc *sc = device_private(self);
    456 	struct usb_attach_arg *uaa = aux;
    457 	char			*devinfop;
    458 	int			s;
    459 	u_char			eaddr[ETHER_ADDR_LEN];
    460 	struct usbd_device *	dev = uaa->uaa_device;
    461 	struct usbd_interface *	iface;
    462 	usbd_status		err;
    463 	struct ifnet		*ifp;
    464 	usb_interface_descriptor_t	*id;
    465 	usb_endpoint_descriptor_t	*ed;
    466 	int			i;
    467 
    468 	DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
    469 
    470 	sc->cue_dev = self;
    471 
    472 	aprint_naive("\n");
    473 	aprint_normal("\n");
    474 
    475 	devinfop = usbd_devinfo_alloc(dev, 0);
    476 	aprint_normal_dev(self, "%s\n", devinfop);
    477 	usbd_devinfo_free(devinfop);
    478 
    479 	err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
    480 	if (err) {
    481 		aprint_error_dev(self, "failed to set configuration"
    482 		    ", err=%s\n", usbd_errstr(err));
    483 		return;
    484 	}
    485 
    486 	sc->cue_udev = dev;
    487 	sc->cue_product = uaa->uaa_product;
    488 	sc->cue_vendor = uaa->uaa_vendor;
    489 
    490 	usb_init_task(&sc->cue_tick_task, cue_tick_task, sc, 0);
    491 	usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc, 0);
    492 
    493 	err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
    494 	if (err) {
    495 		aprint_error_dev(self, "getting interface handle failed\n");
    496 		return;
    497 	}
    498 
    499 	sc->cue_iface = iface;
    500 	id = usbd_get_interface_descriptor(iface);
    501 
    502 	/* Find endpoints. */
    503 	for (i = 0; i < id->bNumEndpoints; i++) {
    504 		ed = usbd_interface2endpoint_descriptor(iface, i);
    505 		if (ed == NULL) {
    506 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    507 			return;
    508 		}
    509 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    510 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    511 			sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
    512 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    513 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    514 			sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
    515 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    516 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    517 			sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
    518 		}
    519 	}
    520 
    521 #if 0
    522 	/* Reset the adapter. */
    523 	cue_reset(sc);
    524 #endif
    525 	/*
    526 	 * Get station address.
    527 	 */
    528 	cue_getmac(sc, &eaddr);
    529 
    530 	s = splnet();
    531 
    532 	/*
    533 	 * A CATC chip was detected. Inform the world.
    534 	 */
    535 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    536 
    537 	/* Initialize interface info.*/
    538 	ifp = GET_IFP(sc);
    539 	ifp->if_softc = sc;
    540 	ifp->if_mtu = ETHERMTU;
    541 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    542 	ifp->if_ioctl = cue_ioctl;
    543 	ifp->if_start = cue_start;
    544 	ifp->if_watchdog = cue_watchdog;
    545 	strncpy(ifp->if_xname, device_xname(sc->cue_dev), IFNAMSIZ);
    546 
    547 	IFQ_SET_READY(&ifp->if_snd);
    548 
    549 	/* Attach the interface. */
    550 	if_attach(ifp);
    551 	ether_ifattach(ifp, eaddr);
    552 	rnd_attach_source(&sc->rnd_source, device_xname(sc->cue_dev),
    553 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    554 
    555 	callout_init(&(sc->cue_stat_ch), 0);
    556 
    557 	sc->cue_attached = 1;
    558 	splx(s);
    559 
    560 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev, sc->cue_dev);
    561 
    562 	return;
    563 }
    564 
    565 int
    566 cue_detach(device_t self, int flags)
    567 {
    568 	struct cue_softc *sc = device_private(self);
    569 	struct ifnet		*ifp = GET_IFP(sc);
    570 	int			s;
    571 
    572 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    573 
    574 	callout_stop(&sc->cue_stat_ch);
    575 	/*
    576 	 * Remove any pending task.  It cannot be executing because it run
    577 	 * in the same thread as detach.
    578 	 */
    579 	usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
    580 	usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
    581 
    582 	if (!sc->cue_attached) {
    583 		/* Detached before attached finished, so just bail out. */
    584 		return 0;
    585 	}
    586 
    587 	s = splusb();
    588 
    589 	if (ifp->if_flags & IFF_RUNNING)
    590 		cue_stop(sc);
    591 
    592 	rnd_detach_source(&sc->rnd_source);
    593 	ether_ifdetach(ifp);
    594 
    595 	if_detach(ifp);
    596 
    597 #ifdef DIAGNOSTIC
    598 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
    599 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
    600 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
    601 		aprint_debug_dev(self, "detach has active endpoints\n");
    602 #endif
    603 
    604 	sc->cue_attached = 0;
    605 	splx(s);
    606 
    607 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev);
    608 
    609 	return 0;
    610 }
    611 
    612 int
    613 cue_activate(device_t self, enum devact act)
    614 {
    615 	struct cue_softc *sc = device_private(self);
    616 
    617 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    618 
    619 	switch (act) {
    620 	case DVACT_DEACTIVATE:
    621 		/* Deactivate the interface. */
    622 		if_deactivate(&sc->cue_ec.ec_if);
    623 		sc->cue_dying = 1;
    624 		return 0;
    625 	default:
    626 		return EOPNOTSUPP;
    627 	}
    628 }
    629 
    630 /*
    631  * Initialize an RX descriptor and attach an MBUF cluster.
    632  */
    633 Static int
    634 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
    635 {
    636 	struct mbuf		*m_new = NULL;
    637 
    638 	if (m == NULL) {
    639 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    640 		if (m_new == NULL) {
    641 			printf("%s: no memory for rx list "
    642 			    "-- packet dropped!\n", device_xname(sc->cue_dev));
    643 			return ENOBUFS;
    644 		}
    645 
    646 		MCLGET(m_new, M_DONTWAIT);
    647 		if (!(m_new->m_flags & M_EXT)) {
    648 			printf("%s: no memory for rx list "
    649 			    "-- packet dropped!\n", device_xname(sc->cue_dev));
    650 			m_freem(m_new);
    651 			return ENOBUFS;
    652 		}
    653 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    654 	} else {
    655 		m_new = m;
    656 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    657 		m_new->m_data = m_new->m_ext.ext_buf;
    658 	}
    659 
    660 	m_adj(m_new, ETHER_ALIGN);
    661 	c->cue_mbuf = m_new;
    662 
    663 	return 0;
    664 }
    665 
    666 Static int
    667 cue_rx_list_init(struct cue_softc *sc)
    668 {
    669 	struct cue_cdata	*cd;
    670 	struct cue_chain	*c;
    671 	int			i;
    672 
    673 	cd = &sc->cue_cdata;
    674 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
    675 		c = &cd->cue_rx_chain[i];
    676 		c->cue_sc = sc;
    677 		c->cue_idx = i;
    678 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
    679 			return ENOBUFS;
    680 		if (c->cue_xfer == NULL) {
    681 			int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX],
    682 			    CUE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->cue_xfer);
    683 			if (error)
    684 				return error;
    685 			c->cue_buf = usbd_get_buffer(c->cue_xfer);
    686 		}
    687 	}
    688 
    689 	return 0;
    690 }
    691 
    692 Static int
    693 cue_tx_list_init(struct cue_softc *sc)
    694 {
    695 	struct cue_cdata	*cd;
    696 	struct cue_chain	*c;
    697 	int			i;
    698 
    699 	cd = &sc->cue_cdata;
    700 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
    701 		c = &cd->cue_tx_chain[i];
    702 		c->cue_sc = sc;
    703 		c->cue_idx = i;
    704 		c->cue_mbuf = NULL;
    705 		if (c->cue_xfer == NULL) {
    706 			int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX],
    707 			    CUE_BUFSZ, 0, 0, &c->cue_xfer);
    708 			if (error)
    709 				return error;
    710 			c->cue_buf = usbd_get_buffer(c->cue_xfer);
    711 		}
    712 	}
    713 
    714 	return 0;
    715 }
    716 
    717 /*
    718  * A frame has been uploaded: pass the resulting mbuf chain up to
    719  * the higher level protocols.
    720  */
    721 Static void
    722 cue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    723 {
    724 	struct cue_chain	*c = priv;
    725 	struct cue_softc	*sc = c->cue_sc;
    726 	struct ifnet		*ifp = GET_IFP(sc);
    727 	struct mbuf		*m;
    728 	int			total_len = 0;
    729 	uint16_t		len;
    730 	int			s;
    731 
    732 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
    733 		     __func__, status));
    734 
    735 	if (sc->cue_dying)
    736 		return;
    737 
    738 	if (!(ifp->if_flags & IFF_RUNNING))
    739 		return;
    740 
    741 	if (status != USBD_NORMAL_COMPLETION) {
    742 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    743 			return;
    744 		sc->cue_rx_errs++;
    745 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
    746 			printf("%s: %u usb errors on rx: %s\n",
    747 			    device_xname(sc->cue_dev), sc->cue_rx_errs,
    748 			    usbd_errstr(status));
    749 			sc->cue_rx_errs = 0;
    750 		}
    751 		if (status == USBD_STALLED)
    752 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
    753 		goto done;
    754 	}
    755 
    756 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    757 
    758 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
    759 
    760 	m = c->cue_mbuf;
    761 	len = UGETW(mtod(m, uint8_t *));
    762 
    763 	/* No errors; receive the packet. */
    764 	total_len = len;
    765 
    766 	if (len < sizeof(struct ether_header)) {
    767 		ifp->if_ierrors++;
    768 		goto done;
    769 	}
    770 
    771 	ifp->if_ipackets++;
    772 	m_adj(m, sizeof(uint16_t));
    773 	m->m_pkthdr.len = m->m_len = total_len;
    774 
    775 	m_set_rcvif(m, ifp);
    776 
    777 	s = splnet();
    778 
    779 	/* XXX ugly */
    780 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    781 		ifp->if_ierrors++;
    782 		goto done1;
    783 	}
    784 
    785 	/*
    786 	 * Handle BPF listeners. Let the BPF user see the packet, but
    787 	 * don't pass it up to the ether_input() layer unless it's
    788 	 * a broadcast packet, multicast packet, matches our ethernet
    789 	 * address or the interface is in promiscuous mode.
    790 	 */
    791 	bpf_mtap(ifp, m);
    792 
    793 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev),
    794 		    __func__, m->m_len));
    795 	if_percpuq_enqueue(ifp->if_percpuq, m);
    796  done1:
    797 	splx(s);
    798 
    799 done:
    800 
    801 	/* Setup new transfer. */
    802 	usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
    803 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
    804 	usbd_transfer(c->cue_xfer);
    805 
    806 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev),
    807 		    __func__));
    808 }
    809 
    810 /*
    811  * A frame was downloaded to the chip. It's safe for us to clean up
    812  * the list buffers.
    813  */
    814 Static void
    815 cue_txeof(struct usbd_xfer *xfer, void *priv,
    816     usbd_status status)
    817 {
    818 	struct cue_chain	*c = priv;
    819 	struct cue_softc	*sc = c->cue_sc;
    820 	struct ifnet		*ifp = GET_IFP(sc);
    821 	int			s;
    822 
    823 	if (sc->cue_dying)
    824 		return;
    825 
    826 	s = splnet();
    827 
    828 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
    829 		    __func__, status));
    830 
    831 	ifp->if_timer = 0;
    832 	ifp->if_flags &= ~IFF_OACTIVE;
    833 
    834 	if (status != USBD_NORMAL_COMPLETION) {
    835 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    836 			splx(s);
    837 			return;
    838 		}
    839 		ifp->if_oerrors++;
    840 		printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev),
    841 		    usbd_errstr(status));
    842 		if (status == USBD_STALLED)
    843 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
    844 		splx(s);
    845 		return;
    846 	}
    847 
    848 	ifp->if_opackets++;
    849 
    850 	m_freem(c->cue_mbuf);
    851 	c->cue_mbuf = NULL;
    852 
    853 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    854 		cue_start(ifp);
    855 
    856 	splx(s);
    857 }
    858 
    859 Static void
    860 cue_tick(void *xsc)
    861 {
    862 	struct cue_softc	*sc = xsc;
    863 
    864 	if (sc == NULL)
    865 		return;
    866 
    867 	if (sc->cue_dying)
    868 		return;
    869 
    870 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    871 
    872 	/* Perform statistics update in process context. */
    873 	usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
    874 }
    875 
    876 Static void
    877 cue_tick_task(void *xsc)
    878 {
    879 	struct cue_softc	*sc = xsc;
    880 	struct ifnet		*ifp;
    881 
    882 	if (sc->cue_dying)
    883 		return;
    884 
    885 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    886 
    887 	ifp = GET_IFP(sc);
    888 
    889 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
    890 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
    891 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
    892 
    893 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
    894 		ifp->if_ierrors++;
    895 }
    896 
    897 Static int
    898 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
    899 {
    900 	int			total_len;
    901 	struct cue_chain	*c;
    902 	usbd_status		err;
    903 
    904 	c = &sc->cue_cdata.cue_tx_chain[idx];
    905 
    906 	/*
    907 	 * Copy the mbuf data into a contiguous buffer, leaving two
    908 	 * bytes at the beginning to hold the frame length.
    909 	 */
    910 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
    911 	c->cue_mbuf = m;
    912 
    913 	total_len = m->m_pkthdr.len + 2;
    914 
    915 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    916 		     device_xname(sc->cue_dev), __func__, total_len));
    917 
    918 	/* The first two bytes are the frame length */
    919 	c->cue_buf[0] = (uint8_t)m->m_pkthdr.len;
    920 	c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
    921 
    922 	/* XXX 10000 */
    923 	usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000,
    924 	    cue_txeof);
    925 
    926 	/* Transmit */
    927 	err = usbd_transfer(c->cue_xfer);
    928 	if (err != USBD_IN_PROGRESS) {
    929 		printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev),
    930 		       usbd_errstr(err));
    931 		/* Stop the interface from process context. */
    932 		usb_add_task(sc->cue_udev, &sc->cue_stop_task,
    933 		    USB_TASKQ_DRIVER);
    934 		return EIO;
    935 	}
    936 
    937 	sc->cue_cdata.cue_tx_cnt++;
    938 
    939 	return 0;
    940 }
    941 
    942 Static void
    943 cue_start(struct ifnet *ifp)
    944 {
    945 	struct cue_softc	*sc = ifp->if_softc;
    946 	struct mbuf		*m_head = NULL;
    947 
    948 	if (sc->cue_dying)
    949 		return;
    950 
    951 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
    952 
    953 	if (ifp->if_flags & IFF_OACTIVE)
    954 		return;
    955 
    956 	IFQ_POLL(&ifp->if_snd, m_head);
    957 	if (m_head == NULL)
    958 		return;
    959 
    960 	if (cue_send(sc, m_head, 0)) {
    961 		ifp->if_flags |= IFF_OACTIVE;
    962 		return;
    963 	}
    964 
    965 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    966 
    967 	/*
    968 	 * If there's a BPF listener, bounce a copy of this frame
    969 	 * to him.
    970 	 */
    971 	bpf_mtap(ifp, m_head);
    972 
    973 	ifp->if_flags |= IFF_OACTIVE;
    974 
    975 	/*
    976 	 * Set a timeout in case the chip goes out to lunch.
    977 	 */
    978 	ifp->if_timer = 5;
    979 }
    980 
    981 Static void
    982 cue_init(void *xsc)
    983 {
    984 	struct cue_softc	*sc = xsc;
    985 	struct ifnet		*ifp = GET_IFP(sc);
    986 	int			i, s, ctl;
    987 	const u_char		*eaddr;
    988 
    989 	if (sc->cue_dying)
    990 		return;
    991 
    992 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
    993 
    994 	if (ifp->if_flags & IFF_RUNNING)
    995 		return;
    996 
    997 	s = splnet();
    998 
    999 	/*
   1000 	 * Cancel pending I/O and free all RX/TX buffers.
   1001 	 */
   1002 #if 1
   1003 	cue_reset(sc);
   1004 #endif
   1005 
   1006 	/* Set advanced operation modes. */
   1007 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1008 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
   1009 
   1010 	eaddr = CLLADDR(ifp->if_sadl);
   1011 	/* Set MAC address */
   1012 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1013 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
   1014 
   1015 	/* Enable RX logic. */
   1016 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
   1017 	if (ifp->if_flags & IFF_PROMISC)
   1018 		ctl |= CUE_ETHCTL_PROMISC;
   1019 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
   1020 
   1021 	/* Load the multicast filter. */
   1022 	cue_setmulti(sc);
   1023 
   1024 	/*
   1025 	 * Set the number of RX and TX buffers that we want
   1026 	 * to reserve inside the ASIC.
   1027 	 */
   1028 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1029 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1030 
   1031 	/* Set advanced operation modes. */
   1032 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1033 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
   1034 
   1035 	/* Program the LED operation. */
   1036 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1037 
   1038 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1039 		if (cue_open_pipes(sc)) {
   1040 			splx(s);
   1041 			return;
   1042 		}
   1043 	}
   1044 	/* Init TX ring. */
   1045 	if (cue_tx_list_init(sc)) {
   1046 		printf("%s: tx list init failed\n", device_xname(sc->cue_dev));
   1047 		splx(s);
   1048 		return;
   1049 	}
   1050 
   1051 	/* Init RX ring. */
   1052 	if (cue_rx_list_init(sc)) {
   1053 		printf("%s: rx list init failed\n", device_xname(sc->cue_dev));
   1054 		splx(s);
   1055 		return;
   1056 	}
   1057 
   1058 
   1059 	ifp->if_flags |= IFF_RUNNING;
   1060 	ifp->if_flags &= ~IFF_OACTIVE;
   1061 
   1062 	splx(s);
   1063 
   1064 	callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc));
   1065 }
   1066 
   1067 Static int
   1068 cue_open_pipes(struct cue_softc	*sc)
   1069 {
   1070 	struct cue_chain	*c;
   1071 	usbd_status		err;
   1072 	int			i;
   1073 
   1074 	/* Open RX and TX pipes. */
   1075 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1076 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1077 	if (err) {
   1078 		printf("%s: open rx pipe failed: %s\n",
   1079 		    device_xname(sc->cue_dev), usbd_errstr(err));
   1080 		return EIO;
   1081 	}
   1082 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1083 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1084 	if (err) {
   1085 		printf("%s: open tx pipe failed: %s\n",
   1086 		    device_xname(sc->cue_dev), usbd_errstr(err));
   1087 		return EIO;
   1088 	}
   1089 
   1090 	/* Start up the receive pipe. */
   1091 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1092 		c = &sc->cue_cdata.cue_rx_chain[i];
   1093 
   1094 		usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
   1095 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
   1096 		usbd_transfer(c->cue_xfer);
   1097 	}
   1098 
   1099 	return 0;
   1100 }
   1101 
   1102 Static int
   1103 cue_ioctl(struct ifnet *ifp, u_long command, void *data)
   1104 {
   1105 	struct cue_softc	*sc = ifp->if_softc;
   1106 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1107 	struct ifreq		*ifr = (struct ifreq *)data;
   1108 	int			s, error = 0;
   1109 
   1110 	if (sc->cue_dying)
   1111 		return EIO;
   1112 
   1113 	s = splnet();
   1114 
   1115 	switch(command) {
   1116 	case SIOCINITIFADDR:
   1117 		ifp->if_flags |= IFF_UP;
   1118 		cue_init(sc);
   1119 
   1120 		switch (ifa->ifa_addr->sa_family) {
   1121 #ifdef INET
   1122 		case AF_INET:
   1123 			arp_ifinit(ifp, ifa);
   1124 			break;
   1125 #endif /* INET */
   1126 		}
   1127 		break;
   1128 
   1129 	case SIOCSIFMTU:
   1130 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1131 			error = EINVAL;
   1132 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
   1133 			error = 0;
   1134 		break;
   1135 
   1136 	case SIOCSIFFLAGS:
   1137 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   1138 			break;
   1139 		if (ifp->if_flags & IFF_UP) {
   1140 			if (ifp->if_flags & IFF_RUNNING &&
   1141 			    ifp->if_flags & IFF_PROMISC &&
   1142 			    !(sc->cue_if_flags & IFF_PROMISC)) {
   1143 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1144 				cue_setmulti(sc);
   1145 			} else if (ifp->if_flags & IFF_RUNNING &&
   1146 			    !(ifp->if_flags & IFF_PROMISC) &&
   1147 			    sc->cue_if_flags & IFF_PROMISC) {
   1148 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1149 				cue_setmulti(sc);
   1150 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1151 				cue_init(sc);
   1152 		} else {
   1153 			if (ifp->if_flags & IFF_RUNNING)
   1154 				cue_stop(sc);
   1155 		}
   1156 		sc->cue_if_flags = ifp->if_flags;
   1157 		error = 0;
   1158 		break;
   1159 	case SIOCADDMULTI:
   1160 	case SIOCDELMULTI:
   1161 		cue_setmulti(sc);
   1162 		error = 0;
   1163 		break;
   1164 	default:
   1165 		error = ether_ioctl(ifp, command, data);
   1166 		break;
   1167 	}
   1168 
   1169 	splx(s);
   1170 
   1171 	return error;
   1172 }
   1173 
   1174 Static void
   1175 cue_watchdog(struct ifnet *ifp)
   1176 {
   1177 	struct cue_softc	*sc = ifp->if_softc;
   1178 	struct cue_chain	*c;
   1179 	usbd_status		stat;
   1180 	int			s;
   1181 
   1182 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
   1183 
   1184 	if (sc->cue_dying)
   1185 		return;
   1186 
   1187 	ifp->if_oerrors++;
   1188 	printf("%s: watchdog timeout\n", device_xname(sc->cue_dev));
   1189 
   1190 	s = splusb();
   1191 	c = &sc->cue_cdata.cue_tx_chain[0];
   1192 	usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
   1193 	cue_txeof(c->cue_xfer, c, stat);
   1194 
   1195 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1196 		cue_start(ifp);
   1197 	splx(s);
   1198 }
   1199 
   1200 /*
   1201  * Stop the adapter and free any mbufs allocated to the
   1202  * RX and TX lists.
   1203  */
   1204 Static void
   1205 cue_stop(struct cue_softc *sc)
   1206 {
   1207 	usbd_status		err;
   1208 	struct ifnet		*ifp;
   1209 	int			i;
   1210 
   1211 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
   1212 
   1213 	ifp = GET_IFP(sc);
   1214 	ifp->if_timer = 0;
   1215 
   1216 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
   1217 	cue_reset(sc);
   1218 	callout_stop(&sc->cue_stat_ch);
   1219 
   1220 	/* Stop transfers. */
   1221 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1222 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1223 		if (err) {
   1224 			printf("%s: abort rx pipe failed: %s\n",
   1225 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1226 		}
   1227 	}
   1228 
   1229 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1230 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1231 		if (err) {
   1232 			printf("%s: abort tx pipe failed: %s\n",
   1233 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1234 		}
   1235 	}
   1236 
   1237 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1238 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1239 		if (err) {
   1240 			printf("%s: abort intr pipe failed: %s\n",
   1241 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1242 		}
   1243 	}
   1244 
   1245 	/* Free RX resources. */
   1246 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1247 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
   1248 			usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
   1249 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
   1250 		}
   1251 	}
   1252 
   1253 	/* Free TX resources. */
   1254 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
   1255 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
   1256 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
   1257 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
   1258 		}
   1259 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
   1260 			usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
   1261 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
   1262 		}
   1263 	}
   1264 
   1265 	/* Stop transfers. */
   1266 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1267 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1268 		if (err) {
   1269 			printf("%s: close rx pipe failed: %s\n",
   1270 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1271 		}
   1272 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1273 	}
   1274 
   1275 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1276 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1277 		if (err) {
   1278 			printf("%s: close tx pipe failed: %s\n",
   1279 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1280 		}
   1281 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1282 	}
   1283 
   1284 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1285 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1286 		if (err) {
   1287 			printf("%s: close intr pipe failed: %s\n",
   1288 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1289 		}
   1290 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1291 	}
   1292 
   1293 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1294 }
   1295