Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.48.10.4
      1 /*	$NetBSD: if_cue.c,v 1.48.10.4 2007/06/18 13:41:26 itohy 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.48.10.4 2007/06/18 13:41:26 itohy Exp $");
     60 
     61 #if defined(__NetBSD__)
     62 #include "opt_inet.h"
     63 #include "bpfilter.h"
     64 #include "rnd.h"
     65 #elif defined(__OpenBSD__)
     66 #include "bpfilter.h"
     67 #endif /* defined(__OpenBSD__) */
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #if !defined(__OpenBSD__)
     72 #include <sys/callout.h>
     73 #endif
     74 #include <sys/sockio.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/malloc.h>
     77 #include <sys/kernel.h>
     78 #include <sys/socket.h>
     79 
     80 #include <sys/device.h>
     81 #if NRND > 0
     82 #include <sys/rnd.h>
     83 #endif
     84 
     85 #include <net/if.h>
     86 #if defined(__NetBSD__)
     87 #include <net/if_arp.h>
     88 #endif
     89 #include <net/if_dl.h>
     90 
     91 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
     92 
     93 #if NBPFILTER > 0
     94 #include <net/bpf.h>
     95 #endif
     96 
     97 #if defined(__NetBSD__)
     98 #include <net/if_ether.h>
     99 #ifdef INET
    100 #include <netinet/in.h>
    101 #include <netinet/if_inarp.h>
    102 #endif
    103 #endif /* defined(__NetBSD__) */
    104 
    105 #if defined(__OpenBSD__)
    106 #ifdef INET
    107 #include <netinet/in.h>
    108 #include <netinet/in_systm.h>
    109 #include <netinet/in_var.h>
    110 #include <netinet/ip.h>
    111 #include <netinet/if_ether.h>
    112 #endif
    113 #endif /* defined(__OpenBSD__) */
    114 
    115 
    116 #include <dev/usb/usb.h>
    117 #include <dev/usb/usbdi.h>
    118 #include <dev/usb/usbdi_util.h>
    119 #include <dev/usb/usbdevs.h>
    120 #include <dev/usb/usb_ethersubr.h>
    121 
    122 #include <dev/usb/if_cuereg.h>
    123 
    124 #ifdef CUE_DEBUG
    125 #define DPRINTF(x)	if (cuedebug) logprintf x
    126 #define DPRINTFN(n,x)	if (cuedebug >= (n)) logprintf x
    127 int	cuedebug = 0;
    128 #else
    129 #define DPRINTF(x)
    130 #define DPRINTFN(n,x)
    131 #endif
    132 
    133 /*
    134  * Various supported device vendors/products.
    135  */
    136 Static struct usb_devno cue_devs[] = {
    137 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
    138 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
    139 	{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
    140 	/* Belkin F5U111 adapter covered by NETMATE entry */
    141 };
    142 #define cue_lookup(v, p) (usb_lookup(cue_devs, v, p))
    143 
    144 USB_DECLARE_DRIVER(cue);
    145 
    146 Static int cue_open_pipes(struct cue_softc *);
    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, usb_ioctlarg_t);
    154 Static int cue_init(struct ifnet *);
    155 Static void cue_stop(struct ifnet *, int);
    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 			 USBDEVNAME(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 		     USBDEVNAME(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 		     USBDEVNAME(sc->cue_dev), reg, UGETW(val)));
    227 
    228 	if (err) {
    229 		DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
    230 			 USBDEVNAME(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 		     USBDEVNAME(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 			 USBDEVNAME(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 		     USBDEVNAME(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 		     USBDEVNAME(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 			 USBDEVNAME(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 		     USBDEVNAME(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 			 USBDEVNAME(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", USBDEVNAME(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",USBDEVNAME(sc->cue_dev));
    350 		return (-1);
    351 	}
    352 
    353 	return (0);
    354 }
    355 
    356 #define CUE_POLY	0xEDB88320
    357 #define CUE_BITS	9
    358 
    359 Static u_int32_t
    360 cue_crc(const char *addr)
    361 {
    362 	u_int32_t		idx, bit, data, crc;
    363 
    364 	/* Compute CRC for the address value. */
    365 	crc = 0xFFFFFFFF; /* initial value */
    366 
    367 	for (idx = 0; idx < 6; idx++) {
    368 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    369 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
    370 	}
    371 
    372 	return (crc & ((1 << CUE_BITS) - 1));
    373 }
    374 
    375 Static void
    376 cue_setmulti(struct cue_softc *sc)
    377 {
    378 	struct ifnet		*ifp;
    379 	struct ether_multi	*enm;
    380 	struct ether_multistep	step;
    381 	u_int32_t		h, i;
    382 
    383 	ifp = GET_IFP(sc);
    384 
    385 	DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
    386 		    USBDEVNAME(sc->cue_dev), ifp->if_flags));
    387 
    388 	if (ifp->if_flags & IFF_PROMISC) {
    389 allmulti:
    390 		ifp->if_flags |= IFF_ALLMULTI;
    391 		for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
    392 			sc->cue_mctab[i] = 0xFF;
    393 		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
    394 		    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
    395 		return;
    396 	}
    397 
    398 	/* first, zot all the existing hash bits */
    399 	for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
    400 		sc->cue_mctab[i] = 0;
    401 
    402 	/* now program new ones */
    403 #if defined(__NetBSD__)
    404 	ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
    405 #else
    406 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    407 #endif
    408 	while (enm != NULL) {
    409 		if (memcmp(enm->enm_addrlo,
    410 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
    411 			goto allmulti;
    412 
    413 		h = cue_crc(enm->enm_addrlo);
    414 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
    415 		ETHER_NEXT_MULTI(step, enm);
    416 	}
    417 
    418 	ifp->if_flags &= ~IFF_ALLMULTI;
    419 
    420 	/*
    421 	 * Also include the broadcast address in the filter
    422 	 * so we can receive broadcast frames.
    423 	 */
    424 	if (ifp->if_flags & IFF_BROADCAST) {
    425 		h = cue_crc(etherbroadcastaddr);
    426 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
    427 	}
    428 
    429 	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
    430 	    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
    431 }
    432 
    433 Static void
    434 cue_reset(struct cue_softc *sc)
    435 {
    436 	usb_device_request_t	req;
    437 	usbd_status		err;
    438 
    439 	DPRINTFN(2,("%s: cue_reset\n", USBDEVNAME(sc->cue_dev)));
    440 
    441 	if (sc->cue_dying)
    442 		return;
    443 
    444 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    445 	req.bRequest = CUE_CMD_RESET;
    446 	USETW(req.wValue, 0);
    447 	USETW(req.wIndex, 0);
    448 	USETW(req.wLength, 0);
    449 
    450 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    451 
    452 	if (err)
    453 		printf("%s: reset failed\n", USBDEVNAME(sc->cue_dev));
    454 
    455 	/* Wait a little while for the chip to get its brains in order. */
    456 	usbd_delay_ms(sc->cue_udev, 1);
    457 }
    458 
    459 /*
    460  * Probe for a CATC chip.
    461  */
    462 USB_MATCH(cue)
    463 {
    464 	USB_MATCH_START(cue, uaa);
    465 
    466 #ifndef USB_USE_IFATTACH
    467 	if (uaa->iface != NULL)
    468 		return (UMATCH_NONE);
    469 #endif /* USB_USE_IFATTACH */
    470 
    471 	return (cue_lookup(uaa->vendor, uaa->product) != NULL ?
    472 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    473 }
    474 
    475 /*
    476  * Attach the interface. Allocate softc structures, do ifmedia
    477  * setup and ethernet/BPF attach.
    478  */
    479 USB_ATTACH(cue)
    480 {
    481 	USB_ATTACH_START(cue, sc, uaa);
    482 	char			*devinfop;
    483 	int			s;
    484 	u_char			eaddr[ETHER_ADDR_LEN];
    485 	usbd_device_handle	dev = uaa->device;
    486 	usbd_interface_handle	iface;
    487 	usbd_status		err;
    488 	struct ifnet		*ifp;
    489 	usb_interface_descriptor_t	*id;
    490 	usb_endpoint_descriptor_t	*ed;
    491 	int			i;
    492 
    493 	DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
    494 
    495 	devinfop = usbd_devinfo_alloc(dev, 0);
    496 	USB_ATTACH_SETUP;
    497 	printf("%s: %s\n", USBDEVNAME(sc->cue_dev), devinfop);
    498 	usbd_devinfo_free(devinfop);
    499 
    500 	err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
    501 	if (err) {
    502 		printf("%s: setting config no failed\n",
    503 		    USBDEVNAME(sc->cue_dev));
    504 		USB_ATTACH_ERROR_RETURN;
    505 	}
    506 
    507 	sc->cue_udev = dev;
    508 	sc->cue_product = uaa->product;
    509 	sc->cue_vendor = uaa->vendor;
    510 
    511 	usb_init_task(&sc->cue_tick_task, cue_tick_task, sc);
    512 	usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc);
    513 
    514 	err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
    515 	if (err) {
    516 		printf("%s: getting interface handle failed\n",
    517 		    USBDEVNAME(sc->cue_dev));
    518 		USB_ATTACH_ERROR_RETURN;
    519 	}
    520 
    521 	sc->cue_iface = iface;
    522 	id = usbd_get_interface_descriptor(iface);
    523 
    524 	/* Find endpoints. */
    525 	for (i = 0; i < id->bNumEndpoints; i++) {
    526 		ed = usbd_interface2endpoint_descriptor(iface, i);
    527 		if (ed == NULL) {
    528 			printf("%s: couldn't get ep %d\n",
    529 			    USBDEVNAME(sc->cue_dev), i);
    530 			USB_ATTACH_ERROR_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 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->cue_dev),
    559 	    ether_sprintf(eaddr));
    560 
    561 	/* Initialize interface info.*/
    562 	ifp = GET_IFP(sc);
    563 	ifp->if_softc = sc;
    564 	ifp->if_mtu = ETHERMTU;
    565 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    566 	ifp->if_ioctl = cue_ioctl;
    567 	ifp->if_start = cue_start;
    568 	ifp->if_init = cue_init;
    569 	ifp->if_stop = cue_stop;
    570 	ifp->if_watchdog = cue_watchdog;
    571 #if defined(__OpenBSD__)
    572 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    573 #endif
    574 	strncpy(ifp->if_xname, USBDEVNAME(sc->cue_dev), IFNAMSIZ);
    575 
    576 	IFQ_SET_READY(&ifp->if_snd);
    577 
    578 	/* Attach the interface. */
    579 	if_attach(ifp);
    580 	Ether_ifattach(ifp, eaddr);
    581 #if NRND > 0
    582 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev),
    583 	    RND_TYPE_NET, 0);
    584 #endif
    585 
    586 	usb_callout_init(sc->cue_stat_ch);
    587 
    588 	sc->cue_attached = 1;
    589 	splx(s);
    590 
    591 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
    592 	    USBDEV(sc->cue_dev));
    593 
    594 	USB_ATTACH_SUCCESS_RETURN;
    595 }
    596 
    597 USB_DETACH(cue)
    598 {
    599 	USB_DETACH_START(cue, sc);
    600 	struct ifnet		*ifp = GET_IFP(sc);
    601 	int			s;
    602 
    603 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
    604 
    605 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
    606 	/*
    607 	 * Remove any pending task.  It cannot be executing because it run
    608 	 * in the same thread as detach.
    609 	 */
    610 	usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
    611 	usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
    612 
    613 	if (!sc->cue_attached) {
    614 		/* Detached before attached finished, so just bail out. */
    615 		return (0);
    616 	}
    617 
    618 	s = splusb();
    619 
    620 	if (ifp->if_flags & IFF_RUNNING)
    621 		cue_stop(ifp, 1);
    622 
    623 #if defined(__NetBSD__)
    624 #if NRND > 0
    625 	rnd_detach_source(&sc->rnd_source);
    626 #endif
    627 	ether_ifdetach(ifp);
    628 #endif /* __NetBSD__ */
    629 
    630 	if_detach(ifp);
    631 
    632 #ifdef DIAGNOSTIC
    633 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
    634 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
    635 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
    636 		printf("%s: detach has active endpoints\n",
    637 		       USBDEVNAME(sc->cue_dev));
    638 #endif
    639 
    640 	sc->cue_attached = 0;
    641 	splx(s);
    642 
    643 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
    644 	    USBDEV(sc->cue_dev));
    645 
    646 	return (0);
    647 }
    648 
    649 int
    650 cue_activate(device_ptr_t self, enum devact act)
    651 {
    652 	struct cue_softc *sc = (struct cue_softc *)self;
    653 
    654 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
    655 
    656 	switch (act) {
    657 	case DVACT_ACTIVATE:
    658 		return (EOPNOTSUPP);
    659 		break;
    660 
    661 	case DVACT_DEACTIVATE:
    662 		/* Deactivate the interface. */
    663 		if_deactivate(&sc->cue_ec.ec_if);
    664 		sc->cue_dying = 1;
    665 		break;
    666 	}
    667 	return (0);
    668 }
    669 
    670 /*
    671  * A frame has been uploaded: pass the resulting mbuf chain up to
    672  * the higher level protocols.
    673  */
    674 Static void
    675 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    676 {
    677 	struct ue_chain		*c = priv;
    678 	struct cue_softc	*sc = (void *)c->ue_dev;
    679 	struct ifnet		*ifp = GET_IFP(sc);
    680 	struct mbuf		*m;
    681 	int			total_len = 0;
    682 	u_int16_t		len;
    683 	int			s;
    684 
    685 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
    686 		     __func__, status));
    687 
    688 	if (sc->cue_dying)
    689 		return;
    690 
    691 	if (!(ifp->if_flags & IFF_RUNNING))
    692 		return;
    693 
    694 	usbd_unmap_buffer(xfer);
    695 
    696 	if (status != USBD_NORMAL_COMPLETION) {
    697 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    698 			return;
    699 		sc->cue_rx_errs++;
    700 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
    701 			printf("%s: %u usb errors on rx: %s\n",
    702 			    USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
    703 			    usbd_errstr(status));
    704 			sc->cue_rx_errs = 0;
    705 		}
    706 		if (status == USBD_STALLED)
    707 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
    708 		goto done;
    709 	}
    710 
    711 #if 0
    712 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    713 	/* XXX should check total_len ? */
    714 #endif
    715 
    716 	m = c->ue_mbuf;
    717 	len = UGETW(mtod(m, u_int8_t *));
    718 
    719 	/* No errors; receive the packet. */
    720 	total_len = len;
    721 
    722 	if (len < sizeof(struct ether_header)) {
    723 		ifp->if_ierrors++;
    724 		goto done;
    725 	}
    726 
    727 	/*
    728 	 * Allocate new mbuf cluster for the next transfer.
    729 	 * If that failed, discard current packet and recycle the mbuf.
    730 	 */
    731 	if ((c->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
    732 		printf("%s: no memory for rx list -- packet dropped!\n",
    733 		    USBDEVNAME(sc->cue_dev));
    734 		ifp->if_ierrors++;
    735 		c->ue_mbuf = usb_ether_newbuf(m);
    736 		goto done;
    737 	}
    738 
    739 	ifp->if_ipackets++;
    740 	m_adj(m, sizeof(u_int16_t));
    741 	m->m_pkthdr.len = m->m_len = total_len;
    742 
    743 	m->m_pkthdr.rcvif = ifp;
    744 
    745 	s = splnet();
    746 
    747 #if NBPFILTER > 0
    748 	/*
    749 	 * Handle BPF listeners. Let the BPF user see the packet, but
    750 	 * don't pass it up to the ether_input() layer unless it's
    751 	 * a broadcast packet, multicast packet, matches our ethernet
    752 	 * address or the interface is in promiscuous mode.
    753 	 */
    754 	if (ifp->if_bpf)
    755 		BPF_MTAP(ifp, m);
    756 #endif
    757 
    758 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
    759 		    __func__, m->m_len));
    760 	IF_INPUT(ifp, m);
    761 	splx(s);
    762 
    763 done:
    764 	/* Setup new transfer. */
    765 	(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
    766 	usbd_setup_xfer(c->ue_xfer, sc->cue_ep[CUE_ENDPT_RX],
    767 	    c, NULL /* XXX buf */, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    768 	    USBD_NO_TIMEOUT, cue_rxeof);
    769 	usbd_transfer(c->ue_xfer);
    770 
    771 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
    772 		    __func__));
    773 }
    774 
    775 /*
    776  * A frame was downloaded to the chip. It's safe for us to clean up
    777  * the list buffers.
    778  */
    779 Static void
    780 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    781     usbd_status status)
    782 {
    783 	struct ue_chain		*c = priv;
    784 	struct cue_softc	*sc = (void *)c->ue_dev;
    785 	struct ifnet		*ifp = GET_IFP(sc);
    786 	int			s;
    787 
    788 	if (sc->cue_dying)
    789 		return;
    790 
    791 	usbd_unmap_buffer(xfer);
    792 
    793 	s = splnet();
    794 
    795 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
    796 		    __func__, status));
    797 
    798 	ifp->if_timer = 0;
    799 	ifp->if_flags &= ~IFF_OACTIVE;
    800 
    801 	if (status != USBD_NORMAL_COMPLETION) {
    802 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    803 			splx(s);
    804 			return;
    805 		}
    806 		ifp->if_oerrors++;
    807 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
    808 		    usbd_errstr(status));
    809 		if (status == USBD_STALLED)
    810 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
    811 		splx(s);
    812 		return;
    813 	}
    814 
    815 	ifp->if_opackets++;
    816 
    817 	m_freem(c->ue_mbuf);
    818 	c->ue_mbuf = NULL;
    819 
    820 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    821 		cue_start(ifp);
    822 
    823 	splx(s);
    824 }
    825 
    826 Static void
    827 cue_tick(void *xsc)
    828 {
    829 	struct cue_softc	*sc = xsc;
    830 
    831 	if (sc == NULL)
    832 		return;
    833 
    834 	if (sc->cue_dying)
    835 		return;
    836 
    837 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
    838 
    839 	/* Perform statistics update in process context. */
    840 	usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
    841 }
    842 
    843 Static void
    844 cue_tick_task(void *xsc)
    845 {
    846 	struct cue_softc	*sc = xsc;
    847 	struct ifnet		*ifp;
    848 
    849 	if (sc->cue_dying)
    850 		return;
    851 
    852 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
    853 
    854 	ifp = GET_IFP(sc);
    855 
    856 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
    857 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
    858 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
    859 
    860 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
    861 		ifp->if_ierrors++;
    862 }
    863 
    864 Static int
    865 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
    866 {
    867 	int			total_len;
    868 	struct ue_chain		*c;
    869 	usbd_status		err;
    870 	int			ret;
    871 
    872 	c = &sc->cue_cdata.cue_tx_chain[idx];
    873 
    874 	/* Prepend two bytes at the beginning to hold the frame length. */
    875 	M_PREPEND(m, sizeof(u_int16_t), M_DONTWAIT);
    876 	if (m != NULL)
    877 		m = m_pullup(m, sizeof(u_int16_t));	/* just in case */
    878 	if (m == NULL) {
    879 		GET_IFP(sc)->if_oerrors++;
    880 		return (ENOBUFS);
    881 	}
    882 
    883 	total_len = m->m_pkthdr.len;
    884 
    885 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    886 		     USBDEVNAME(sc->cue_dev), __func__, total_len));
    887 
    888 	/* The first two bytes are the frame length */
    889 	USETW(mtod(m, char *), m->m_pkthdr.len - sizeof(u_int16_t));
    890 
    891 	ret = usb_ether_map_tx_buffer_mbuf(c, m);
    892 	if (ret) {
    893 		m_freem(m);
    894 		return (ret);
    895 	}
    896 
    897 	/* XXX 10000 */
    898 	usbd_setup_xfer(c->ue_xfer, sc->cue_ep[CUE_ENDPT_TX],
    899 	    c, NULL /* XXX buf */, total_len, USBD_NO_COPY, 10000, cue_txeof);
    900 
    901 	/* Transmit */
    902 	err = usbd_transfer(c->ue_xfer);
    903 	if (err != USBD_IN_PROGRESS) {
    904 		c->ue_mbuf = NULL;
    905 		m_freem(m);
    906 		printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
    907 		       usbd_errstr(err));
    908 		/* Stop the interface from process context. */
    909 		usb_add_task(sc->cue_udev, &sc->cue_stop_task,
    910 		    USB_TASKQ_DRIVER);
    911 		return (EIO);
    912 	}
    913 
    914 	sc->cue_cdata.cue_tx_cnt++;
    915 
    916 	return (0);
    917 }
    918 
    919 Static void
    920 cue_start(struct ifnet *ifp)
    921 {
    922 	struct cue_softc	*sc = ifp->if_softc;
    923 	struct mbuf		*m_head = NULL;
    924 
    925 	if (sc->cue_dying)
    926 		return;
    927 
    928 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
    929 
    930 	if (ifp->if_flags & IFF_OACTIVE)
    931 		return;
    932 
    933 	IFQ_POLL(&ifp->if_snd, m_head);
    934 	if (m_head == NULL)
    935 		return;
    936 
    937 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    938 
    939 #if NBPFILTER > 0
    940 	/*
    941 	 * If there's a BPF listener, bounce a copy of this frame
    942 	 * to him.
    943 	 */
    944 	if (ifp->if_bpf)
    945 		BPF_MTAP(ifp, m_head);
    946 #endif
    947 
    948 	if (cue_send(sc, m_head, 0)) {
    949 		ifp->if_flags |= IFF_OACTIVE;
    950 		return;
    951 	}
    952 
    953 	ifp->if_flags |= IFF_OACTIVE;
    954 
    955 	/*
    956 	 * Set a timeout in case the chip goes out to lunch.
    957 	 */
    958 	ifp->if_timer = 5;
    959 }
    960 
    961 Static int
    962 cue_init(struct ifnet *ifp)
    963 {
    964 	struct cue_softc	*sc = ifp->if_softc;
    965 	int			i, s, ctl;
    966 	u_char			*eaddr;
    967 	struct ue_chain		*c;
    968 
    969 	if (sc->cue_dying)
    970 		return (EIO);
    971 
    972 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
    973 
    974 	if (ifp->if_flags & IFF_RUNNING)
    975 		return (EIO);
    976 
    977 	s = splnet();
    978 
    979 	/*
    980 	 * Cancel pending I/O and free all RX/TX buffers.
    981 	 */
    982 #if 1
    983 	cue_reset(sc);
    984 #endif
    985 
    986 	/* Set advanced operation modes. */
    987 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
    988 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
    989 
    990 #if defined(__OpenBSD__)
    991 	eaddr = sc->arpcom.ac_enaddr;
    992 #elif defined(__NetBSD__)
    993 	eaddr = LLADDR(ifp->if_sadl);
    994 #endif
    995 	/* Set MAC address */
    996 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    997 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
    998 
    999 	/* Enable RX logic. */
   1000 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
   1001 	if (ifp->if_flags & IFF_PROMISC)
   1002 		ctl |= CUE_ETHCTL_PROMISC;
   1003 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
   1004 
   1005 	/* Load the multicast filter. */
   1006 	cue_setmulti(sc);
   1007 
   1008 	/*
   1009 	 * Set the number of RX and TX buffers that we want
   1010 	 * to reserve inside the ASIC.
   1011 	 */
   1012 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1013 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1014 
   1015 	/* Set advanced operation modes. */
   1016 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1017 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
   1018 
   1019 	/* Program the LED operation. */
   1020 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1021 
   1022 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1023 		if (cue_open_pipes(sc)) {
   1024 			splx(s);
   1025 			return (EIO);
   1026 		}
   1027 	}
   1028 
   1029 	/* Init TX ring. */
   1030 	if ((i = usb_ether_tx_list_init(USBDEV(sc->cue_dev),
   1031 	    sc->cue_cdata.cue_tx_chain, CUE_TX_LIST_CNT,
   1032 	    sc->cue_udev, sc->cue_ep[CUE_ENDPT_TX], NULL))) {
   1033 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
   1034 		splx(s);
   1035 		return (i);
   1036 	}
   1037 
   1038 	/* Init RX ring. */
   1039 	if ((i = usb_ether_rx_list_init(USBDEV(sc->cue_dev),
   1040 	    sc->cue_cdata.cue_rx_chain, CUE_RX_LIST_CNT,
   1041 	    sc->cue_udev, sc->cue_ep[CUE_ENDPT_RX]))) {
   1042 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
   1043 		splx(s);
   1044 		return (i);
   1045 	}
   1046 
   1047 	/* Start up the receive pipe. */
   1048 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1049 		c = &sc->cue_cdata.cue_rx_chain[i];
   1050 		(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
   1051 		usbd_setup_xfer(c->ue_xfer, sc->cue_ep[CUE_ENDPT_RX],
   1052 		    c, NULL /* XXX buf */, CUE_BUFSZ,
   1053 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1054 		    cue_rxeof);
   1055 		usbd_transfer(c->ue_xfer);
   1056 	}
   1057 
   1058 	ifp->if_flags |= IFF_RUNNING;
   1059 	ifp->if_flags &= ~IFF_OACTIVE;
   1060 
   1061 	splx(s);
   1062 
   1063 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
   1064 
   1065 	return (0);
   1066 }
   1067 
   1068 Static int
   1069 cue_open_pipes(struct cue_softc	*sc)
   1070 {
   1071 	usbd_status		err;
   1072 
   1073 	/* Open RX and TX pipes. */
   1074 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1075 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1076 	if (err) {
   1077 		printf("%s: open rx pipe failed: %s\n",
   1078 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1079 		return (EIO);
   1080 	}
   1081 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1082 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1083 	if (err) {
   1084 		printf("%s: open tx pipe failed: %s\n",
   1085 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1086 		return (EIO);
   1087 	}
   1088 
   1089 	return (0);
   1090 }
   1091 
   1092 Static int
   1093 cue_ioctl(struct ifnet *ifp, u_long command, usb_ioctlarg_t data)
   1094 {
   1095 	struct cue_softc	*sc = ifp->if_softc;
   1096 #if 0
   1097 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1098 	struct ifreq		*ifr = (struct ifreq *)data;
   1099 #endif
   1100 	int			s, error = 0;
   1101 
   1102 	if (sc->cue_dying)
   1103 		return (EIO);
   1104 
   1105 	s = splnet();
   1106 
   1107 	switch(command) {
   1108 #if 0
   1109 	case SIOCSIFADDR:
   1110 		ifp->if_flags |= IFF_UP;
   1111 		cue_init(ifp);
   1112 
   1113 		switch (ifa->ifa_addr->sa_family) {
   1114 #ifdef INET
   1115 		case AF_INET:
   1116 #if defined(__NetBSD__)
   1117 			arp_ifinit(ifp, ifa);
   1118 #else
   1119 			arp_ifinit(&sc->arpcom, ifa);
   1120 #endif
   1121 			break;
   1122 #endif /* INET */
   1123 		}
   1124 		break;
   1125 
   1126 	case SIOCSIFMTU:
   1127 		if (ifr->ifr_mtu > ETHERMTU)
   1128 			error = EINVAL;
   1129 		else
   1130 			ifp->if_mtu = ifr->ifr_mtu;
   1131 		break;
   1132 #endif
   1133 
   1134 	case SIOCSIFFLAGS:
   1135 		if (ifp->if_flags & IFF_UP) {
   1136 			if (ifp->if_flags & IFF_RUNNING &&
   1137 			    ifp->if_flags & IFF_PROMISC &&
   1138 			    !(sc->cue_if_flags & IFF_PROMISC)) {
   1139 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1140 				cue_setmulti(sc);
   1141 			} else if (ifp->if_flags & IFF_RUNNING &&
   1142 			    !(ifp->if_flags & IFF_PROMISC) &&
   1143 			    sc->cue_if_flags & IFF_PROMISC) {
   1144 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1145 				cue_setmulti(sc);
   1146 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1147 				cue_init(ifp);
   1148 		} else {
   1149 			if (ifp->if_flags & IFF_RUNNING)
   1150 				cue_stop(ifp, 0);
   1151 		}
   1152 		sc->cue_if_flags = ifp->if_flags;
   1153 		error = 0;
   1154 		break;
   1155 #if 0
   1156 	case SIOCADDMULTI:
   1157 	case SIOCDELMULTI:
   1158 		cue_setmulti(sc);
   1159 		error = 0;
   1160 		break;
   1161 #endif
   1162 	default:
   1163 		error = ether_ioctl(ifp, command, data);
   1164 		if (error == ENETRESET) {
   1165 			/*
   1166 			 * Multicast list has changed; set the hardware
   1167 			 * filter accordingly.
   1168 			 */
   1169 			if (ifp->if_flags & IFF_RUNNING)
   1170 				cue_setmulti(sc);
   1171 			error = 0;
   1172 		}
   1173 		break;
   1174 	}
   1175 
   1176 	splx(s);
   1177 
   1178 	return (error);
   1179 }
   1180 
   1181 Static void
   1182 cue_watchdog(struct ifnet *ifp)
   1183 {
   1184 	struct cue_softc	*sc = ifp->if_softc;
   1185 	struct ue_chain		*c;
   1186 	usbd_status		stat;
   1187 	int			s;
   1188 
   1189 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
   1190 
   1191 	if (sc->cue_dying)
   1192 		return;
   1193 
   1194 	ifp->if_oerrors++;
   1195 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
   1196 
   1197 	s = splusb();
   1198 	c = &sc->cue_cdata.cue_tx_chain[0];
   1199 	usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
   1200 	cue_txeof(c->ue_xfer, c, stat);
   1201 
   1202 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1203 		cue_start(ifp);
   1204 	splx(s);
   1205 }
   1206 
   1207 /*
   1208  * Stop the adapter and free any mbufs allocated to the
   1209  * RX and TX lists.
   1210  */
   1211 Static void
   1212 cue_stop(struct ifnet *ifp, int disable)
   1213 {
   1214 	struct cue_softc	*sc = ifp->if_softc;
   1215 	usbd_status		err;
   1216 
   1217 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
   1218 
   1219 	ifp = GET_IFP(sc);
   1220 	ifp->if_timer = 0;
   1221 
   1222 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
   1223 	cue_reset(sc);
   1224 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
   1225 
   1226 	/* Stop transfers. */
   1227 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1228 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1229 		if (err) {
   1230 			printf("%s: abort rx pipe failed: %s\n",
   1231 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1232 		}
   1233 	}
   1234 
   1235 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1236 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1237 		if (err) {
   1238 			printf("%s: abort tx pipe failed: %s\n",
   1239 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1240 		}
   1241 	}
   1242 
   1243 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1244 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1245 		if (err) {
   1246 			printf("%s: abort intr pipe failed: %s\n",
   1247 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1248 		}
   1249 	}
   1250 
   1251 	/* Free RX/TX resources. */
   1252 	usb_ether_rx_list_free(sc->cue_cdata.cue_rx_chain, CUE_RX_LIST_CNT);
   1253 	usb_ether_tx_list_free(sc->cue_cdata.cue_tx_chain, CUE_TX_LIST_CNT);
   1254 
   1255 	/* Close pipes. */
   1256 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1257 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1258 		if (err) {
   1259 			printf("%s: close rx pipe failed: %s\n",
   1260 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1261 		}
   1262 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1263 	}
   1264 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1265 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1266 		if (err) {
   1267 			printf("%s: close tx pipe failed: %s\n",
   1268 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1269 		}
   1270 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1271 	}
   1272 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1273 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1274 		if (err) {
   1275 			printf("%s: close intr pipe failed: %s\n",
   1276 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1277 		}
   1278 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1279 	}
   1280 
   1281 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1282 }
   1283