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