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