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