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