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