Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.23
      1 /*	$NetBSD: if_cue.c,v 1.23 2000/06/01 14:28:57 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, 0);
    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 #if defined(__NetBSD__)
   1005 		struct ether_header *eh = mtod(m, struct ether_header *);
   1006 		BPF_MTAP(ifp, m);
   1007 		if ((ifp->if_flags & IFF_PROMISC) &&
   1008 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
   1009 			   ETHER_ADDR_LEN) &&
   1010 		    !(eh->ether_dhost[0] & 1)) {
   1011 			m_freem(m);
   1012 			goto done1;
   1013 		}
   1014 #else
   1015 		BPF_MTAP(ifp, m);
   1016 #endif
   1017 	}
   1018 #endif
   1019 
   1020 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
   1021 		    __FUNCTION__, m->m_len));
   1022 	IF_INPUT(ifp, m);
   1023  done1:
   1024 	splx(s);
   1025 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1026 
   1027 done:
   1028 	/* Setup new transfer. */
   1029 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
   1030 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1031 	    USBD_NO_TIMEOUT, cue_rxeof);
   1032 	usbd_transfer(c->cue_xfer);
   1033 
   1034 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
   1035 		    __FUNCTION__));
   1036 }
   1037 
   1038 /*
   1039  * A frame was downloaded to the chip. It's safe for us to clean up
   1040  * the list buffers.
   1041  */
   1042 Static void
   1043 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1044 {
   1045 	struct cue_chain	*c = priv;
   1046 	struct cue_softc	*sc = c->cue_sc;
   1047 	struct ifnet		*ifp = GET_IFP(sc);
   1048 	int			s;
   1049 
   1050 	if (sc->cue_dying)
   1051 		return;
   1052 
   1053 	s = splimp();
   1054 
   1055 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
   1056 		    __FUNCTION__, status));
   1057 
   1058 	ifp->if_timer = 0;
   1059 	ifp->if_flags &= ~IFF_OACTIVE;
   1060 
   1061 	if (status != USBD_NORMAL_COMPLETION) {
   1062 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1063 			splx(s);
   1064 			return;
   1065 		}
   1066 		ifp->if_oerrors++;
   1067 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
   1068 		    usbd_errstr(status));
   1069 		if (status == USBD_STALLED)
   1070 			usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
   1071 		splx(s);
   1072 		return;
   1073 	}
   1074 
   1075 	ifp->if_opackets++;
   1076 
   1077 #if defined(__FreeBSD__)
   1078 	c->cue_mbuf->m_pkthdr.rcvif = ifp;
   1079 	usb_tx_done(c->cue_mbuf);
   1080 	c->cue_mbuf = NULL;
   1081 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1082 	m_freem(c->cue_mbuf);
   1083 	c->cue_mbuf = NULL;
   1084 
   1085 	if (ifp->if_snd.ifq_head != NULL)
   1086 		cue_start(ifp);
   1087 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1088 
   1089 	splx(s);
   1090 }
   1091 
   1092 Static void
   1093 cue_tick(void *xsc)
   1094 {
   1095 	struct cue_softc	*sc = xsc;
   1096 	struct ifnet		*ifp;
   1097 	int			s;
   1098 
   1099 	if (sc == NULL)
   1100 		return;
   1101 
   1102 	if (sc->cue_dying)
   1103 		return;
   1104 
   1105 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
   1106 
   1107 	s = splimp();
   1108 
   1109 	ifp = GET_IFP(sc);
   1110 
   1111 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
   1112 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
   1113 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
   1114 
   1115 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
   1116 		ifp->if_ierrors++;
   1117 
   1118 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
   1119 
   1120 	splx(s);
   1121 }
   1122 
   1123 Static int
   1124 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
   1125 {
   1126 	int			total_len;
   1127 	struct cue_chain	*c;
   1128 	usbd_status		err;
   1129 
   1130 	c = &sc->cue_cdata.cue_tx_chain[idx];
   1131 
   1132 	/*
   1133 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1134 	 * bytes at the beginning to hold the frame length.
   1135 	 */
   1136 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
   1137 	c->cue_mbuf = m;
   1138 
   1139 	total_len = m->m_pkthdr.len + 2;
   1140 
   1141 	DPRINTFN(10,("%s: %s: total_len=%d\n",
   1142 		     USBDEVNAME(sc->cue_dev), __FUNCTION__, total_len));
   1143 
   1144 	/* The first two bytes are the frame length */
   1145 	c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1146 	c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1147 
   1148 	/* XXX 10000 */
   1149 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
   1150 	    c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
   1151 
   1152 	/* Transmit */
   1153 	err = usbd_transfer(c->cue_xfer);
   1154 	if (err != USBD_IN_PROGRESS) {
   1155 		printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
   1156 		       usbd_errstr(err));
   1157 		cue_stop(sc);
   1158 		return (EIO);
   1159 	}
   1160 
   1161 	sc->cue_cdata.cue_tx_cnt++;
   1162 
   1163 	return (0);
   1164 }
   1165 
   1166 Static void
   1167 cue_start(struct ifnet *ifp)
   1168 {
   1169 	struct cue_softc	*sc = ifp->if_softc;
   1170 	struct mbuf		*m_head = NULL;
   1171 
   1172 	if (sc->cue_dying)
   1173 		return;
   1174 
   1175 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1176 
   1177 	if (ifp->if_flags & IFF_OACTIVE)
   1178 		return;
   1179 
   1180 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1181 	if (m_head == NULL)
   1182 		return;
   1183 
   1184 	if (cue_send(sc, m_head, 0)) {
   1185 		IF_PREPEND(&ifp->if_snd, m_head);
   1186 		ifp->if_flags |= IFF_OACTIVE;
   1187 		return;
   1188 	}
   1189 
   1190 #if NBPFILTER > 0
   1191 	/*
   1192 	 * If there's a BPF listener, bounce a copy of this frame
   1193 	 * to him.
   1194 	 */
   1195 	if (ifp->if_bpf)
   1196 		BPF_MTAP(ifp, m_head);
   1197 #endif
   1198 
   1199 	ifp->if_flags |= IFF_OACTIVE;
   1200 
   1201 	/*
   1202 	 * Set a timeout in case the chip goes out to lunch.
   1203 	 */
   1204 	ifp->if_timer = 5;
   1205 }
   1206 
   1207 Static void
   1208 cue_init(void *xsc)
   1209 {
   1210 	struct cue_softc	*sc = xsc;
   1211 	struct ifnet		*ifp = GET_IFP(sc);
   1212 	int			i, s, ctl;
   1213 	u_char			*eaddr;
   1214 
   1215 	if (sc->cue_dying)
   1216 		return;
   1217 
   1218 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1219 
   1220 	if (ifp->if_flags & IFF_RUNNING)
   1221 		return;
   1222 
   1223 	s = splimp();
   1224 
   1225 	/*
   1226 	 * Cancel pending I/O and free all RX/TX buffers.
   1227 	 */
   1228 #if 1
   1229 	cue_reset(sc);
   1230 #endif
   1231 
   1232 	/* Set advanced operation modes. */
   1233 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1234 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
   1235 
   1236 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   1237 	eaddr = sc->arpcom.ac_enaddr;
   1238 #elif defined(__NetBSD__)
   1239 	eaddr = LLADDR(ifp->if_sadl);
   1240 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1241 	/* Set MAC address */
   1242 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1243 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
   1244 
   1245 	/* Enable RX logic. */
   1246 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
   1247 	if (ifp->if_flags & IFF_PROMISC)
   1248 		ctl |= CUE_ETHCTL_PROMISC;
   1249 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
   1250 
   1251 	/* Init TX ring. */
   1252 	if (cue_tx_list_init(sc) == ENOBUFS) {
   1253 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
   1254 		splx(s);
   1255 		return;
   1256 	}
   1257 
   1258 	/* Init RX ring. */
   1259 	if (cue_rx_list_init(sc) == ENOBUFS) {
   1260 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
   1261 		splx(s);
   1262 		return;
   1263 	}
   1264 
   1265 	/* Load the multicast filter. */
   1266 	cue_setmulti(sc);
   1267 
   1268 	/*
   1269 	 * Set the number of RX and TX buffers that we want
   1270 	 * to reserve inside the ASIC.
   1271 	 */
   1272 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1273 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1274 
   1275 	/* Set advanced operation modes. */
   1276 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1277 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
   1278 
   1279 	/* Program the LED operation. */
   1280 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1281 
   1282 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1283 		if (cue_open_pipes(sc)) {
   1284 			splx(s);
   1285 			return;
   1286 		}
   1287 	}
   1288 
   1289 	ifp->if_flags |= IFF_RUNNING;
   1290 	ifp->if_flags &= ~IFF_OACTIVE;
   1291 
   1292 	splx(s);
   1293 
   1294 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
   1295 }
   1296 
   1297 Static int
   1298 cue_open_pipes(struct cue_softc	*sc)
   1299 {
   1300 	struct cue_chain	*c;
   1301 	usbd_status		err;
   1302 	int			i;
   1303 
   1304 	/* Open RX and TX pipes. */
   1305 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1306 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1307 	if (err) {
   1308 		printf("%s: open rx pipe failed: %s\n",
   1309 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1310 		return (EIO);
   1311 	}
   1312 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1313 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1314 	if (err) {
   1315 		printf("%s: open tx pipe failed: %s\n",
   1316 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1317 		return (EIO);
   1318 	}
   1319 
   1320 	/* Start up the receive pipe. */
   1321 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1322 		c = &sc->cue_cdata.cue_rx_chain[i];
   1323 		usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
   1324 		    c, c->cue_buf, CUE_BUFSZ,
   1325 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1326 		    cue_rxeof);
   1327 		usbd_transfer(c->cue_xfer);
   1328 	}
   1329 
   1330 	return (0);
   1331 }
   1332 
   1333 Static int
   1334 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   1335 {
   1336 	struct cue_softc	*sc = ifp->if_softc;
   1337 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1338 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1339 	struct ifreq		*ifr = (struct ifreq *)data;
   1340 #endif
   1341 	int			s, error = 0;
   1342 
   1343 	if (sc->cue_dying)
   1344 		return (EIO);
   1345 
   1346 	s = splimp();
   1347 
   1348 	switch(command) {
   1349 #if defined(__FreeBSD__)
   1350 	case SIOCSIFADDR:
   1351 	case SIOCGIFADDR:
   1352 	case SIOCSIFMTU:
   1353 		error = ether_ioctl(ifp, command, data);
   1354 		break;
   1355 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1356 	case SIOCSIFADDR:
   1357 		ifp->if_flags |= IFF_UP;
   1358 		cue_init(sc);
   1359 
   1360 		switch (ifa->ifa_addr->sa_family) {
   1361 #ifdef INET
   1362 		case AF_INET:
   1363 #if defined(__NetBSD__)
   1364 			arp_ifinit(ifp, ifa);
   1365 #else
   1366 			arp_ifinit(&sc->arpcom, ifa);
   1367 #endif
   1368 			break;
   1369 #endif /* INET */
   1370 #ifdef NS
   1371 		case AF_NS:
   1372 		    {
   1373 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1374 
   1375 			if (ns_nullhost(*ina))
   1376 				ina->x_host = *(union ns_host *)
   1377 					LLADDR(ifp->if_sadl);
   1378 			else
   1379 				memcpy(LLADDR(ifp->if_sadl),
   1380 				       ina->x_host.c_host,
   1381 				       ifp->if_addrlen);
   1382 			break;
   1383 		    }
   1384 #endif /* NS */
   1385 		}
   1386 		break;
   1387 
   1388 	case SIOCSIFMTU:
   1389 		if (ifr->ifr_mtu > ETHERMTU)
   1390 			error = EINVAL;
   1391 		else
   1392 			ifp->if_mtu = ifr->ifr_mtu;
   1393 		break;
   1394 
   1395 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1396 
   1397 	case SIOCSIFFLAGS:
   1398 		if (ifp->if_flags & IFF_UP) {
   1399 			if (ifp->if_flags & IFF_RUNNING &&
   1400 			    ifp->if_flags & IFF_PROMISC &&
   1401 			    !(sc->cue_if_flags & IFF_PROMISC)) {
   1402 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1403 				cue_setmulti(sc);
   1404 			} else if (ifp->if_flags & IFF_RUNNING &&
   1405 			    !(ifp->if_flags & IFF_PROMISC) &&
   1406 			    sc->cue_if_flags & IFF_PROMISC) {
   1407 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1408 				cue_setmulti(sc);
   1409 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1410 				cue_init(sc);
   1411 		} else {
   1412 			if (ifp->if_flags & IFF_RUNNING)
   1413 				cue_stop(sc);
   1414 		}
   1415 		sc->cue_if_flags = ifp->if_flags;
   1416 		error = 0;
   1417 		break;
   1418 	case SIOCADDMULTI:
   1419 	case SIOCDELMULTI:
   1420 		cue_setmulti(sc);
   1421 		error = 0;
   1422 		break;
   1423 	default:
   1424 		error = EINVAL;
   1425 		break;
   1426 	}
   1427 
   1428 	splx(s);
   1429 
   1430 	return (error);
   1431 }
   1432 
   1433 Static void
   1434 cue_watchdog(struct ifnet *ifp)
   1435 {
   1436 	struct cue_softc	*sc = ifp->if_softc;
   1437 
   1438 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1439 
   1440 	if (sc->cue_dying)
   1441 		return;
   1442 
   1443 	ifp->if_oerrors++;
   1444 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
   1445 
   1446 	/*
   1447 	 * The polling business is a kludge to avoid allowing the
   1448 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1449 	 * kill us since the watchdog routine is invoked from
   1450 	 * interrupt context.
   1451 	 */
   1452 	usbd_set_polling(sc->cue_udev, 1);
   1453 	cue_stop(sc);
   1454 	cue_init(sc);
   1455 	usbd_set_polling(sc->cue_udev, 0);
   1456 
   1457 	if (ifp->if_snd.ifq_head != NULL)
   1458 		cue_start(ifp);
   1459 }
   1460 
   1461 /*
   1462  * Stop the adapter and free any mbufs allocated to the
   1463  * RX and TX lists.
   1464  */
   1465 Static void
   1466 cue_stop(struct cue_softc *sc)
   1467 {
   1468 	usbd_status		err;
   1469 	struct ifnet		*ifp;
   1470 	int			i;
   1471 
   1472 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
   1473 
   1474 	ifp = GET_IFP(sc);
   1475 	ifp->if_timer = 0;
   1476 
   1477 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
   1478 	cue_reset(sc);
   1479 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
   1480 
   1481 	/* Stop transfers. */
   1482 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1483 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1484 		if (err) {
   1485 			printf("%s: abort rx pipe failed: %s\n",
   1486 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1487 		}
   1488 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1489 		if (err) {
   1490 			printf("%s: close rx pipe failed: %s\n",
   1491 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1492 		}
   1493 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1494 	}
   1495 
   1496 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1497 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1498 		if (err) {
   1499 			printf("%s: abort tx pipe failed: %s\n",
   1500 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1501 		}
   1502 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1503 		if (err) {
   1504 			printf("%s: close tx pipe failed: %s\n",
   1505 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1506 		}
   1507 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1508 	}
   1509 
   1510 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1511 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1512 		if (err) {
   1513 			printf("%s: abort intr pipe failed: %s\n",
   1514 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1515 		}
   1516 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1517 		if (err) {
   1518 			printf("%s: close intr pipe failed: %s\n",
   1519 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
   1520 		}
   1521 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1522 	}
   1523 
   1524 	/* Free RX resources. */
   1525 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1526 		if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
   1527 			m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
   1528 			sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
   1529 		}
   1530 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
   1531 			usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
   1532 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
   1533 		}
   1534 	}
   1535 
   1536 	/* Free TX resources. */
   1537 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
   1538 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
   1539 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
   1540 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
   1541 		}
   1542 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
   1543 			usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
   1544 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
   1545 		}
   1546 	}
   1547 
   1548 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1549 }
   1550 
   1551 #ifdef __FreeBSD__
   1552 /*
   1553  * Stop all chip I/O so that the kernel's probe routines don't
   1554  * get confused by errant DMAs when rebooting.
   1555  */
   1556 Static void
   1557 cue_shutdown(device_t dev)
   1558 {
   1559 	struct cue_softc	*sc;
   1560 
   1561 	sc = device_get_softc(dev);
   1562 
   1563 	cue_reset(sc);
   1564 	cue_stop(sc);
   1565 }
   1566 #endif
   1567