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