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