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