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