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