Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.79
      1 /*	$NetBSD: if_cue.c,v 1.79 2018/07/29 02:00:48 riastradh 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.79 2018/07/29 02:00:48 riastradh 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 	strlcpy(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 	/*
    575 	 * XXX Halting callout guarantees no more tick tasks.  What
    576 	 * guarantees no more stop tasks?  What guarantees no more
    577 	 * calls to cue_send?  Don't we need to wait for if_detach or
    578 	 * something?  Should we set sc->cue_dying here?  Is device
    579 	 * deactivation guaranteed to have already happened?
    580 	 */
    581 	callout_halt(&sc->cue_stat_ch, NULL);
    582 	usb_rem_task_wait(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
    583 	usb_rem_task_wait(sc->cue_udev, &sc->cue_stop_task, USB_TASKQ_DRIVER);
    584 
    585 	if (!sc->cue_attached) {
    586 		/* Detached before attached finished, so just bail out. */
    587 		return 0;
    588 	}
    589 
    590 	s = splusb();
    591 
    592 	if (ifp->if_flags & IFF_RUNNING)
    593 		cue_stop(sc);
    594 
    595 	rnd_detach_source(&sc->rnd_source);
    596 	ether_ifdetach(ifp);
    597 
    598 	if_detach(ifp);
    599 
    600 #ifdef DIAGNOSTIC
    601 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
    602 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
    603 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
    604 		aprint_debug_dev(self, "detach has active endpoints\n");
    605 #endif
    606 
    607 	sc->cue_attached = 0;
    608 	splx(s);
    609 
    610 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev);
    611 
    612 	return 0;
    613 }
    614 
    615 int
    616 cue_activate(device_t self, enum devact act)
    617 {
    618 	struct cue_softc *sc = device_private(self);
    619 
    620 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    621 
    622 	switch (act) {
    623 	case DVACT_DEACTIVATE:
    624 		/* Deactivate the interface. */
    625 		if_deactivate(&sc->cue_ec.ec_if);
    626 		sc->cue_dying = 1;
    627 		return 0;
    628 	default:
    629 		return EOPNOTSUPP;
    630 	}
    631 }
    632 
    633 /*
    634  * Initialize an RX descriptor and attach an MBUF cluster.
    635  */
    636 Static int
    637 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
    638 {
    639 	struct mbuf		*m_new = NULL;
    640 
    641 	if (m == NULL) {
    642 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    643 		if (m_new == NULL) {
    644 			printf("%s: no memory for rx list "
    645 			    "-- packet dropped!\n", device_xname(sc->cue_dev));
    646 			return ENOBUFS;
    647 		}
    648 
    649 		MCLGET(m_new, M_DONTWAIT);
    650 		if (!(m_new->m_flags & M_EXT)) {
    651 			printf("%s: no memory for rx list "
    652 			    "-- packet dropped!\n", device_xname(sc->cue_dev));
    653 			m_freem(m_new);
    654 			return ENOBUFS;
    655 		}
    656 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    657 	} else {
    658 		m_new = m;
    659 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    660 		m_new->m_data = m_new->m_ext.ext_buf;
    661 	}
    662 
    663 	m_adj(m_new, ETHER_ALIGN);
    664 	c->cue_mbuf = m_new;
    665 
    666 	return 0;
    667 }
    668 
    669 Static int
    670 cue_rx_list_init(struct cue_softc *sc)
    671 {
    672 	struct cue_cdata	*cd;
    673 	struct cue_chain	*c;
    674 	int			i;
    675 
    676 	cd = &sc->cue_cdata;
    677 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
    678 		c = &cd->cue_rx_chain[i];
    679 		c->cue_sc = sc;
    680 		c->cue_idx = i;
    681 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
    682 			return ENOBUFS;
    683 		if (c->cue_xfer == NULL) {
    684 			int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX],
    685 			    CUE_BUFSZ, 0, 0, &c->cue_xfer);
    686 			if (error)
    687 				return error;
    688 			c->cue_buf = usbd_get_buffer(c->cue_xfer);
    689 		}
    690 	}
    691 
    692 	return 0;
    693 }
    694 
    695 Static int
    696 cue_tx_list_init(struct cue_softc *sc)
    697 {
    698 	struct cue_cdata	*cd;
    699 	struct cue_chain	*c;
    700 	int			i;
    701 
    702 	cd = &sc->cue_cdata;
    703 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
    704 		c = &cd->cue_tx_chain[i];
    705 		c->cue_sc = sc;
    706 		c->cue_idx = i;
    707 		c->cue_mbuf = NULL;
    708 		if (c->cue_xfer == NULL) {
    709 			int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX],
    710 			    CUE_BUFSZ, 0, 0, &c->cue_xfer);
    711 			if (error)
    712 				return error;
    713 			c->cue_buf = usbd_get_buffer(c->cue_xfer);
    714 		}
    715 	}
    716 
    717 	return 0;
    718 }
    719 
    720 /*
    721  * A frame has been uploaded: pass the resulting mbuf chain up to
    722  * the higher level protocols.
    723  */
    724 Static void
    725 cue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    726 {
    727 	struct cue_chain	*c = priv;
    728 	struct cue_softc	*sc = c->cue_sc;
    729 	struct ifnet		*ifp = GET_IFP(sc);
    730 	struct mbuf		*m;
    731 	int			total_len = 0;
    732 	uint16_t		len;
    733 	int			s;
    734 
    735 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
    736 		     __func__, status));
    737 
    738 	if (sc->cue_dying)
    739 		return;
    740 
    741 	if (!(ifp->if_flags & IFF_RUNNING))
    742 		return;
    743 
    744 	if (status != USBD_NORMAL_COMPLETION) {
    745 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    746 			return;
    747 		sc->cue_rx_errs++;
    748 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
    749 			printf("%s: %u usb errors on rx: %s\n",
    750 			    device_xname(sc->cue_dev), sc->cue_rx_errs,
    751 			    usbd_errstr(status));
    752 			sc->cue_rx_errs = 0;
    753 		}
    754 		if (status == USBD_STALLED)
    755 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
    756 		goto done;
    757 	}
    758 
    759 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    760 
    761 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
    762 
    763 	m = c->cue_mbuf;
    764 	len = UGETW(mtod(m, uint8_t *));
    765 
    766 	/* No errors; receive the packet. */
    767 	total_len = len;
    768 
    769 	if (len < sizeof(struct ether_header)) {
    770 		ifp->if_ierrors++;
    771 		goto done;
    772 	}
    773 
    774 	m_adj(m, sizeof(uint16_t));
    775 	m->m_pkthdr.len = m->m_len = total_len;
    776 
    777 	m_set_rcvif(m, ifp);
    778 
    779 	s = splnet();
    780 
    781 	/* XXX ugly */
    782 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    783 		ifp->if_ierrors++;
    784 		goto done1;
    785 	}
    786 
    787 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev),
    788 		    __func__, m->m_len));
    789 	if_percpuq_enqueue(ifp->if_percpuq, m);
    790  done1:
    791 	splx(s);
    792 
    793 done:
    794 
    795 	/* Setup new transfer. */
    796 	usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
    797 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
    798 	usbd_transfer(c->cue_xfer);
    799 
    800 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev),
    801 		    __func__));
    802 }
    803 
    804 /*
    805  * A frame was downloaded to the chip. It's safe for us to clean up
    806  * the list buffers.
    807  */
    808 Static void
    809 cue_txeof(struct usbd_xfer *xfer, void *priv,
    810     usbd_status status)
    811 {
    812 	struct cue_chain	*c = priv;
    813 	struct cue_softc	*sc = c->cue_sc;
    814 	struct ifnet		*ifp = GET_IFP(sc);
    815 	int			s;
    816 
    817 	if (sc->cue_dying)
    818 		return;
    819 
    820 	s = splnet();
    821 
    822 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
    823 		    __func__, status));
    824 
    825 	ifp->if_timer = 0;
    826 	ifp->if_flags &= ~IFF_OACTIVE;
    827 
    828 	if (status != USBD_NORMAL_COMPLETION) {
    829 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    830 			splx(s);
    831 			return;
    832 		}
    833 		ifp->if_oerrors++;
    834 		printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev),
    835 		    usbd_errstr(status));
    836 		if (status == USBD_STALLED)
    837 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
    838 		splx(s);
    839 		return;
    840 	}
    841 
    842 	ifp->if_opackets++;
    843 
    844 	m_freem(c->cue_mbuf);
    845 	c->cue_mbuf = NULL;
    846 
    847 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    848 		cue_start(ifp);
    849 
    850 	splx(s);
    851 }
    852 
    853 Static void
    854 cue_tick(void *xsc)
    855 {
    856 	struct cue_softc	*sc = xsc;
    857 
    858 	if (sc == NULL)
    859 		return;
    860 
    861 	if (sc->cue_dying)
    862 		return;
    863 
    864 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    865 
    866 	/* Perform statistics update in process context. */
    867 	usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
    868 }
    869 
    870 Static void
    871 cue_tick_task(void *xsc)
    872 {
    873 	struct cue_softc	*sc = xsc;
    874 	struct ifnet		*ifp;
    875 
    876 	if (sc->cue_dying)
    877 		return;
    878 
    879 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    880 
    881 	ifp = GET_IFP(sc);
    882 
    883 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
    884 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
    885 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
    886 
    887 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
    888 		ifp->if_ierrors++;
    889 }
    890 
    891 Static int
    892 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
    893 {
    894 	int			total_len;
    895 	struct cue_chain	*c;
    896 	usbd_status		err;
    897 
    898 	c = &sc->cue_cdata.cue_tx_chain[idx];
    899 
    900 	/*
    901 	 * Copy the mbuf data into a contiguous buffer, leaving two
    902 	 * bytes at the beginning to hold the frame length.
    903 	 */
    904 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
    905 	c->cue_mbuf = m;
    906 
    907 	total_len = m->m_pkthdr.len + 2;
    908 
    909 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    910 		     device_xname(sc->cue_dev), __func__, total_len));
    911 
    912 	/* The first two bytes are the frame length */
    913 	c->cue_buf[0] = (uint8_t)m->m_pkthdr.len;
    914 	c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
    915 
    916 	/* XXX 10000 */
    917 	usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000,
    918 	    cue_txeof);
    919 
    920 	/* Transmit */
    921 	err = usbd_transfer(c->cue_xfer);
    922 	if (err != USBD_IN_PROGRESS) {
    923 		printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev),
    924 		       usbd_errstr(err));
    925 		/* Stop the interface from process context. */
    926 		usb_add_task(sc->cue_udev, &sc->cue_stop_task,
    927 		    USB_TASKQ_DRIVER);
    928 		return EIO;
    929 	}
    930 
    931 	sc->cue_cdata.cue_tx_cnt++;
    932 
    933 	return 0;
    934 }
    935 
    936 Static void
    937 cue_start(struct ifnet *ifp)
    938 {
    939 	struct cue_softc	*sc = ifp->if_softc;
    940 	struct mbuf		*m_head = NULL;
    941 
    942 	if (sc->cue_dying)
    943 		return;
    944 
    945 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
    946 
    947 	if (ifp->if_flags & IFF_OACTIVE)
    948 		return;
    949 
    950 	IFQ_POLL(&ifp->if_snd, m_head);
    951 	if (m_head == NULL)
    952 		return;
    953 
    954 	if (cue_send(sc, m_head, 0)) {
    955 		ifp->if_flags |= IFF_OACTIVE;
    956 		return;
    957 	}
    958 
    959 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    960 
    961 	/*
    962 	 * If there's a BPF listener, bounce a copy of this frame
    963 	 * to him.
    964 	 */
    965 	bpf_mtap(ifp, m_head, BPF_D_OUT);
    966 
    967 	ifp->if_flags |= IFF_OACTIVE;
    968 
    969 	/*
    970 	 * Set a timeout in case the chip goes out to lunch.
    971 	 */
    972 	ifp->if_timer = 5;
    973 }
    974 
    975 Static void
    976 cue_init(void *xsc)
    977 {
    978 	struct cue_softc	*sc = xsc;
    979 	struct ifnet		*ifp = GET_IFP(sc);
    980 	int			i, s, ctl;
    981 	const u_char		*eaddr;
    982 
    983 	if (sc->cue_dying)
    984 		return;
    985 
    986 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
    987 
    988 	if (ifp->if_flags & IFF_RUNNING)
    989 		return;
    990 
    991 	s = splnet();
    992 
    993 	/*
    994 	 * Cancel pending I/O and free all RX/TX buffers.
    995 	 */
    996 #if 1
    997 	cue_reset(sc);
    998 #endif
    999 
   1000 	/* Set advanced operation modes. */
   1001 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1002 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
   1003 
   1004 	eaddr = CLLADDR(ifp->if_sadl);
   1005 	/* Set MAC address */
   1006 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1007 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
   1008 
   1009 	/* Enable RX logic. */
   1010 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
   1011 	if (ifp->if_flags & IFF_PROMISC)
   1012 		ctl |= CUE_ETHCTL_PROMISC;
   1013 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
   1014 
   1015 	/* Load the multicast filter. */
   1016 	cue_setmulti(sc);
   1017 
   1018 	/*
   1019 	 * Set the number of RX and TX buffers that we want
   1020 	 * to reserve inside the ASIC.
   1021 	 */
   1022 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1023 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1024 
   1025 	/* Set advanced operation modes. */
   1026 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1027 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
   1028 
   1029 	/* Program the LED operation. */
   1030 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1031 
   1032 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1033 		if (cue_open_pipes(sc)) {
   1034 			splx(s);
   1035 			return;
   1036 		}
   1037 	}
   1038 	/* Init TX ring. */
   1039 	if (cue_tx_list_init(sc)) {
   1040 		printf("%s: tx list init failed\n", device_xname(sc->cue_dev));
   1041 		splx(s);
   1042 		return;
   1043 	}
   1044 
   1045 	/* Init RX ring. */
   1046 	if (cue_rx_list_init(sc)) {
   1047 		printf("%s: rx list init failed\n", device_xname(sc->cue_dev));
   1048 		splx(s);
   1049 		return;
   1050 	}
   1051 
   1052 
   1053 	ifp->if_flags |= IFF_RUNNING;
   1054 	ifp->if_flags &= ~IFF_OACTIVE;
   1055 
   1056 	splx(s);
   1057 
   1058 	callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc));
   1059 }
   1060 
   1061 Static int
   1062 cue_open_pipes(struct cue_softc	*sc)
   1063 {
   1064 	struct cue_chain	*c;
   1065 	usbd_status		err;
   1066 	int			i;
   1067 
   1068 	/* Open RX and TX pipes. */
   1069 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1070 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1071 	if (err) {
   1072 		printf("%s: open rx pipe failed: %s\n",
   1073 		    device_xname(sc->cue_dev), usbd_errstr(err));
   1074 		return EIO;
   1075 	}
   1076 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1077 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1078 	if (err) {
   1079 		printf("%s: open tx pipe failed: %s\n",
   1080 		    device_xname(sc->cue_dev), usbd_errstr(err));
   1081 		return EIO;
   1082 	}
   1083 
   1084 	/* Start up the receive pipe. */
   1085 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1086 		c = &sc->cue_cdata.cue_rx_chain[i];
   1087 
   1088 		usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
   1089 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
   1090 		usbd_transfer(c->cue_xfer);
   1091 	}
   1092 
   1093 	return 0;
   1094 }
   1095 
   1096 Static int
   1097 cue_ioctl(struct ifnet *ifp, u_long command, void *data)
   1098 {
   1099 	struct cue_softc	*sc = ifp->if_softc;
   1100 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1101 	struct ifreq		*ifr = (struct ifreq *)data;
   1102 	int			s, error = 0;
   1103 
   1104 	if (sc->cue_dying)
   1105 		return EIO;
   1106 
   1107 	s = splnet();
   1108 
   1109 	switch(command) {
   1110 	case SIOCINITIFADDR:
   1111 		ifp->if_flags |= IFF_UP;
   1112 		cue_init(sc);
   1113 
   1114 		switch (ifa->ifa_addr->sa_family) {
   1115 #ifdef INET
   1116 		case AF_INET:
   1117 			arp_ifinit(ifp, ifa);
   1118 			break;
   1119 #endif /* INET */
   1120 		}
   1121 		break;
   1122 
   1123 	case SIOCSIFMTU:
   1124 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1125 			error = EINVAL;
   1126 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
   1127 			error = 0;
   1128 		break;
   1129 
   1130 	case SIOCSIFFLAGS:
   1131 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   1132 			break;
   1133 		if (ifp->if_flags & IFF_UP) {
   1134 			if (ifp->if_flags & IFF_RUNNING &&
   1135 			    ifp->if_flags & IFF_PROMISC &&
   1136 			    !(sc->cue_if_flags & IFF_PROMISC)) {
   1137 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1138 				cue_setmulti(sc);
   1139 			} else if (ifp->if_flags & IFF_RUNNING &&
   1140 			    !(ifp->if_flags & IFF_PROMISC) &&
   1141 			    sc->cue_if_flags & IFF_PROMISC) {
   1142 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1143 				cue_setmulti(sc);
   1144 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1145 				cue_init(sc);
   1146 		} else {
   1147 			if (ifp->if_flags & IFF_RUNNING)
   1148 				cue_stop(sc);
   1149 		}
   1150 		sc->cue_if_flags = ifp->if_flags;
   1151 		error = 0;
   1152 		break;
   1153 	case SIOCADDMULTI:
   1154 	case SIOCDELMULTI:
   1155 		cue_setmulti(sc);
   1156 		error = 0;
   1157 		break;
   1158 	default:
   1159 		error = ether_ioctl(ifp, command, data);
   1160 		break;
   1161 	}
   1162 
   1163 	splx(s);
   1164 
   1165 	return error;
   1166 }
   1167 
   1168 Static void
   1169 cue_watchdog(struct ifnet *ifp)
   1170 {
   1171 	struct cue_softc	*sc = ifp->if_softc;
   1172 	struct cue_chain	*c;
   1173 	usbd_status		stat;
   1174 	int			s;
   1175 
   1176 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
   1177 
   1178 	if (sc->cue_dying)
   1179 		return;
   1180 
   1181 	ifp->if_oerrors++;
   1182 	printf("%s: watchdog timeout\n", device_xname(sc->cue_dev));
   1183 
   1184 	s = splusb();
   1185 	c = &sc->cue_cdata.cue_tx_chain[0];
   1186 	usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
   1187 	cue_txeof(c->cue_xfer, c, stat);
   1188 
   1189 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1190 		cue_start(ifp);
   1191 	splx(s);
   1192 }
   1193 
   1194 /*
   1195  * Stop the adapter and free any mbufs allocated to the
   1196  * RX and TX lists.
   1197  */
   1198 Static void
   1199 cue_stop(struct cue_softc *sc)
   1200 {
   1201 	usbd_status		err;
   1202 	struct ifnet		*ifp;
   1203 	int			i;
   1204 
   1205 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
   1206 
   1207 	ifp = GET_IFP(sc);
   1208 	ifp->if_timer = 0;
   1209 
   1210 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
   1211 	cue_reset(sc);
   1212 	callout_stop(&sc->cue_stat_ch);
   1213 
   1214 	/* Stop transfers. */
   1215 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1216 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1217 		if (err) {
   1218 			printf("%s: abort rx pipe failed: %s\n",
   1219 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1220 		}
   1221 	}
   1222 
   1223 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1224 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1225 		if (err) {
   1226 			printf("%s: abort tx pipe failed: %s\n",
   1227 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1228 		}
   1229 	}
   1230 
   1231 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1232 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1233 		if (err) {
   1234 			printf("%s: abort intr pipe failed: %s\n",
   1235 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1236 		}
   1237 	}
   1238 
   1239 	/* Free RX resources. */
   1240 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1241 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
   1242 			usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
   1243 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
   1244 		}
   1245 	}
   1246 
   1247 	/* Free TX resources. */
   1248 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
   1249 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
   1250 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
   1251 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
   1252 		}
   1253 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
   1254 			usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
   1255 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
   1256 		}
   1257 	}
   1258 
   1259 	/* Stop transfers. */
   1260 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1261 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1262 		if (err) {
   1263 			printf("%s: close rx pipe failed: %s\n",
   1264 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1265 		}
   1266 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1267 	}
   1268 
   1269 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1270 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1271 		if (err) {
   1272 			printf("%s: close tx pipe failed: %s\n",
   1273 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1274 		}
   1275 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1276 	}
   1277 
   1278 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1279 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1280 		if (err) {
   1281 			printf("%s: close intr pipe failed: %s\n",
   1282 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1283 		}
   1284 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1285 	}
   1286 
   1287 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1288 }
   1289