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