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