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