Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.12
      1 /*	$NetBSD: if_cue.c,v 1.12 2000/03/06 21:02:03 thorpej 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_snd.ifq_maxlen = IFQ_MAXLEN;
    610 
    611 	cue_qdat.ifp = ifp;
    612 	cue_qdat.if_rxstart = cue_rxstart;
    613 
    614 	/*
    615 	 * Call MI attach routines.
    616 	 */
    617 	if_attach(ifp);
    618 	ether_ifattach(ifp);
    619 	callout_handle_init(&sc->cue_stat_ch);
    620 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    621 	usb_register_netisr();
    622 
    623 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    624 
    625 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->cue_dev),
    626 	    ether_sprintf(eaddr));
    627 
    628 	/* Initialize interface info.*/
    629 	ifp = GET_IFP(sc);
    630 	ifp->if_softc = sc;
    631 	ifp->if_mtu = ETHERMTU;
    632 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    633 	ifp->if_ioctl = cue_ioctl;
    634 	ifp->if_start = cue_start;
    635 	ifp->if_watchdog = cue_watchdog;
    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 
    653 	sc->cue_attached = 1;
    654 	splx(s);
    655 
    656 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
    657 			   USBDEV(sc->cue_dev));
    658 
    659 	USB_ATTACH_SUCCESS_RETURN;
    660 }
    661 
    662 USB_DETACH(cue)
    663 {
    664 	USB_DETACH_START(cue, sc);
    665 	struct ifnet		*ifp = GET_IFP(sc);
    666 	int			s;
    667 
    668 	s = splusb();
    669 
    670 	usb_untimeout(cue_tick, sc, sc->cue_stat_ch);
    671 
    672 	if (!sc->cue_attached) {
    673 		/* Detached before attached finished, so just bail out. */
    674 		splx(s);
    675 		return (0);
    676 	}
    677 
    678 	if (ifp->if_flags & IFF_RUNNING)
    679 		cue_stop(sc);
    680 
    681 #if defined(__NetBSD__)
    682 #if NRND > 0
    683 	rnd_detach_source(&sc->rnd_source);
    684 #endif
    685 #if NBPFILTER > 0
    686 	bpfdetach(ifp);
    687 #endif
    688 	ether_ifdetach(ifp);
    689 #endif /* __NetBSD__ */
    690 
    691 	if_detach(ifp);
    692 
    693 #ifdef DIAGNOSTIC
    694 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
    695 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
    696 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
    697 		printf("%s: detach has active endpoints\n",
    698 		       USBDEVNAME(sc->cue_dev));
    699 #endif
    700 
    701 	sc->cue_attached = 0;
    702 	splx(s);
    703 
    704 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
    705 			   USBDEV(sc->cue_dev));
    706 
    707 	return (0);
    708 }
    709 
    710 #if defined(__NetBSD__) || defined(__OpenBSD__)
    711 int
    712 cue_activate(self, act)
    713 	device_ptr_t self;
    714 	enum devact act;
    715 {
    716 	struct cue_softc *sc = (struct cue_softc *)self;
    717 
    718 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
    719 
    720 	switch (act) {
    721 	case DVACT_ACTIVATE:
    722 		return (EOPNOTSUPP);
    723 		break;
    724 
    725 	case DVACT_DEACTIVATE:
    726 		/* Deactivate the interface. */
    727 		if_deactivate(&sc->cue_ec.ec_if);
    728 		sc->cue_dying = 1;
    729 		break;
    730 	}
    731 	return (0);
    732 }
    733 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    734 
    735 /*
    736  * Initialize an RX descriptor and attach an MBUF cluster.
    737  */
    738 static int
    739 cue_newbuf(sc, c, m)
    740 	struct cue_softc	*sc;
    741 	struct cue_chain	*c;
    742 	struct mbuf		*m;
    743 {
    744 	struct mbuf		*m_new = NULL;
    745 
    746 	if (m == NULL) {
    747 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    748 		if (m_new == NULL) {
    749 			printf("%s: no memory for rx list "
    750 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
    751 			return (ENOBUFS);
    752 		}
    753 
    754 		MCLGET(m_new, M_DONTWAIT);
    755 		if (!(m_new->m_flags & M_EXT)) {
    756 			printf("%s: no memory for rx list "
    757 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
    758 			m_freem(m_new);
    759 			return (ENOBUFS);
    760 		}
    761 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    762 	} else {
    763 		m_new = m;
    764 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    765 		m_new->m_data = m_new->m_ext.ext_buf;
    766 	}
    767 
    768 	m_adj(m_new, ETHER_ALIGN);
    769 	c->cue_mbuf = m_new;
    770 
    771 	return (0);
    772 }
    773 
    774 static int
    775 cue_rx_list_init(sc)
    776 	struct cue_softc	*sc;
    777 {
    778 	struct cue_cdata	*cd;
    779 	struct cue_chain	*c;
    780 	int			i;
    781 
    782 	cd = &sc->cue_cdata;
    783 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
    784 		c = &cd->cue_rx_chain[i];
    785 		c->cue_sc = sc;
    786 		c->cue_idx = i;
    787 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
    788 			return (ENOBUFS);
    789 		if (c->cue_xfer == NULL) {
    790 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
    791 			if (c->cue_xfer == NULL)
    792 				return (ENOBUFS);
    793 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
    794 			if (c->cue_buf == NULL)
    795 				return (ENOBUFS); /* XXX free xfer */
    796 		}
    797 	}
    798 
    799 	return (0);
    800 }
    801 
    802 static int
    803 cue_tx_list_init(sc)
    804 	struct cue_softc	*sc;
    805 {
    806 	struct cue_cdata	*cd;
    807 	struct cue_chain	*c;
    808 	int			i;
    809 
    810 	cd = &sc->cue_cdata;
    811 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
    812 		c = &cd->cue_tx_chain[i];
    813 		c->cue_sc = sc;
    814 		c->cue_idx = i;
    815 		c->cue_mbuf = NULL;
    816 		if (c->cue_xfer == NULL) {
    817 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
    818 			if (c->cue_xfer == NULL)
    819 				return (ENOBUFS);
    820 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
    821 			if (c->cue_buf == NULL)
    822 				return (ENOBUFS);
    823 		}
    824 	}
    825 
    826 	return (0);
    827 }
    828 
    829 #ifdef __FreeBSD__
    830 static void
    831 cue_rxstart(ifp)
    832 	struct ifnet		*ifp;
    833 {
    834 	struct cue_softc	*sc;
    835 	struct cue_chain	*c;
    836 
    837 	sc = ifp->if_softc;
    838 	c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
    839 
    840 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    841 		ifp->if_ierrors++;
    842 		return;
    843 	}
    844 
    845 	/* Setup new transfer. */
    846 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
    847 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    848 	    USBD_NO_TIMEOUT, cue_rxeof);
    849 	usbd_transfer(c->cue_xfer);
    850 
    851 	return;
    852 }
    853 #endif
    854 
    855 /*
    856  * A frame has been uploaded: pass the resulting mbuf chain up to
    857  * the higher level protocols.
    858  */
    859 static void
    860 cue_rxeof(xfer, priv, status)
    861 	usbd_xfer_handle	xfer;
    862 	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 	struct mbuf		*m;
    869 	int			total_len = 0;
    870 	u_int16_t		len;
    871 #if defined(__NetBSD__) || defined(__OpenBSD__)
    872 	int			s;
    873 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    874 
    875 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
    876 		     __FUNCTION__, status));
    877 
    878 	if (sc->cue_dying)
    879 		return;
    880 
    881 	if (!(ifp->if_flags & IFF_RUNNING))
    882 		return;
    883 
    884 	if (status != USBD_NORMAL_COMPLETION) {
    885 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    886 			return;
    887 		sc->cue_rx_errs++;
    888 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
    889 			printf("%s: %u usb errors on rx: %s\n",
    890 			    USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
    891 			    usbd_errstr(status));
    892 			sc->cue_rx_errs = 0;
    893 		}
    894 		if (status == USBD_STALLED)
    895 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
    896 		goto done;
    897 	}
    898 
    899 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    900 
    901 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
    902 
    903 	m = c->cue_mbuf;
    904 	len = UGETW(mtod(m, u_int8_t *));
    905 
    906 	/* No errors; receive the packet. */
    907 	total_len = len;
    908 
    909 	if (len < sizeof(struct ether_header)) {
    910 		ifp->if_ierrors++;
    911 		goto done;
    912 	}
    913 
    914 	ifp->if_ipackets++;
    915 	m_adj(m, sizeof(u_int16_t));
    916 	m->m_pkthdr.len = m->m_len = total_len;
    917 
    918 #if defined(__FreeBSD__)
    919 	m->m_pkthdr.rcvif = (struct ifnet *)&cue_qdat;
    920 	/* Put the packet on the special USB input queue. */
    921 	usb_ether_input(m);
    922 
    923 	return;
    924 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    925 	m->m_pkthdr.rcvif = ifp;
    926 
    927 	s = splimp();
    928 
    929 	/* XXX ugly */
    930 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    931 		ifp->if_ierrors++;
    932 		goto done1;
    933 	}
    934 
    935 #if NBPFILTER > 0
    936 	/*
    937 	 * Handle BPF listeners. Let the BPF user see the packet, but
    938 	 * don't pass it up to the ether_input() layer unless it's
    939 	 * a broadcast packet, multicast packet, matches our ethernet
    940 	 * address or the interface is in promiscuous mode.
    941 	 */
    942 	if (ifp->if_bpf) {
    943 		struct ether_header *eh = mtod(m, struct ether_header *);
    944 		BPF_MTAP(ifp, m);
    945 		if ((ifp->if_flags & IFF_PROMISC) &&
    946 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    947 			   ETHER_ADDR_LEN) &&
    948 		    !(eh->ether_dhost[0] & 1)) {
    949 			m_freem(m);
    950 			goto done1;
    951 		}
    952 	}
    953 #endif
    954 
    955 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
    956 		    __FUNCTION__, m->m_len));
    957 	(*ifp->if_input)(ifp, m);
    958  done1:
    959 	splx(s);
    960 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    961 
    962 done:
    963 	/* Setup new transfer. */
    964 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
    965 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    966 	    USBD_NO_TIMEOUT, cue_rxeof);
    967 	usbd_transfer(c->cue_xfer);
    968 
    969 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
    970 		    __FUNCTION__));
    971 }
    972 
    973 /*
    974  * A frame was downloaded to the chip. It's safe for us to clean up
    975  * the list buffers.
    976  */
    977 
    978 static void
    979 cue_txeof(xfer, priv, status)
    980 	usbd_xfer_handle	xfer;
    981 	usbd_private_handle	priv;
    982 	usbd_status		status;
    983 {
    984 	struct cue_chain	*c = priv;
    985 	struct cue_softc	*sc = c->cue_sc;
    986 	struct ifnet		*ifp = GET_IFP(sc);
    987 	int			s;
    988 
    989 	if (sc->cue_dying)
    990 		return;
    991 
    992 	s = splimp();
    993 
    994 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
    995 		    __FUNCTION__, status));
    996 
    997 	ifp->if_timer = 0;
    998 	ifp->if_flags &= ~IFF_OACTIVE;
    999 
   1000 	if (status != USBD_NORMAL_COMPLETION) {
   1001 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1002 			splx(s);
   1003 			return;
   1004 		}
   1005 		ifp->if_oerrors++;
   1006 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
   1007 		    usbd_errstr(status));
   1008 		if (status == USBD_STALLED)
   1009 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
   1010 		splx(s);
   1011 		return;
   1012 	}
   1013 
   1014 	ifp->if_opackets++;
   1015 
   1016 #if defined(__FreeBSD__)
   1017 	c->cue_mbuf->m_pkthdr.rcvif = ifp;
   1018 	usb_tx_done(c->cue_mbuf);
   1019 	c->cue_mbuf = NULL;
   1020 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1021 	m_freem(c->cue_mbuf);
   1022 	c->cue_mbuf = NULL;
   1023 
   1024 	if (ifp->if_snd.ifq_head != NULL)
   1025 		cue_start(ifp);
   1026 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1027 
   1028 	splx(s);
   1029 }
   1030 
   1031 static void
   1032 cue_tick(xsc)
   1033 	void			*xsc;
   1034 {
   1035 	struct cue_softc	*sc = xsc;
   1036 	struct ifnet		*ifp;
   1037 	int			s;
   1038 
   1039 	if (sc == NULL)
   1040 		return;
   1041 
   1042 	if (sc->cue_dying)
   1043 		return;
   1044 
   1045 	s = splimp();
   1046 
   1047 	ifp = GET_IFP(sc);
   1048 
   1049 	ifp->if_collisions += csr_read_2(sc, CUE_TX_SINGLECOLL);
   1050 	ifp->if_collisions += csr_read_2(sc, CUE_TX_MULTICOLL);
   1051 	ifp->if_collisions += csr_read_2(sc, CUE_TX_EXCESSCOLL);
   1052 
   1053 	if (csr_read_2(sc, CUE_RX_FRAMEERR))
   1054 		ifp->if_ierrors++;
   1055 
   1056 	usb_timeout(cue_tick, sc, hz, sc->cue_stat_ch);
   1057 
   1058 	splx(s);
   1059 }
   1060 
   1061 static int
   1062 cue_send(sc, m, idx)
   1063 	struct cue_softc	*sc;
   1064 	struct mbuf		*m;
   1065 	int			idx;
   1066 {
   1067 	int			total_len;
   1068 	struct cue_chain	*c;
   1069 	usbd_status		err;
   1070 
   1071 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1072 
   1073 	c = &sc->cue_cdata.cue_tx_chain[idx];
   1074 
   1075 	/*
   1076 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1077 	 * bytes at the beginning to hold the frame length.
   1078 	 */
   1079 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
   1080 	c->cue_mbuf = m;
   1081 
   1082 	total_len = m->m_pkthdr.len + 2;
   1083 
   1084 	/* The first two bytes are the frame length */
   1085 	c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1086 	c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1087 
   1088 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
   1089 	    c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
   1090 
   1091 	/* Transmit */
   1092 	err = usbd_transfer(c->cue_xfer);
   1093 	if (err != USBD_IN_PROGRESS) {
   1094 		cue_stop(sc);
   1095 		return (EIO);
   1096 	}
   1097 
   1098 	sc->cue_cdata.cue_tx_cnt++;
   1099 
   1100 	return (0);
   1101 }
   1102 
   1103 static void
   1104 cue_start(ifp)
   1105 	struct ifnet		*ifp;
   1106 {
   1107 	struct cue_softc	*sc = ifp->if_softc;
   1108 	struct mbuf		*m_head = NULL;
   1109 
   1110 	if (sc->cue_dying)
   1111 		return;
   1112 
   1113 	if (ifp->if_flags & IFF_OACTIVE)
   1114 		return;
   1115 
   1116 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1117 	if (m_head == NULL)
   1118 		return;
   1119 
   1120 	if (cue_send(sc, m_head, 0)) {
   1121 		IF_PREPEND(&ifp->if_snd, m_head);
   1122 		ifp->if_flags |= IFF_OACTIVE;
   1123 		return;
   1124 	}
   1125 
   1126 #if NBPFILTER > 0
   1127 	/*
   1128 	 * If there's a BPF listener, bounce a copy of this frame
   1129 	 * to him.
   1130 	 */
   1131 	if (ifp->if_bpf)
   1132 		BPF_MTAP(ifp, m_head);
   1133 #endif
   1134 
   1135 	ifp->if_flags |= IFF_OACTIVE;
   1136 
   1137 	/*
   1138 	 * Set a timeout in case the chip goes out to lunch.
   1139 	 */
   1140 	ifp->if_timer = 5;
   1141 }
   1142 
   1143 static void
   1144 cue_init(xsc)
   1145 	void			*xsc;
   1146 {
   1147 	struct cue_softc	*sc = xsc;
   1148 	struct ifnet		*ifp = GET_IFP(sc);
   1149 	struct cue_chain	*c;
   1150 	usbd_status		err;
   1151 	int			i, s;
   1152 	u_char			*eaddr;
   1153 
   1154 	if (sc->cue_dying)
   1155 		return;
   1156 
   1157 	if (ifp->if_flags & IFF_RUNNING)
   1158 		return;
   1159 
   1160 	s = splimp();
   1161 
   1162 	/*
   1163 	 * Cancel pending I/O and free all RX/TX buffers.
   1164 	 */
   1165 #ifdef foo
   1166 	cue_reset(sc);
   1167 #endif
   1168 
   1169 #if defined(__FreeBSD__)
   1170 	eaddr = sc->arpcom.ac_enaddr;
   1171 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1172 	eaddr = LLADDR(ifp->if_sadl);
   1173 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1174 	/* Set MAC address */
   1175 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1176 		csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
   1177 
   1178 	/* Enable RX logic. */
   1179 	csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
   1180 
   1181 	 /* If we want promiscuous mode, set the allframes bit. */
   1182 	if (ifp->if_flags & IFF_PROMISC)
   1183 		CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1184 	else
   1185 		CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1186 
   1187 	/* Init TX ring. */
   1188 	if (cue_tx_list_init(sc) == ENOBUFS) {
   1189 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
   1190 		splx(s);
   1191 		return;
   1192 	}
   1193 
   1194 	/* Init RX ring. */
   1195 	if (cue_rx_list_init(sc) == ENOBUFS) {
   1196 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
   1197 		splx(s);
   1198 		return;
   1199 	}
   1200 
   1201 	/* Load the multicast filter. */
   1202 	cue_setmulti(sc);
   1203 
   1204 	/*
   1205 	 * Set the number of RX and TX buffers that we want
   1206 	 * to reserve inside the ASIC.
   1207 	 */
   1208 	csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1209 	csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1210 
   1211 	/* Set advanced operation modes. */
   1212 	csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1213 	    CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
   1214 
   1215 	/* Program the LED operation. */
   1216 	csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1217 
   1218 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1219 	/* Open RX and TX pipes. */
   1220 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1221 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1222 	if (err) {
   1223 		printf("%s: open rx pipe failed: %s\n",
   1224 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1225 		splx(s);
   1226 		return;
   1227 	}
   1228 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1229 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1230 	if (err) {
   1231 		printf("%s: open tx pipe failed: %s\n",
   1232 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1233 		splx(s);
   1234 		return;
   1235 	}
   1236 
   1237 	/* Start up the receive pipe. */
   1238 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1239 		c = &sc->cue_cdata.cue_rx_chain[i];
   1240 		usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
   1241 		    c, c->cue_buf, CUE_BUFSZ,
   1242 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1243 		    cue_rxeof);
   1244 		usbd_transfer(c->cue_xfer);
   1245 	}
   1246 	}
   1247 
   1248 	ifp->if_flags |= IFF_RUNNING;
   1249 	ifp->if_flags &= ~IFF_OACTIVE;
   1250 
   1251 	splx(s);
   1252 
   1253 	usb_timeout(cue_tick, sc, hz, sc->cue_stat_ch);
   1254 }
   1255 
   1256 static int
   1257 cue_ioctl(ifp, command, data)
   1258 	struct ifnet		*ifp;
   1259 	u_long			command;
   1260 	caddr_t			data;
   1261 {
   1262 	struct cue_softc	*sc = ifp->if_softc;
   1263 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1264 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1265 	struct ifreq		*ifr = (struct ifreq *)data;
   1266 #endif
   1267 	int			s, error = 0;
   1268 
   1269 	if (sc->cue_dying)
   1270 		return (EIO);
   1271 
   1272 	s = splimp();
   1273 
   1274 	switch(command) {
   1275 #if defined(__FreeBSD__)
   1276 	case SIOCSIFADDR:
   1277 	case SIOCGIFADDR:
   1278 	case SIOCSIFMTU:
   1279 		error = ether_ioctl(ifp, command, data);
   1280 		break;
   1281 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1282 	case SIOCSIFADDR:
   1283 		ifp->if_flags |= IFF_UP;
   1284 		cue_init(sc);
   1285 
   1286 		switch (ifa->ifa_addr->sa_family) {
   1287 #ifdef INET
   1288 		case AF_INET:
   1289 			arp_ifinit(ifp, ifa);
   1290 			break;
   1291 #endif /* INET */
   1292 #ifdef NS
   1293 		case AF_NS:
   1294 		    {
   1295 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1296 
   1297 			if (ns_nullhost(*ina))
   1298 				ina->x_host = *(union ns_host *)
   1299 					LLADDR(ifp->if_sadl);
   1300 			else
   1301 				memcpy(LLADDR(ifp->if_sadl),
   1302 				       ina->x_host.c_host,
   1303 				       ifp->if_addrlen);
   1304 			break;
   1305 		    }
   1306 #endif /* NS */
   1307 		}
   1308 		break;
   1309 
   1310 	case SIOCSIFMTU:
   1311 		if (ifr->ifr_mtu > ETHERMTU)
   1312 			error = EINVAL;
   1313 		else
   1314 			ifp->if_mtu = ifr->ifr_mtu;
   1315 		break;
   1316 
   1317 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1318 
   1319 	case SIOCSIFFLAGS:
   1320 		if (ifp->if_flags & IFF_UP) {
   1321 			if (ifp->if_flags & IFF_RUNNING &&
   1322 			    ifp->if_flags & IFF_PROMISC &&
   1323 			    !(sc->cue_if_flags & IFF_PROMISC)) {
   1324 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1325 				cue_setmulti(sc);
   1326 			} else if (ifp->if_flags & IFF_RUNNING &&
   1327 			    !(ifp->if_flags & IFF_PROMISC) &&
   1328 			    sc->cue_if_flags & IFF_PROMISC) {
   1329 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1330 				cue_setmulti(sc);
   1331 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1332 				cue_init(sc);
   1333 		} else {
   1334 			if (ifp->if_flags & IFF_RUNNING)
   1335 				cue_stop(sc);
   1336 		}
   1337 		sc->cue_if_flags = ifp->if_flags;
   1338 		error = 0;
   1339 		break;
   1340 	case SIOCADDMULTI:
   1341 	case SIOCDELMULTI:
   1342 		cue_setmulti(sc);
   1343 		error = 0;
   1344 		break;
   1345 	default:
   1346 		error = EINVAL;
   1347 		break;
   1348 	}
   1349 
   1350 	splx(s);
   1351 
   1352 	return (error);
   1353 }
   1354 
   1355 static void
   1356 cue_watchdog(ifp)
   1357 	struct ifnet		*ifp;
   1358 {
   1359 	struct cue_softc	*sc = ifp->if_softc;
   1360 
   1361 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1362 
   1363 	if (sc->cue_dying)
   1364 		return;
   1365 
   1366 	ifp->if_oerrors++;
   1367 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
   1368 
   1369 	/*
   1370 	 * The polling business is a kludge to avoid allowing the
   1371 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1372 	 * kill us since the watchdog routine is invoked from
   1373 	 * interrupt context.
   1374 	 */
   1375 	usbd_set_polling(sc->cue_udev, 1);
   1376 	cue_stop(sc);
   1377 	cue_init(sc);
   1378 	usbd_set_polling(sc->cue_udev, 0);
   1379 
   1380 	if (ifp->if_snd.ifq_head != NULL)
   1381 		cue_start(ifp);
   1382 }
   1383 
   1384 /*
   1385  * Stop the adapter and free any mbufs allocated to the
   1386  * RX and TX lists.
   1387  */
   1388 static void
   1389 cue_stop(sc)
   1390 	struct cue_softc	*sc;
   1391 {
   1392 	usbd_status		err;
   1393 	struct ifnet		*ifp;
   1394 	int			i;
   1395 
   1396 	ifp = GET_IFP(sc);
   1397 	ifp->if_timer = 0;
   1398 
   1399 	csr_write_1(sc, CUE_ETHCTL, 0);
   1400 	cue_reset(sc);
   1401 	usb_untimeout(cue_tick, sc, sc->cue_stat_ch);
   1402 
   1403 	/* Stop transfers. */
   1404 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1405 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1406 		if (err) {
   1407 			printf("%s: abort rx pipe failed: %s\n",
   1408 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1409 		}
   1410 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1411 		if (err) {
   1412 			printf("%s: close rx pipe failed: %s\n",
   1413 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1414 		}
   1415 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1416 	}
   1417 
   1418 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1419 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1420 		if (err) {
   1421 			printf("%s: abort tx pipe failed: %s\n",
   1422 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1423 		}
   1424 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1425 		if (err) {
   1426 			printf("%s: close tx pipe failed: %s\n",
   1427 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1428 		}
   1429 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1430 	}
   1431 
   1432 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1433 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1434 		if (err) {
   1435 			printf("%s: abort intr pipe failed: %s\n",
   1436 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1437 		}
   1438 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1439 		if (err) {
   1440 			printf("%s: close intr pipe failed: %s\n",
   1441 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1442 		}
   1443 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1444 	}
   1445 
   1446 	/* Free RX resources. */
   1447 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1448 		if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
   1449 			m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
   1450 			sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
   1451 		}
   1452 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
   1453 			usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
   1454 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
   1455 		}
   1456 	}
   1457 
   1458 	/* Free TX resources. */
   1459 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
   1460 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
   1461 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
   1462 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
   1463 		}
   1464 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
   1465 			usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
   1466 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
   1467 		}
   1468 	}
   1469 
   1470 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1471 
   1472 	return;
   1473 }
   1474 
   1475 #ifdef __FreeBSD__
   1476 /*
   1477  * Stop all chip I/O so that the kernel's probe routines don't
   1478  * get confused by errant DMAs when rebooting.
   1479  */
   1480 static void
   1481 cue_shutdown(dev)
   1482 	device_t		dev;
   1483 {
   1484 	struct cue_softc	*sc;
   1485 
   1486 	sc = device_get_softc(dev);
   1487 
   1488 	cue_reset(sc);
   1489 	cue_stop(sc);
   1490 }
   1491 #endif
   1492