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