Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.25
      1 /*	$NetBSD: if_cue.c,v 1.25 2000/10/24 14:53:59 augustss Exp $	*/
      2 /*
      3  * Copyright (c) 1997, 1998, 1999, 2000
      4  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Bill Paul.
     17  * 4. Neither the name of the author nor the names of any co-contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
     34  */
     35 
     36 /*
     37  * CATC USB-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 	/* Attach the interface. */
    705 	if_attach(ifp);
    706 	Ether_ifattach(ifp, eaddr);
    707 
    708 #if NBPFILTER > 0
    709 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    710 		  sizeof(struct ether_header));
    711 #endif
    712 #if NRND > 0
    713 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev),
    714 	    RND_TYPE_NET, 0);
    715 #endif
    716 
    717 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    718 
    719 	usb_callout_init(sc->cue_stat_ch);
    720 
    721 	sc->cue_attached = 1;
    722 	splx(s);
    723 
    724 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
    725 	    USBDEV(sc->cue_dev));
    726 
    727 	USB_ATTACH_SUCCESS_RETURN;
    728 }
    729 
    730 USB_DETACH(cue)
    731 {
    732 	USB_DETACH_START(cue, sc);
    733 	struct ifnet		*ifp = GET_IFP(sc);
    734 	int			s;
    735 
    736 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
    737 
    738 	s = splusb();
    739 
    740 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
    741 
    742 	if (!sc->cue_attached) {
    743 		/* Detached before attached finished, so just bail out. */
    744 		splx(s);
    745 		return (0);
    746 	}
    747 
    748 	if (ifp->if_flags & IFF_RUNNING)
    749 		cue_stop(sc);
    750 
    751 #if defined(__NetBSD__)
    752 #if NRND > 0
    753 	rnd_detach_source(&sc->rnd_source);
    754 #endif
    755 #if NBPFILTER > 0
    756 	bpfdetach(ifp);
    757 #endif
    758 	ether_ifdetach(ifp);
    759 #endif /* __NetBSD__ */
    760 
    761 	if_detach(ifp);
    762 
    763 #ifdef DIAGNOSTIC
    764 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
    765 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
    766 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
    767 		printf("%s: detach has active endpoints\n",
    768 		       USBDEVNAME(sc->cue_dev));
    769 #endif
    770 
    771 	sc->cue_attached = 0;
    772 	splx(s);
    773 
    774 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
    775 	    USBDEV(sc->cue_dev));
    776 
    777 	return (0);
    778 }
    779 
    780 #if defined(__NetBSD__) || defined(__OpenBSD__)
    781 int
    782 cue_activate(device_ptr_t self, enum devact act)
    783 {
    784 	struct cue_softc *sc = (struct cue_softc *)self;
    785 
    786 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
    787 
    788 	switch (act) {
    789 	case DVACT_ACTIVATE:
    790 		return (EOPNOTSUPP);
    791 		break;
    792 
    793 	case DVACT_DEACTIVATE:
    794 		/* Deactivate the interface. */
    795 		if_deactivate(&sc->cue_ec.ec_if);
    796 		sc->cue_dying = 1;
    797 		break;
    798 	}
    799 	return (0);
    800 }
    801 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    802 
    803 /*
    804  * Initialize an RX descriptor and attach an MBUF cluster.
    805  */
    806 Static int
    807 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
    808 {
    809 	struct mbuf		*m_new = NULL;
    810 
    811 	if (m == NULL) {
    812 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    813 		if (m_new == NULL) {
    814 			printf("%s: no memory for rx list "
    815 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
    816 			return (ENOBUFS);
    817 		}
    818 
    819 		MCLGET(m_new, M_DONTWAIT);
    820 		if (!(m_new->m_flags & M_EXT)) {
    821 			printf("%s: no memory for rx list "
    822 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
    823 			m_freem(m_new);
    824 			return (ENOBUFS);
    825 		}
    826 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    827 	} else {
    828 		m_new = m;
    829 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    830 		m_new->m_data = m_new->m_ext.ext_buf;
    831 	}
    832 
    833 	m_adj(m_new, ETHER_ALIGN);
    834 	c->cue_mbuf = m_new;
    835 
    836 	return (0);
    837 }
    838 
    839 Static int
    840 cue_rx_list_init(struct cue_softc *sc)
    841 {
    842 	struct cue_cdata	*cd;
    843 	struct cue_chain	*c;
    844 	int			i;
    845 
    846 	cd = &sc->cue_cdata;
    847 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
    848 		c = &cd->cue_rx_chain[i];
    849 		c->cue_sc = sc;
    850 		c->cue_idx = i;
    851 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
    852 			return (ENOBUFS);
    853 		if (c->cue_xfer == NULL) {
    854 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
    855 			if (c->cue_xfer == NULL)
    856 				return (ENOBUFS);
    857 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
    858 			if (c->cue_buf == NULL) {
    859 				usbd_free_xfer(c->cue_xfer);
    860 				return (ENOBUFS);
    861 			}
    862 		}
    863 	}
    864 
    865 	return (0);
    866 }
    867 
    868 Static int
    869 cue_tx_list_init(struct cue_softc *sc)
    870 {
    871 	struct cue_cdata	*cd;
    872 	struct cue_chain	*c;
    873 	int			i;
    874 
    875 	cd = &sc->cue_cdata;
    876 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
    877 		c = &cd->cue_tx_chain[i];
    878 		c->cue_sc = sc;
    879 		c->cue_idx = i;
    880 		c->cue_mbuf = NULL;
    881 		if (c->cue_xfer == NULL) {
    882 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
    883 			if (c->cue_xfer == NULL)
    884 				return (ENOBUFS);
    885 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
    886 			if (c->cue_buf == NULL) {
    887 				usbd_free_xfer(c->cue_xfer);
    888 				return (ENOBUFS);
    889 			}
    890 		}
    891 	}
    892 
    893 	return (0);
    894 }
    895 
    896 #ifdef __FreeBSD__
    897 Static void
    898 cue_rxstart(struct ifnet *ifp)
    899 {
    900 	struct cue_softc	*sc;
    901 	struct cue_chain	*c;
    902 
    903 	sc = ifp->if_softc;
    904 	c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
    905 
    906 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    907 		ifp->if_ierrors++;
    908 		return;
    909 	}
    910 
    911 	/* Setup new transfer. */
    912 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
    913 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    914 	    USBD_NO_TIMEOUT, cue_rxeof);
    915 	usbd_transfer(c->cue_xfer);
    916 }
    917 #endif
    918 
    919 /*
    920  * A frame has been uploaded: pass the resulting mbuf chain up to
    921  * the higher level protocols.
    922  */
    923 Static void
    924 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    925 {
    926 	struct cue_chain	*c = priv;
    927 	struct cue_softc	*sc = c->cue_sc;
    928 	struct ifnet		*ifp = GET_IFP(sc);
    929 	struct mbuf		*m;
    930 	int			total_len = 0;
    931 	u_int16_t		len;
    932 #if defined(__NetBSD__) || defined(__OpenBSD__)
    933 	int			s;
    934 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    935 
    936 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
    937 		     __FUNCTION__, status));
    938 
    939 	if (sc->cue_dying)
    940 		return;
    941 
    942 	if (!(ifp->if_flags & IFF_RUNNING))
    943 		return;
    944 
    945 	if (status != USBD_NORMAL_COMPLETION) {
    946 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    947 			return;
    948 		sc->cue_rx_errs++;
    949 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
    950 			printf("%s: %u usb errors on rx: %s\n",
    951 			    USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
    952 			    usbd_errstr(status));
    953 			sc->cue_rx_errs = 0;
    954 		}
    955 		if (status == USBD_STALLED)
    956 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
    957 		goto done;
    958 	}
    959 
    960 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    961 
    962 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
    963 
    964 	m = c->cue_mbuf;
    965 	len = UGETW(mtod(m, u_int8_t *));
    966 
    967 	/* No errors; receive the packet. */
    968 	total_len = len;
    969 
    970 	if (len < sizeof(struct ether_header)) {
    971 		ifp->if_ierrors++;
    972 		goto done;
    973 	}
    974 
    975 	ifp->if_ipackets++;
    976 	m_adj(m, sizeof(u_int16_t));
    977 	m->m_pkthdr.len = m->m_len = total_len;
    978 
    979 #if defined(__FreeBSD__)
    980 	m->m_pkthdr.rcvif = (struct ifnet *)&cue_qdat;
    981 	/* Put the packet on the special USB input queue. */
    982 	usb_ether_input(m);
    983 
    984 	return;
    985 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    986 	m->m_pkthdr.rcvif = ifp;
    987 
    988 	s = splimp();
    989 
    990 	/* XXX ugly */
    991 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    992 		ifp->if_ierrors++;
    993 		goto done1;
    994 	}
    995 
    996 #if NBPFILTER > 0
    997 	/*
    998 	 * Handle BPF listeners. Let the BPF user see the packet, but
    999 	 * don't pass it up to the ether_input() layer unless it's
   1000 	 * a broadcast packet, multicast packet, matches our ethernet
   1001 	 * address or the interface is in promiscuous mode.
   1002 	 */
   1003 	if (ifp->if_bpf)
   1004 		BPF_MTAP(ifp, m);
   1005 #endif
   1006 
   1007 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
   1008 		    __FUNCTION__, m->m_len));
   1009 	IF_INPUT(ifp, m);
   1010  done1:
   1011 	splx(s);
   1012 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1013 
   1014 done:
   1015 	/* Setup new transfer. */
   1016 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
   1017 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1018 	    USBD_NO_TIMEOUT, cue_rxeof);
   1019 	usbd_transfer(c->cue_xfer);
   1020 
   1021 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
   1022 		    __FUNCTION__));
   1023 }
   1024 
   1025 /*
   1026  * A frame was downloaded to the chip. It's safe for us to clean up
   1027  * the list buffers.
   1028  */
   1029 Static void
   1030 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1031 {
   1032 	struct cue_chain	*c = priv;
   1033 	struct cue_softc	*sc = c->cue_sc;
   1034 	struct ifnet		*ifp = GET_IFP(sc);
   1035 	int			s;
   1036 
   1037 	if (sc->cue_dying)
   1038 		return;
   1039 
   1040 	s = splimp();
   1041 
   1042 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
   1043 		    __FUNCTION__, status));
   1044 
   1045 	ifp->if_timer = 0;
   1046 	ifp->if_flags &= ~IFF_OACTIVE;
   1047 
   1048 	if (status != USBD_NORMAL_COMPLETION) {
   1049 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1050 			splx(s);
   1051 			return;
   1052 		}
   1053 		ifp->if_oerrors++;
   1054 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
   1055 		    usbd_errstr(status));
   1056 		if (status == USBD_STALLED)
   1057 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
   1058 		splx(s);
   1059 		return;
   1060 	}
   1061 
   1062 	ifp->if_opackets++;
   1063 
   1064 #if defined(__FreeBSD__)
   1065 	c->cue_mbuf->m_pkthdr.rcvif = ifp;
   1066 	usb_tx_done(c->cue_mbuf);
   1067 	c->cue_mbuf = NULL;
   1068 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1069 	m_freem(c->cue_mbuf);
   1070 	c->cue_mbuf = NULL;
   1071 
   1072 	if (ifp->if_snd.ifq_head != NULL)
   1073 		cue_start(ifp);
   1074 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1075 
   1076 	splx(s);
   1077 }
   1078 
   1079 Static void
   1080 cue_tick(void *xsc)
   1081 {
   1082 	struct cue_softc	*sc = xsc;
   1083 	struct ifnet		*ifp;
   1084 	int			s;
   1085 
   1086 	if (sc == NULL)
   1087 		return;
   1088 
   1089 	if (sc->cue_dying)
   1090 		return;
   1091 
   1092 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
   1093 
   1094 	s = splimp();
   1095 
   1096 	ifp = GET_IFP(sc);
   1097 
   1098 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
   1099 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
   1100 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
   1101 
   1102 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
   1103 		ifp->if_ierrors++;
   1104 
   1105 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
   1106 
   1107 	splx(s);
   1108 }
   1109 
   1110 Static int
   1111 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
   1112 {
   1113 	int			total_len;
   1114 	struct cue_chain	*c;
   1115 	usbd_status		err;
   1116 
   1117 	c = &sc->cue_cdata.cue_tx_chain[idx];
   1118 
   1119 	/*
   1120 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1121 	 * bytes at the beginning to hold the frame length.
   1122 	 */
   1123 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
   1124 	c->cue_mbuf = m;
   1125 
   1126 	total_len = m->m_pkthdr.len + 2;
   1127 
   1128 	DPRINTFN(10,("%s: %s: total_len=%d\n",
   1129 		     USBDEVNAME(sc->cue_dev), __FUNCTION__, total_len));
   1130 
   1131 	/* The first two bytes are the frame length */
   1132 	c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1133 	c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1134 
   1135 	/* XXX 10000 */
   1136 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
   1137 	    c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
   1138 
   1139 	/* Transmit */
   1140 	err = usbd_transfer(c->cue_xfer);
   1141 	if (err != USBD_IN_PROGRESS) {
   1142 		printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
   1143 		       usbd_errstr(err));
   1144 		cue_stop(sc);
   1145 		return (EIO);
   1146 	}
   1147 
   1148 	sc->cue_cdata.cue_tx_cnt++;
   1149 
   1150 	return (0);
   1151 }
   1152 
   1153 Static void
   1154 cue_start(struct ifnet *ifp)
   1155 {
   1156 	struct cue_softc	*sc = ifp->if_softc;
   1157 	struct mbuf		*m_head = NULL;
   1158 
   1159 	if (sc->cue_dying)
   1160 		return;
   1161 
   1162 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1163 
   1164 	if (ifp->if_flags & IFF_OACTIVE)
   1165 		return;
   1166 
   1167 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1168 	if (m_head == NULL)
   1169 		return;
   1170 
   1171 	if (cue_send(sc, m_head, 0)) {
   1172 		IF_PREPEND(&ifp->if_snd, m_head);
   1173 		ifp->if_flags |= IFF_OACTIVE;
   1174 		return;
   1175 	}
   1176 
   1177 #if NBPFILTER > 0
   1178 	/*
   1179 	 * If there's a BPF listener, bounce a copy of this frame
   1180 	 * to him.
   1181 	 */
   1182 	if (ifp->if_bpf)
   1183 		BPF_MTAP(ifp, m_head);
   1184 #endif
   1185 
   1186 	ifp->if_flags |= IFF_OACTIVE;
   1187 
   1188 	/*
   1189 	 * Set a timeout in case the chip goes out to lunch.
   1190 	 */
   1191 	ifp->if_timer = 5;
   1192 }
   1193 
   1194 Static void
   1195 cue_init(void *xsc)
   1196 {
   1197 	struct cue_softc	*sc = xsc;
   1198 	struct ifnet		*ifp = GET_IFP(sc);
   1199 	int			i, s, ctl;
   1200 	u_char			*eaddr;
   1201 
   1202 	if (sc->cue_dying)
   1203 		return;
   1204 
   1205 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1206 
   1207 	if (ifp->if_flags & IFF_RUNNING)
   1208 		return;
   1209 
   1210 	s = splimp();
   1211 
   1212 	/*
   1213 	 * Cancel pending I/O and free all RX/TX buffers.
   1214 	 */
   1215 #if 1
   1216 	cue_reset(sc);
   1217 #endif
   1218 
   1219 	/* Set advanced operation modes. */
   1220 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1221 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
   1222 
   1223 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   1224 	eaddr = sc->arpcom.ac_enaddr;
   1225 #elif defined(__NetBSD__)
   1226 	eaddr = LLADDR(ifp->if_sadl);
   1227 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1228 	/* Set MAC address */
   1229 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1230 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
   1231 
   1232 	/* Enable RX logic. */
   1233 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
   1234 	if (ifp->if_flags & IFF_PROMISC)
   1235 		ctl |= CUE_ETHCTL_PROMISC;
   1236 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
   1237 
   1238 	/* Init TX ring. */
   1239 	if (cue_tx_list_init(sc) == ENOBUFS) {
   1240 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
   1241 		splx(s);
   1242 		return;
   1243 	}
   1244 
   1245 	/* Init RX ring. */
   1246 	if (cue_rx_list_init(sc) == ENOBUFS) {
   1247 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
   1248 		splx(s);
   1249 		return;
   1250 	}
   1251 
   1252 	/* Load the multicast filter. */
   1253 	cue_setmulti(sc);
   1254 
   1255 	/*
   1256 	 * Set the number of RX and TX buffers that we want
   1257 	 * to reserve inside the ASIC.
   1258 	 */
   1259 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1260 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1261 
   1262 	/* Set advanced operation modes. */
   1263 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1264 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
   1265 
   1266 	/* Program the LED operation. */
   1267 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1268 
   1269 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1270 		if (cue_open_pipes(sc)) {
   1271 			splx(s);
   1272 			return;
   1273 		}
   1274 	}
   1275 
   1276 	ifp->if_flags |= IFF_RUNNING;
   1277 	ifp->if_flags &= ~IFF_OACTIVE;
   1278 
   1279 	splx(s);
   1280 
   1281 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
   1282 }
   1283 
   1284 Static int
   1285 cue_open_pipes(struct cue_softc	*sc)
   1286 {
   1287 	struct cue_chain	*c;
   1288 	usbd_status		err;
   1289 	int			i;
   1290 
   1291 	/* Open RX and TX pipes. */
   1292 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1293 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1294 	if (err) {
   1295 		printf("%s: open rx pipe failed: %s\n",
   1296 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1297 		return (EIO);
   1298 	}
   1299 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1300 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1301 	if (err) {
   1302 		printf("%s: open tx pipe failed: %s\n",
   1303 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1304 		return (EIO);
   1305 	}
   1306 
   1307 	/* Start up the receive pipe. */
   1308 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1309 		c = &sc->cue_cdata.cue_rx_chain[i];
   1310 		usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
   1311 		    c, c->cue_buf, CUE_BUFSZ,
   1312 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1313 		    cue_rxeof);
   1314 		usbd_transfer(c->cue_xfer);
   1315 	}
   1316 
   1317 	return (0);
   1318 }
   1319 
   1320 Static int
   1321 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   1322 {
   1323 	struct cue_softc	*sc = ifp->if_softc;
   1324 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1325 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1326 	struct ifreq		*ifr = (struct ifreq *)data;
   1327 #endif
   1328 	int			s, error = 0;
   1329 
   1330 	if (sc->cue_dying)
   1331 		return (EIO);
   1332 
   1333 	s = splimp();
   1334 
   1335 	switch(command) {
   1336 #if defined(__FreeBSD__)
   1337 	case SIOCSIFADDR:
   1338 	case SIOCGIFADDR:
   1339 	case SIOCSIFMTU:
   1340 		error = ether_ioctl(ifp, command, data);
   1341 		break;
   1342 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1343 	case SIOCSIFADDR:
   1344 		ifp->if_flags |= IFF_UP;
   1345 		cue_init(sc);
   1346 
   1347 		switch (ifa->ifa_addr->sa_family) {
   1348 #ifdef INET
   1349 		case AF_INET:
   1350 #if defined(__NetBSD__)
   1351 			arp_ifinit(ifp, ifa);
   1352 #else
   1353 			arp_ifinit(&sc->arpcom, ifa);
   1354 #endif
   1355 			break;
   1356 #endif /* INET */
   1357 #ifdef NS
   1358 		case AF_NS:
   1359 		    {
   1360 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1361 
   1362 			if (ns_nullhost(*ina))
   1363 				ina->x_host = *(union ns_host *)
   1364 					LLADDR(ifp->if_sadl);
   1365 			else
   1366 				memcpy(LLADDR(ifp->if_sadl),
   1367 				       ina->x_host.c_host,
   1368 				       ifp->if_addrlen);
   1369 			break;
   1370 		    }
   1371 #endif /* NS */
   1372 		}
   1373 		break;
   1374 
   1375 	case SIOCSIFMTU:
   1376 		if (ifr->ifr_mtu > ETHERMTU)
   1377 			error = EINVAL;
   1378 		else
   1379 			ifp->if_mtu = ifr->ifr_mtu;
   1380 		break;
   1381 
   1382 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1383 
   1384 	case SIOCSIFFLAGS:
   1385 		if (ifp->if_flags & IFF_UP) {
   1386 			if (ifp->if_flags & IFF_RUNNING &&
   1387 			    ifp->if_flags & IFF_PROMISC &&
   1388 			    !(sc->cue_if_flags & IFF_PROMISC)) {
   1389 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1390 				cue_setmulti(sc);
   1391 			} else if (ifp->if_flags & IFF_RUNNING &&
   1392 			    !(ifp->if_flags & IFF_PROMISC) &&
   1393 			    sc->cue_if_flags & IFF_PROMISC) {
   1394 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1395 				cue_setmulti(sc);
   1396 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1397 				cue_init(sc);
   1398 		} else {
   1399 			if (ifp->if_flags & IFF_RUNNING)
   1400 				cue_stop(sc);
   1401 		}
   1402 		sc->cue_if_flags = ifp->if_flags;
   1403 		error = 0;
   1404 		break;
   1405 	case SIOCADDMULTI:
   1406 	case SIOCDELMULTI:
   1407 		cue_setmulti(sc);
   1408 		error = 0;
   1409 		break;
   1410 	default:
   1411 		error = EINVAL;
   1412 		break;
   1413 	}
   1414 
   1415 	splx(s);
   1416 
   1417 	return (error);
   1418 }
   1419 
   1420 Static void
   1421 cue_watchdog(struct ifnet *ifp)
   1422 {
   1423 	struct cue_softc	*sc = ifp->if_softc;
   1424 
   1425 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1426 
   1427 	if (sc->cue_dying)
   1428 		return;
   1429 
   1430 	ifp->if_oerrors++;
   1431 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
   1432 
   1433 	/*
   1434 	 * The polling business is a kludge to avoid allowing the
   1435 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1436 	 * kill us since the watchdog routine is invoked from
   1437 	 * interrupt context.
   1438 	 */
   1439 	usbd_set_polling(sc->cue_udev, 1);
   1440 	cue_stop(sc);
   1441 	cue_init(sc);
   1442 	usbd_set_polling(sc->cue_udev, 0);
   1443 
   1444 	if (ifp->if_snd.ifq_head != NULL)
   1445 		cue_start(ifp);
   1446 }
   1447 
   1448 /*
   1449  * Stop the adapter and free any mbufs allocated to the
   1450  * RX and TX lists.
   1451  */
   1452 Static void
   1453 cue_stop(struct cue_softc *sc)
   1454 {
   1455 	usbd_status		err;
   1456 	struct ifnet		*ifp;
   1457 	int			i;
   1458 
   1459 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1460 
   1461 	ifp = GET_IFP(sc);
   1462 	ifp->if_timer = 0;
   1463 
   1464 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
   1465 	cue_reset(sc);
   1466 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
   1467 
   1468 	/* Stop transfers. */
   1469 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1470 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1471 		if (err) {
   1472 			printf("%s: abort rx pipe failed: %s\n",
   1473 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1474 		}
   1475 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1476 		if (err) {
   1477 			printf("%s: close rx pipe failed: %s\n",
   1478 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1479 		}
   1480 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1481 	}
   1482 
   1483 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1484 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1485 		if (err) {
   1486 			printf("%s: abort tx pipe failed: %s\n",
   1487 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1488 		}
   1489 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1490 		if (err) {
   1491 			printf("%s: close tx pipe failed: %s\n",
   1492 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1493 		}
   1494 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1495 	}
   1496 
   1497 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1498 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1499 		if (err) {
   1500 			printf("%s: abort intr pipe failed: %s\n",
   1501 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1502 		}
   1503 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1504 		if (err) {
   1505 			printf("%s: close intr pipe failed: %s\n",
   1506 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1507 		}
   1508 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1509 	}
   1510 
   1511 	/* Free RX resources. */
   1512 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1513 		if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
   1514 			m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
   1515 			sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
   1516 		}
   1517 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
   1518 			usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
   1519 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
   1520 		}
   1521 	}
   1522 
   1523 	/* Free TX resources. */
   1524 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
   1525 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
   1526 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
   1527 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
   1528 		}
   1529 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
   1530 			usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
   1531 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
   1532 		}
   1533 	}
   1534 
   1535 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1536 }
   1537 
   1538 #ifdef __FreeBSD__
   1539 /*
   1540  * Stop all chip I/O so that the kernel's probe routines don't
   1541  * get confused by errant DMAs when rebooting.
   1542  */
   1543 Static void
   1544 cue_shutdown(device_t dev)
   1545 {
   1546 	struct cue_softc	*sc;
   1547 
   1548 	sc = device_get_softc(dev);
   1549 
   1550 	cue_reset(sc);
   1551 	cue_stop(sc);
   1552 }
   1553 #endif
   1554