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