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