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