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