Home | History | Annotate | Line # | Download | only in usb
if_cue.c revision 1.68.4.16
      1  1.68.4.16     skrll /*	$NetBSD: if_cue.c,v 1.68.4.16 2017/09/01 13:45:24 skrll Exp $	*/
      2        1.1  augustss /*
      3        1.1  augustss  * Copyright (c) 1997, 1998, 1999, 2000
      4        1.1  augustss  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      5        1.1  augustss  *
      6        1.1  augustss  * Redistribution and use in source and binary forms, with or without
      7        1.1  augustss  * modification, are permitted provided that the following conditions
      8        1.1  augustss  * are met:
      9        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     10        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     11        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     13        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     14        1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     15        1.1  augustss  *    must display the following acknowledgement:
     16        1.1  augustss  *	This product includes software developed by Bill Paul.
     17        1.1  augustss  * 4. Neither the name of the author nor the names of any co-contributors
     18        1.1  augustss  *    may be used to endorse or promote products derived from this software
     19        1.1  augustss  *    without specific prior written permission.
     20        1.1  augustss  *
     21        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     22        1.1  augustss  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23        1.1  augustss  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24        1.1  augustss  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     25        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31        1.1  augustss  * THE POSSIBILITY OF SUCH DAMAGE.
     32        1.1  augustss  *
     33        1.1  augustss  * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
     34        1.1  augustss  */
     35        1.1  augustss 
     36        1.1  augustss /*
     37       1.15  augustss  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
     38        1.1  augustss  * adapters and others.
     39        1.1  augustss  *
     40        1.1  augustss  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     41        1.1  augustss  * Electrical Engineering Department
     42        1.1  augustss  * Columbia University, New York City
     43        1.1  augustss  */
     44        1.1  augustss 
     45        1.1  augustss /*
     46       1.15  augustss  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
     47        1.1  augustss  * RX filter uses a 512-bit multicast hash table, single perfect entry
     48        1.1  augustss  * for the station address, and promiscuous mode. Unlike the ADMtek
     49        1.1  augustss  * and KLSI chips, the CATC ASIC supports read and write combining
     50        1.1  augustss  * mode where multiple packets can be transfered using a single bulk
     51        1.1  augustss  * transaction, which helps performance a great deal.
     52        1.1  augustss  */
     53        1.1  augustss 
     54        1.1  augustss /*
     55        1.1  augustss  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     56        1.1  augustss  */
     57       1.36     lukem 
     58       1.36     lukem #include <sys/cdefs.h>
     59  1.68.4.16     skrll __KERNEL_RCSID(0, "$NetBSD: if_cue.c,v 1.68.4.16 2017/09/01 13:45:24 skrll Exp $");
     60        1.1  augustss 
     61       1.65  christos #ifdef _KERNEL_OPT
     62        1.1  augustss #include "opt_inet.h"
     63  1.68.4.12     skrll #include "opt_usb.h"
     64       1.65  christos #endif
     65        1.1  augustss 
     66        1.1  augustss #include <sys/param.h>
     67        1.1  augustss #include <sys/systm.h>
     68       1.14   thorpej #include <sys/callout.h>
     69        1.1  augustss #include <sys/sockio.h>
     70        1.1  augustss #include <sys/mbuf.h>
     71        1.1  augustss #include <sys/kernel.h>
     72        1.1  augustss #include <sys/socket.h>
     73       1.63       mrg #include <sys/bus.h>
     74        1.1  augustss #include <sys/device.h>
     75        1.1  augustss 
     76        1.1  augustss #include <net/if.h>
     77        1.1  augustss #include <net/if_arp.h>
     78        1.1  augustss #include <net/if_dl.h>
     79        1.1  augustss #include <net/bpf.h>
     80       1.62       mrg #include <net/if_ether.h>
     81        1.1  augustss 
     82        1.1  augustss #ifdef INET
     83       1.40  augustss #include <netinet/in.h>
     84        1.1  augustss #include <netinet/if_inarp.h>
     85        1.1  augustss #endif
     86        1.1  augustss 
     87        1.1  augustss #include <dev/usb/usb.h>
     88        1.1  augustss #include <dev/usb/usbdi.h>
     89        1.1  augustss #include <dev/usb/usbdi_util.h>
     90       1.63       mrg #include <dev/usb/usbdivar.h>
     91        1.1  augustss #include <dev/usb/usbdevs.h>
     92        1.1  augustss 
     93        1.1  augustss #include <dev/usb/if_cuereg.h>
     94        1.1  augustss 
     95        1.1  augustss #ifdef CUE_DEBUG
     96       1.60    dyoung #define DPRINTF(x)	if (cuedebug) printf x
     97       1.60    dyoung #define DPRINTFN(n,x)	if (cuedebug >= (n)) printf x
     98        1.1  augustss int	cuedebug = 0;
     99        1.1  augustss #else
    100        1.1  augustss #define DPRINTF(x)
    101        1.1  augustss #define DPRINTFN(n,x)
    102        1.1  augustss #endif
    103        1.1  augustss 
    104        1.1  augustss /*
    105        1.1  augustss  * Various supported device vendors/products.
    106        1.1  augustss  */
    107       1.37  augustss Static struct usb_devno cue_devs[] = {
    108        1.1  augustss 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
    109        1.1  augustss 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
    110       1.22  augustss 	{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
    111       1.15  augustss 	/* Belkin F5U111 adapter covered by NETMATE entry */
    112        1.1  augustss };
    113       1.38  augustss #define cue_lookup(v, p) (usb_lookup(cue_devs, v, p))
    114        1.1  augustss 
    115       1.60    dyoung int cue_match(device_t, cfdata_t, void *);
    116       1.60    dyoung void cue_attach(device_t, device_t, void *);
    117       1.60    dyoung int cue_detach(device_t, int);
    118       1.60    dyoung int cue_activate(device_t, enum devact);
    119       1.60    dyoung extern struct cfdriver cue_cd;
    120       1.60    dyoung CFATTACH_DECL_NEW(cue, sizeof(struct cue_softc), cue_match, cue_attach,
    121       1.60    dyoung     cue_detach, cue_activate);
    122        1.1  augustss 
    123       1.23  augustss Static int cue_tx_list_init(struct cue_softc *);
    124  1.68.4.13     skrll Static void cue_tx_list_free(struct cue_softc *);
    125       1.23  augustss Static int cue_rx_list_init(struct cue_softc *);
    126  1.68.4.13     skrll #if 0
    127  1.68.4.13     skrll Static void cue_rx_list_free(struct cue_softc *);
    128  1.68.4.13     skrll #endif
    129       1.23  augustss Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
    130       1.23  augustss Static int cue_send(struct cue_softc *, struct mbuf *, int);
    131   1.68.4.5     skrll Static void cue_rxeof(struct usbd_xfer *, void *, usbd_status);
    132   1.68.4.5     skrll Static void cue_txeof(struct usbd_xfer *, void *, usbd_status);
    133       1.23  augustss Static void cue_tick(void *);
    134       1.30  augustss Static void cue_tick_task(void *);
    135       1.23  augustss Static void cue_start(struct ifnet *);
    136  1.68.4.13     skrll Static void cue_start_locked(struct ifnet *);
    137       1.49  christos Static int cue_ioctl(struct ifnet *, u_long, void *);
    138  1.68.4.13     skrll Static int cue_ifflags_cb(struct ethercom *);
    139  1.68.4.13     skrll Static int cue_init(struct ifnet *);
    140  1.68.4.13     skrll Static int cue_init_locked(struct ifnet *);
    141       1.23  augustss Static void cue_stop(struct cue_softc *);
    142  1.68.4.13     skrll Static void cue_stop_locked(struct cue_softc *);
    143       1.23  augustss Static void cue_watchdog(struct ifnet *);
    144       1.23  augustss 
    145       1.23  augustss Static void cue_setmulti(struct cue_softc *);
    146   1.68.4.1     skrll Static uint32_t cue_crc(const char *);
    147       1.23  augustss Static void cue_reset(struct cue_softc *);
    148       1.23  augustss 
    149       1.23  augustss Static int cue_csr_read_1(struct cue_softc *, int);
    150       1.23  augustss Static int cue_csr_write_1(struct cue_softc *, int, int);
    151       1.23  augustss Static int cue_csr_read_2(struct cue_softc *, int);
    152       1.23  augustss #if 0
    153       1.23  augustss Static int cue_csr_write_2(struct cue_softc *, int, int);
    154       1.23  augustss #endif
    155       1.23  augustss Static int cue_mem(struct cue_softc *, int, int, void *, int);
    156       1.23  augustss Static int cue_getmac(struct cue_softc *, void *);
    157        1.1  augustss 
    158        1.1  augustss #define CUE_SETBIT(sc, reg, x)				\
    159       1.15  augustss 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
    160        1.1  augustss 
    161        1.1  augustss #define CUE_CLRBIT(sc, reg, x)				\
    162       1.15  augustss 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
    163        1.8  augustss 
    164       1.17  augustss Static int
    165       1.23  augustss cue_csr_read_1(struct cue_softc	*sc, int reg)
    166        1.1  augustss {
    167        1.1  augustss 	usb_device_request_t	req;
    168        1.1  augustss 	usbd_status		err;
    169   1.68.4.1     skrll 	uint8_t			val = 0;
    170        1.1  augustss 
    171       1.15  augustss 	if (sc->cue_dying)
    172   1.68.4.4     skrll 		return 0;
    173        1.1  augustss 
    174        1.1  augustss 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    175        1.1  augustss 	req.bRequest = CUE_CMD_READREG;
    176        1.1  augustss 	USETW(req.wValue, 0);
    177        1.1  augustss 	USETW(req.wIndex, reg);
    178        1.1  augustss 	USETW(req.wLength, 1);
    179        1.1  augustss 
    180       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, &val);
    181        1.1  augustss 
    182       1.15  augustss 	if (err) {
    183       1.15  augustss 		DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n",
    184       1.60    dyoung 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    185   1.68.4.4     skrll 		return 0;
    186       1.15  augustss 	}
    187       1.15  augustss 
    188       1.40  augustss 	DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n",
    189       1.60    dyoung 	    device_xname(sc->cue_dev), reg, val));
    190        1.1  augustss 
    191   1.68.4.4     skrll 	return val;
    192        1.1  augustss }
    193        1.1  augustss 
    194       1.17  augustss Static int
    195       1.23  augustss cue_csr_read_2(struct cue_softc	*sc, int reg)
    196        1.1  augustss {
    197        1.1  augustss 	usb_device_request_t	req;
    198        1.1  augustss 	usbd_status		err;
    199       1.15  augustss 	uWord			val;
    200        1.1  augustss 
    201       1.15  augustss 	if (sc->cue_dying)
    202   1.68.4.4     skrll 		return 0;
    203        1.1  augustss 
    204        1.1  augustss 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    205        1.1  augustss 	req.bRequest = CUE_CMD_READREG;
    206        1.1  augustss 	USETW(req.wValue, 0);
    207        1.1  augustss 	USETW(req.wIndex, reg);
    208        1.1  augustss 	USETW(req.wLength, 2);
    209        1.1  augustss 
    210       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, &val);
    211        1.1  augustss 
    212       1.40  augustss 	DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n",
    213       1.60    dyoung 	    device_xname(sc->cue_dev), reg, UGETW(val)));
    214        1.1  augustss 
    215       1.15  augustss 	if (err) {
    216       1.15  augustss 		DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
    217       1.60    dyoung 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    218   1.68.4.4     skrll 		return 0;
    219       1.15  augustss 	}
    220        1.1  augustss 
    221   1.68.4.4     skrll 	return UGETW(val);
    222        1.1  augustss }
    223        1.1  augustss 
    224       1.17  augustss Static int
    225       1.23  augustss cue_csr_write_1(struct cue_softc *sc, int reg, int val)
    226        1.1  augustss {
    227        1.1  augustss 	usb_device_request_t	req;
    228        1.1  augustss 	usbd_status		err;
    229        1.1  augustss 
    230       1.15  augustss 	if (sc->cue_dying)
    231   1.68.4.4     skrll 		return 0;
    232       1.15  augustss 
    233       1.40  augustss 	DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n",
    234       1.60    dyoung 	    device_xname(sc->cue_dev), reg, val));
    235        1.1  augustss 
    236        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    237        1.1  augustss 	req.bRequest = CUE_CMD_WRITEREG;
    238        1.1  augustss 	USETW(req.wValue, val);
    239        1.1  augustss 	USETW(req.wIndex, reg);
    240        1.1  augustss 	USETW(req.wLength, 0);
    241        1.1  augustss 
    242       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    243        1.1  augustss 
    244       1.15  augustss 	if (err) {
    245       1.15  augustss 		DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n",
    246       1.60    dyoung 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    247   1.68.4.4     skrll 		return -1;
    248       1.15  augustss 	}
    249       1.15  augustss 
    250       1.40  augustss 	DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n",
    251       1.60    dyoung 	    device_xname(sc->cue_dev), reg, cue_csr_read_1(sc, reg)));
    252        1.1  augustss 
    253   1.68.4.4     skrll 	return 0;
    254        1.1  augustss }
    255        1.1  augustss 
    256       1.23  augustss #if 0
    257       1.17  augustss Static int
    258       1.23  augustss cue_csr_write_2(struct cue_softc *sc, int reg, int aval)
    259        1.1  augustss {
    260        1.1  augustss 	usb_device_request_t	req;
    261        1.1  augustss 	usbd_status		err;
    262       1.15  augustss 	uWord			val;
    263        1.1  augustss 	int			s;
    264        1.1  augustss 
    265       1.15  augustss 	if (sc->cue_dying)
    266   1.68.4.4     skrll 		return 0;
    267       1.15  augustss 
    268       1.40  augustss 	DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n",
    269       1.60    dyoung 	    device_xname(sc->cue_dev), reg, aval));
    270        1.1  augustss 
    271       1.15  augustss 	USETW(val, aval);
    272        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    273        1.1  augustss 	req.bRequest = CUE_CMD_WRITEREG;
    274        1.1  augustss 	USETW(req.wValue, val);
    275        1.1  augustss 	USETW(req.wIndex, reg);
    276        1.1  augustss 	USETW(req.wLength, 0);
    277        1.1  augustss 
    278       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    279        1.1  augustss 
    280       1.15  augustss 	if (err) {
    281       1.15  augustss 		DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n",
    282       1.60    dyoung 		    device_xname(sc->cue_dev), reg, usbd_errstr(err)));
    283   1.68.4.4     skrll 		return -1;
    284       1.15  augustss 	}
    285        1.1  augustss 
    286   1.68.4.4     skrll 	return 0;
    287        1.1  augustss }
    288        1.1  augustss #endif
    289        1.1  augustss 
    290       1.17  augustss Static int
    291       1.23  augustss cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
    292        1.1  augustss {
    293        1.1  augustss 	usb_device_request_t	req;
    294        1.1  augustss 	usbd_status		err;
    295        1.1  augustss 
    296       1.15  augustss 	DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n",
    297       1.60    dyoung 	    device_xname(sc->cue_dev), cmd, addr, len));
    298        1.1  augustss 
    299        1.1  augustss 	if (cmd == CUE_CMD_READSRAM)
    300        1.1  augustss 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    301        1.1  augustss 	else
    302        1.1  augustss 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    303        1.1  augustss 	req.bRequest = cmd;
    304        1.1  augustss 	USETW(req.wValue, 0);
    305        1.1  augustss 	USETW(req.wIndex, addr);
    306        1.1  augustss 	USETW(req.wLength, len);
    307        1.1  augustss 
    308       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, buf);
    309        1.1  augustss 
    310       1.15  augustss 	if (err) {
    311       1.15  augustss 		DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n",
    312       1.60    dyoung 		    device_xname(sc->cue_dev), addr, usbd_errstr(err)));
    313   1.68.4.4     skrll 		return -1;
    314       1.15  augustss 	}
    315        1.1  augustss 
    316   1.68.4.4     skrll 	return 0;
    317        1.1  augustss }
    318        1.1  augustss 
    319       1.17  augustss Static int
    320       1.23  augustss cue_getmac(struct cue_softc *sc, void *buf)
    321        1.1  augustss {
    322        1.1  augustss 	usb_device_request_t	req;
    323        1.1  augustss 	usbd_status		err;
    324        1.1  augustss 
    325       1.60    dyoung 	DPRINTFN(10,("%s: cue_getmac\n", device_xname(sc->cue_dev)));
    326        1.1  augustss 
    327        1.1  augustss 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    328        1.1  augustss 	req.bRequest = CUE_CMD_GET_MACADDR;
    329        1.1  augustss 	USETW(req.wValue, 0);
    330        1.1  augustss 	USETW(req.wIndex, 0);
    331        1.1  augustss 	USETW(req.wLength, ETHER_ADDR_LEN);
    332        1.1  augustss 
    333       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, buf);
    334        1.1  augustss 
    335        1.1  augustss 	if (err) {
    336       1.60    dyoung 		printf("%s: read MAC address failed\n",
    337       1.60    dyoung 		    device_xname(sc->cue_dev));
    338   1.68.4.4     skrll 		return -1;
    339        1.1  augustss 	}
    340        1.1  augustss 
    341   1.68.4.4     skrll 	return 0;
    342        1.1  augustss }
    343        1.1  augustss 
    344        1.1  augustss #define CUE_POLY	0xEDB88320
    345        1.1  augustss #define CUE_BITS	9
    346        1.1  augustss 
    347   1.68.4.1     skrll Static uint32_t
    348       1.42      yamt cue_crc(const char *addr)
    349        1.1  augustss {
    350   1.68.4.1     skrll 	uint32_t		idx, bit, data, crc;
    351        1.1  augustss 
    352        1.1  augustss 	/* Compute CRC for the address value. */
    353        1.1  augustss 	crc = 0xFFFFFFFF; /* initial value */
    354        1.1  augustss 
    355        1.1  augustss 	for (idx = 0; idx < 6; idx++) {
    356        1.1  augustss 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    357        1.1  augustss 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
    358        1.1  augustss 	}
    359        1.1  augustss 
    360   1.68.4.4     skrll 	return crc & ((1 << CUE_BITS) - 1);
    361        1.1  augustss }
    362        1.1  augustss 
    363       1.17  augustss Static void
    364       1.23  augustss cue_setmulti(struct cue_softc *sc)
    365        1.1  augustss {
    366        1.1  augustss 	struct ifnet		*ifp;
    367       1.15  augustss 	struct ether_multi	*enm;
    368       1.15  augustss 	struct ether_multistep	step;
    369   1.68.4.1     skrll 	uint32_t		h, i;
    370        1.1  augustss 
    371        1.1  augustss 	ifp = GET_IFP(sc);
    372        1.1  augustss 
    373       1.40  augustss 	DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
    374       1.60    dyoung 	    device_xname(sc->cue_dev), ifp->if_flags));
    375       1.15  augustss 
    376       1.33     enami 	if (ifp->if_flags & IFF_PROMISC) {
    377       1.33     enami allmulti:
    378       1.33     enami 		ifp->if_flags |= IFF_ALLMULTI;
    379        1.1  augustss 		for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
    380        1.1  augustss 			sc->cue_mctab[i] = 0xFF;
    381       1.15  augustss 		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
    382       1.15  augustss 		    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
    383        1.1  augustss 		return;
    384        1.1  augustss 	}
    385        1.1  augustss 
    386        1.1  augustss 	/* first, zot all the existing hash bits */
    387        1.1  augustss 	for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
    388        1.1  augustss 		sc->cue_mctab[i] = 0;
    389        1.1  augustss 
    390        1.1  augustss 	/* now program new ones */
    391       1.15  augustss 	ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
    392       1.15  augustss 	while (enm != NULL) {
    393       1.15  augustss 		if (memcmp(enm->enm_addrlo,
    394       1.33     enami 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
    395       1.33     enami 			goto allmulti;
    396       1.33     enami 
    397       1.15  augustss 		h = cue_crc(enm->enm_addrlo);
    398       1.40  augustss 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
    399       1.15  augustss 		ETHER_NEXT_MULTI(step, enm);
    400       1.15  augustss 	}
    401       1.33     enami 
    402       1.33     enami 	ifp->if_flags &= ~IFF_ALLMULTI;
    403        1.1  augustss 
    404        1.1  augustss 	/*
    405        1.1  augustss 	 * Also include the broadcast address in the filter
    406        1.1  augustss 	 * so we can receive broadcast frames.
    407        1.1  augustss 	 */
    408        1.1  augustss 	if (ifp->if_flags & IFF_BROADCAST) {
    409        1.1  augustss 		h = cue_crc(etherbroadcastaddr);
    410       1.40  augustss 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
    411        1.1  augustss 	}
    412        1.1  augustss 
    413        1.1  augustss 	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
    414        1.1  augustss 	    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
    415        1.1  augustss }
    416        1.1  augustss 
    417       1.17  augustss Static void
    418       1.23  augustss cue_reset(struct cue_softc *sc)
    419        1.1  augustss {
    420        1.1  augustss 	usb_device_request_t	req;
    421        1.1  augustss 	usbd_status		err;
    422        1.1  augustss 
    423       1.60    dyoung 	DPRINTFN(2,("%s: cue_reset\n", device_xname(sc->cue_dev)));
    424       1.15  augustss 
    425       1.15  augustss 	if (sc->cue_dying)
    426       1.15  augustss 		return;
    427        1.1  augustss 
    428        1.1  augustss 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    429        1.1  augustss 	req.bRequest = CUE_CMD_RESET;
    430        1.1  augustss 	USETW(req.wValue, 0);
    431        1.1  augustss 	USETW(req.wIndex, 0);
    432        1.1  augustss 	USETW(req.wLength, 0);
    433       1.15  augustss 
    434       1.30  augustss 	err = usbd_do_request(sc->cue_udev, &req, NULL);
    435        1.1  augustss 
    436        1.1  augustss 	if (err)
    437       1.60    dyoung 		printf("%s: reset failed\n", device_xname(sc->cue_dev));
    438        1.1  augustss 
    439        1.1  augustss 	/* Wait a little while for the chip to get its brains in order. */
    440       1.30  augustss 	usbd_delay_ms(sc->cue_udev, 1);
    441        1.1  augustss }
    442        1.1  augustss 
    443        1.1  augustss /*
    444        1.1  augustss  * Probe for a CATC chip.
    445        1.1  augustss  */
    446       1.60    dyoung int
    447       1.60    dyoung cue_match(device_t parent, cfdata_t match, void *aux)
    448        1.1  augustss {
    449       1.60    dyoung 	struct usb_attach_arg *uaa = aux;
    450        1.1  augustss 
    451   1.68.4.6     skrll 	return cue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    452   1.68.4.4     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    453        1.1  augustss }
    454        1.1  augustss 
    455        1.1  augustss /*
    456        1.1  augustss  * Attach the interface. Allocate softc structures, do ifmedia
    457        1.1  augustss  * setup and ethernet/BPF attach.
    458        1.1  augustss  */
    459       1.60    dyoung void
    460       1.60    dyoung cue_attach(device_t parent, device_t self, void *aux)
    461        1.1  augustss {
    462       1.60    dyoung 	struct cue_softc *sc = device_private(self);
    463       1.60    dyoung 	struct usb_attach_arg *uaa = aux;
    464       1.43  augustss 	char			*devinfop;
    465        1.1  augustss 	u_char			eaddr[ETHER_ADDR_LEN];
    466   1.68.4.6     skrll 	struct usbd_device *	dev = uaa->uaa_device;
    467   1.68.4.5     skrll 	struct usbd_interface *	iface;
    468        1.1  augustss 	usbd_status		err;
    469        1.1  augustss 	struct ifnet		*ifp;
    470        1.1  augustss 	usb_interface_descriptor_t	*id;
    471        1.1  augustss 	usb_endpoint_descriptor_t	*ed;
    472        1.1  augustss 	int			i;
    473        1.1  augustss 
    474        1.1  augustss 	DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
    475        1.1  augustss 
    476       1.53      cube 	sc->cue_dev = self;
    477       1.53      cube 
    478       1.55    plunky 	aprint_naive("\n");
    479       1.55    plunky 	aprint_normal("\n");
    480       1.55    plunky 
    481       1.43  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    482       1.53      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    483       1.43  augustss 	usbd_devinfo_free(devinfop);
    484        1.1  augustss 
    485       1.25  augustss 	err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
    486        1.1  augustss 	if (err) {
    487       1.64     skrll 		aprint_error_dev(self, "failed to set configuration"
    488       1.64     skrll 		    ", err=%s\n", usbd_errstr(err));
    489       1.60    dyoung 		return;
    490        1.1  augustss 	}
    491        1.1  augustss 
    492        1.1  augustss 	sc->cue_udev = dev;
    493   1.68.4.6     skrll 	sc->cue_product = uaa->uaa_product;
    494   1.68.4.6     skrll 	sc->cue_vendor = uaa->uaa_vendor;
    495        1.1  augustss 
    496       1.66  jmcneill 	usb_init_task(&sc->cue_tick_task, cue_tick_task, sc, 0);
    497       1.67  jmcneill 	usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc, 0);
    498       1.30  augustss 
    499  1.68.4.13     skrll 	mutex_init(&sc->cue_lock, MUTEX_DEFAULT, IPL_NONE);
    500  1.68.4.13     skrll 	mutex_init(&sc->cue_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    501  1.68.4.13     skrll 	mutex_init(&sc->cue_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    502  1.68.4.13     skrll 
    503        1.1  augustss 	err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
    504        1.1  augustss 	if (err) {
    505       1.53      cube 		aprint_error_dev(self, "getting interface handle failed\n");
    506       1.60    dyoung 		return;
    507        1.1  augustss 	}
    508        1.1  augustss 
    509        1.1  augustss 	sc->cue_iface = iface;
    510        1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    511        1.1  augustss 
    512        1.1  augustss 	/* Find endpoints. */
    513        1.1  augustss 	for (i = 0; i < id->bNumEndpoints; i++) {
    514        1.8  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    515        1.1  augustss 		if (ed == NULL) {
    516       1.53      cube 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    517       1.60    dyoung 			return;
    518        1.1  augustss 		}
    519        1.1  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    520       1.11  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    521        1.1  augustss 			sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
    522        1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    523       1.11  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    524        1.1  augustss 			sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
    525        1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    526       1.11  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    527        1.1  augustss 			sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
    528        1.1  augustss 		}
    529        1.1  augustss 	}
    530        1.1  augustss 
    531       1.15  augustss #if 0
    532        1.1  augustss 	/* Reset the adapter. */
    533        1.1  augustss 	cue_reset(sc);
    534        1.1  augustss #endif
    535        1.1  augustss 	/*
    536        1.1  augustss 	 * Get station address.
    537        1.1  augustss 	 */
    538        1.1  augustss 	cue_getmac(sc, &eaddr);
    539        1.1  augustss 
    540        1.1  augustss 	/*
    541        1.1  augustss 	 * A CATC chip was detected. Inform the world.
    542        1.1  augustss 	 */
    543       1.53      cube 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    544        1.1  augustss 
    545        1.1  augustss 	/* Initialize interface info.*/
    546        1.1  augustss 	ifp = GET_IFP(sc);
    547        1.1  augustss 	ifp->if_softc = sc;
    548        1.1  augustss 	ifp->if_mtu = ETHERMTU;
    549        1.1  augustss 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    550  1.68.4.13     skrll 	ifp->if_extflags = IFEF_START_MPSAFE;
    551  1.68.4.13     skrll 	ifp->if_init = cue_init;
    552        1.1  augustss 	ifp->if_ioctl = cue_ioctl;
    553        1.1  augustss 	ifp->if_start = cue_start;
    554        1.1  augustss 	ifp->if_watchdog = cue_watchdog;
    555  1.68.4.15     skrll 	strlcpy(ifp->if_xname, device_xname(sc->cue_dev), IFNAMSIZ);
    556        1.1  augustss 
    557       1.27   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    558       1.27   thorpej 
    559        1.1  augustss 	/* Attach the interface. */
    560  1.68.4.13     skrll 	if_initialize(ifp);
    561  1.68.4.13     skrll 	sc->cue_ipq = if_percpuq_create(&sc->cue_ec.ec_if);
    562       1.60    dyoung 	ether_ifattach(ifp, eaddr);
    563  1.68.4.13     skrll 	if_register(ifp);
    564  1.68.4.13     skrll 	ether_set_ifflags_cb(&sc->cue_ec, cue_ifflags_cb);
    565       1.60    dyoung 	rnd_attach_source(&sc->rnd_source, device_xname(sc->cue_dev),
    566       1.68       tls 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    567        1.1  augustss 
    568       1.60    dyoung 	callout_init(&(sc->cue_stat_ch), 0);
    569       1.16  augustss 
    570        1.7  augustss 	sc->cue_attached = 1;
    571        1.5  augustss 
    572       1.60    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev, sc->cue_dev);
    573        1.5  augustss 
    574       1.60    dyoung 	return;
    575        1.1  augustss }
    576        1.1  augustss 
    577       1.60    dyoung int
    578       1.60    dyoung cue_detach(device_t self, int flags)
    579        1.1  augustss {
    580       1.60    dyoung 	struct cue_softc *sc = device_private(self);
    581        1.4  augustss 	struct ifnet		*ifp = GET_IFP(sc);
    582        1.1  augustss 	int			s;
    583        1.1  augustss 
    584       1.60    dyoung 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    585       1.15  augustss 
    586       1.60    dyoung 	callout_stop(&sc->cue_stat_ch);
    587       1.32  augustss 	/*
    588       1.32  augustss 	 * Remove any pending task.  It cannot be executing because it run
    589       1.32  augustss 	 * in the same thread as detach.
    590       1.32  augustss 	 */
    591       1.32  augustss 	usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
    592       1.34  augustss 	usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
    593        1.7  augustss 
    594        1.7  augustss 	if (!sc->cue_attached) {
    595        1.7  augustss 		/* Detached before attached finished, so just bail out. */
    596   1.68.4.4     skrll 		return 0;
    597        1.7  augustss 	}
    598        1.7  augustss 
    599       1.30  augustss 	s = splusb();
    600       1.30  augustss 
    601        1.4  augustss 	if (ifp->if_flags & IFF_RUNNING)
    602        1.4  augustss 		cue_stop(sc);
    603        1.4  augustss 
    604        1.5  augustss 	rnd_detach_source(&sc->rnd_source);
    605        1.4  augustss 	ether_ifdetach(ifp);
    606        1.1  augustss 
    607        1.1  augustss 	if_detach(ifp);
    608        1.1  augustss 
    609        1.4  augustss #ifdef DIAGNOSTIC
    610        1.4  augustss 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
    611        1.4  augustss 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
    612        1.4  augustss 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
    613       1.53      cube 		aprint_debug_dev(self, "detach has active endpoints\n");
    614        1.4  augustss #endif
    615        1.1  augustss 
    616  1.68.4.14     skrll 	mutex_destroy(&sc->cue_rxlock);
    617  1.68.4.14     skrll 	mutex_destroy(&sc->cue_txlock);
    618  1.68.4.14     skrll 	mutex_destroy(&sc->cue_lock);
    619  1.68.4.14     skrll 
    620        1.7  augustss 	sc->cue_attached = 0;
    621        1.1  augustss 	splx(s);
    622        1.5  augustss 
    623       1.60    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev);
    624        1.1  augustss 
    625   1.68.4.4     skrll 	return 0;
    626        1.1  augustss }
    627        1.1  augustss 
    628        1.1  augustss int
    629       1.60    dyoung cue_activate(device_t self, enum devact act)
    630        1.1  augustss {
    631       1.53      cube 	struct cue_softc *sc = device_private(self);
    632        1.1  augustss 
    633       1.60    dyoung 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    634        1.1  augustss 
    635        1.1  augustss 	switch (act) {
    636        1.1  augustss 	case DVACT_DEACTIVATE:
    637        1.4  augustss 		/* Deactivate the interface. */
    638        1.4  augustss 		if_deactivate(&sc->cue_ec.ec_if);
    639        1.1  augustss 		sc->cue_dying = 1;
    640       1.56    dyoung 		return 0;
    641       1.56    dyoung 	default:
    642       1.56    dyoung 		return EOPNOTSUPP;
    643        1.1  augustss 	}
    644        1.1  augustss }
    645        1.1  augustss 
    646        1.1  augustss /*
    647        1.1  augustss  * Initialize an RX descriptor and attach an MBUF cluster.
    648        1.1  augustss  */
    649       1.17  augustss Static int
    650       1.23  augustss cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
    651        1.1  augustss {
    652        1.1  augustss 	struct mbuf		*m_new = NULL;
    653        1.1  augustss 
    654        1.1  augustss 	if (m == NULL) {
    655        1.1  augustss 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    656        1.1  augustss 		if (m_new == NULL) {
    657        1.1  augustss 			printf("%s: no memory for rx list "
    658       1.60    dyoung 			    "-- packet dropped!\n", device_xname(sc->cue_dev));
    659   1.68.4.4     skrll 			return ENOBUFS;
    660        1.1  augustss 		}
    661        1.1  augustss 
    662        1.1  augustss 		MCLGET(m_new, M_DONTWAIT);
    663        1.1  augustss 		if (!(m_new->m_flags & M_EXT)) {
    664        1.1  augustss 			printf("%s: no memory for rx list "
    665       1.60    dyoung 			    "-- packet dropped!\n", device_xname(sc->cue_dev));
    666        1.1  augustss 			m_freem(m_new);
    667   1.68.4.4     skrll 			return ENOBUFS;
    668        1.1  augustss 		}
    669        1.1  augustss 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    670        1.1  augustss 	} else {
    671        1.1  augustss 		m_new = m;
    672        1.1  augustss 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    673        1.1  augustss 		m_new->m_data = m_new->m_ext.ext_buf;
    674        1.1  augustss 	}
    675        1.1  augustss 
    676        1.1  augustss 	m_adj(m_new, ETHER_ALIGN);
    677        1.1  augustss 	c->cue_mbuf = m_new;
    678        1.1  augustss 
    679   1.68.4.4     skrll 	return 0;
    680        1.1  augustss }
    681        1.1  augustss 
    682       1.17  augustss Static int
    683       1.23  augustss cue_rx_list_init(struct cue_softc *sc)
    684        1.1  augustss {
    685        1.1  augustss 	struct cue_cdata	*cd;
    686        1.1  augustss 	struct cue_chain	*c;
    687        1.1  augustss 	int			i;
    688        1.1  augustss 
    689        1.1  augustss 	cd = &sc->cue_cdata;
    690        1.1  augustss 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
    691        1.1  augustss 		c = &cd->cue_rx_chain[i];
    692        1.1  augustss 		c->cue_sc = sc;
    693        1.1  augustss 		c->cue_idx = i;
    694        1.1  augustss 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
    695   1.68.4.4     skrll 			return ENOBUFS;
    696        1.1  augustss 		if (c->cue_xfer == NULL) {
    697   1.68.4.8     skrll 			int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX],
    698   1.68.4.8     skrll 			    CUE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->cue_xfer);
    699   1.68.4.8     skrll 			if (error)
    700   1.68.4.8     skrll 				return error;
    701   1.68.4.8     skrll 			c->cue_buf = usbd_get_buffer(c->cue_xfer);
    702        1.1  augustss 		}
    703        1.1  augustss 	}
    704        1.1  augustss 
    705   1.68.4.4     skrll 	return 0;
    706        1.1  augustss }
    707  1.68.4.13     skrll #if 0
    708  1.68.4.13     skrll Static void
    709  1.68.4.13     skrll cue_rx_list_free(struct cue_softc *sc)
    710  1.68.4.13     skrll {
    711  1.68.4.13     skrll 	/* Free RX resources. */
    712  1.68.4.13     skrll 	for (int i = 0; i < CUE_RX_LIST_CNT; i++) {
    713  1.68.4.13     skrll 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
    714  1.68.4.13     skrll 			usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
    715  1.68.4.13     skrll 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
    716  1.68.4.13     skrll 		}
    717  1.68.4.13     skrll 	}
    718  1.68.4.13     skrll }
    719  1.68.4.13     skrll #endif
    720       1.17  augustss Static int
    721       1.23  augustss cue_tx_list_init(struct cue_softc *sc)
    722        1.1  augustss {
    723        1.1  augustss 	struct cue_cdata	*cd;
    724        1.1  augustss 	struct cue_chain	*c;
    725        1.1  augustss 	int			i;
    726        1.1  augustss 
    727        1.1  augustss 	cd = &sc->cue_cdata;
    728        1.1  augustss 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
    729        1.1  augustss 		c = &cd->cue_tx_chain[i];
    730        1.1  augustss 		c->cue_sc = sc;
    731        1.1  augustss 		c->cue_idx = i;
    732        1.1  augustss 		c->cue_mbuf = NULL;
    733        1.1  augustss 		if (c->cue_xfer == NULL) {
    734   1.68.4.8     skrll 			int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX],
    735   1.68.4.8     skrll 			    CUE_BUFSZ, 0, 0, &c->cue_xfer);
    736   1.68.4.8     skrll 			if (error)
    737   1.68.4.8     skrll 				return error;
    738   1.68.4.8     skrll 			c->cue_buf = usbd_get_buffer(c->cue_xfer);
    739        1.1  augustss 		}
    740        1.1  augustss 	}
    741        1.1  augustss 
    742   1.68.4.4     skrll 	return 0;
    743        1.1  augustss }
    744        1.1  augustss 
    745  1.68.4.13     skrll Static void
    746  1.68.4.13     skrll cue_tx_list_free(struct cue_softc *sc)
    747  1.68.4.13     skrll {
    748  1.68.4.13     skrll 	/* Free TX resources. */
    749  1.68.4.13     skrll 	for (int i = 0; i < CUE_TX_LIST_CNT; i++) {
    750  1.68.4.13     skrll 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
    751  1.68.4.13     skrll 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
    752  1.68.4.13     skrll 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
    753  1.68.4.13     skrll 		}
    754  1.68.4.13     skrll 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
    755  1.68.4.13     skrll 			usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
    756  1.68.4.13     skrll 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
    757  1.68.4.13     skrll 		}
    758  1.68.4.13     skrll 	}
    759  1.68.4.13     skrll }
    760  1.68.4.13     skrll 
    761        1.1  augustss /*
    762        1.1  augustss  * A frame has been uploaded: pass the resulting mbuf chain up to
    763        1.1  augustss  * the higher level protocols.
    764        1.1  augustss  */
    765       1.17  augustss Static void
    766   1.68.4.5     skrll cue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    767        1.1  augustss {
    768        1.1  augustss 	struct cue_chain	*c = priv;
    769        1.1  augustss 	struct cue_softc	*sc = c->cue_sc;
    770        1.1  augustss 	struct ifnet		*ifp = GET_IFP(sc);
    771        1.1  augustss 	struct mbuf		*m;
    772        1.1  augustss 	int			total_len = 0;
    773   1.68.4.1     skrll 	uint16_t		len;
    774        1.1  augustss 
    775       1.60    dyoung 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
    776       1.39  augustss 		     __func__, status));
    777        1.1  augustss 
    778        1.4  augustss 	if (sc->cue_dying)
    779        1.4  augustss 		return;
    780        1.4  augustss 
    781        1.1  augustss 	if (!(ifp->if_flags & IFF_RUNNING))
    782        1.1  augustss 		return;
    783        1.1  augustss 
    784        1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    785        1.1  augustss 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    786        1.1  augustss 			return;
    787       1.10  augustss 		sc->cue_rx_errs++;
    788       1.10  augustss 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
    789       1.10  augustss 			printf("%s: %u usb errors on rx: %s\n",
    790       1.60    dyoung 			    device_xname(sc->cue_dev), sc->cue_rx_errs,
    791       1.10  augustss 			    usbd_errstr(status));
    792       1.10  augustss 			sc->cue_rx_errs = 0;
    793       1.10  augustss 		}
    794        1.1  augustss 		if (status == USBD_STALLED)
    795       1.44  augustss 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
    796        1.1  augustss 		goto done;
    797        1.1  augustss 	}
    798        1.1  augustss 
    799        1.1  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    800        1.1  augustss 
    801        1.3  augustss 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
    802        1.1  augustss 
    803        1.1  augustss 	m = c->cue_mbuf;
    804   1.68.4.1     skrll 	len = UGETW(mtod(m, uint8_t *));
    805        1.1  augustss 
    806        1.1  augustss 	/* No errors; receive the packet. */
    807        1.1  augustss 	total_len = len;
    808        1.1  augustss 
    809        1.1  augustss 	if (len < sizeof(struct ether_header)) {
    810        1.1  augustss 		ifp->if_ierrors++;
    811        1.1  augustss 		goto done;
    812        1.1  augustss 	}
    813        1.1  augustss 
    814   1.68.4.1     skrll 	m_adj(m, sizeof(uint16_t));
    815        1.1  augustss 	m->m_pkthdr.len = m->m_len = total_len;
    816        1.1  augustss 
    817  1.68.4.11     skrll 	m_set_rcvif(m, ifp);
    818        1.1  augustss 
    819        1.1  augustss 	/* XXX ugly */
    820        1.1  augustss 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
    821        1.1  augustss 		ifp->if_ierrors++;
    822  1.68.4.13     skrll 		goto done;
    823        1.1  augustss 	}
    824        1.1  augustss 
    825       1.60    dyoung 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev),
    826       1.39  augustss 		    __func__, m->m_len));
    827  1.68.4.13     skrll 	if_percpuq_enqueue(sc->cue_ipq, m);
    828        1.1  augustss 
    829        1.1  augustss done:
    830   1.68.4.8     skrll 
    831        1.1  augustss 	/* Setup new transfer. */
    832   1.68.4.8     skrll 	usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
    833   1.68.4.8     skrll 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
    834        1.1  augustss 	usbd_transfer(c->cue_xfer);
    835        1.1  augustss 
    836       1.60    dyoung 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev),
    837       1.39  augustss 		    __func__));
    838        1.1  augustss }
    839        1.1  augustss 
    840        1.1  augustss /*
    841        1.1  augustss  * A frame was downloaded to the chip. It's safe for us to clean up
    842        1.1  augustss  * the list buffers.
    843        1.1  augustss  */
    844       1.17  augustss Static void
    845   1.68.4.5     skrll cue_txeof(struct usbd_xfer *xfer, void *priv,
    846       1.46  christos     usbd_status status)
    847        1.1  augustss {
    848        1.1  augustss 	struct cue_chain	*c = priv;
    849        1.1  augustss 	struct cue_softc	*sc = c->cue_sc;
    850        1.1  augustss 	struct ifnet		*ifp = GET_IFP(sc);
    851        1.1  augustss 	int			s;
    852        1.1  augustss 
    853        1.4  augustss 	if (sc->cue_dying)
    854        1.4  augustss 		return;
    855        1.4  augustss 
    856       1.35   thorpej 	s = splnet();
    857        1.1  augustss 
    858       1.60    dyoung 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
    859       1.39  augustss 		    __func__, status));
    860        1.1  augustss 
    861        1.1  augustss 	ifp->if_timer = 0;
    862        1.1  augustss 	ifp->if_flags &= ~IFF_OACTIVE;
    863        1.1  augustss 
    864        1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    865        1.1  augustss 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    866        1.1  augustss 			splx(s);
    867        1.1  augustss 			return;
    868        1.1  augustss 		}
    869        1.1  augustss 		ifp->if_oerrors++;
    870       1.60    dyoung 		printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev),
    871        1.1  augustss 		    usbd_errstr(status));
    872        1.1  augustss 		if (status == USBD_STALLED)
    873       1.44  augustss 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
    874        1.1  augustss 		splx(s);
    875        1.1  augustss 		return;
    876        1.1  augustss 	}
    877        1.1  augustss 
    878        1.1  augustss 	ifp->if_opackets++;
    879        1.1  augustss 
    880        1.1  augustss 	m_freem(c->cue_mbuf);
    881        1.1  augustss 	c->cue_mbuf = NULL;
    882        1.1  augustss 
    883       1.27   thorpej 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    884        1.1  augustss 		cue_start(ifp);
    885        1.1  augustss 
    886        1.1  augustss 	splx(s);
    887        1.1  augustss }
    888        1.1  augustss 
    889       1.17  augustss Static void
    890       1.23  augustss cue_tick(void *xsc)
    891        1.1  augustss {
    892        1.1  augustss 	struct cue_softc	*sc = xsc;
    893        1.1  augustss 
    894        1.4  augustss 	if (sc == NULL)
    895        1.4  augustss 		return;
    896        1.1  augustss 
    897        1.4  augustss 	if (sc->cue_dying)
    898        1.1  augustss 		return;
    899        1.4  augustss 
    900       1.60    dyoung 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    901       1.15  augustss 
    902       1.30  augustss 	/* Perform statistics update in process context. */
    903       1.47     joerg 	usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
    904       1.30  augustss }
    905       1.30  augustss 
    906       1.30  augustss Static void
    907       1.30  augustss cue_tick_task(void *xsc)
    908       1.30  augustss {
    909       1.30  augustss 	struct cue_softc	*sc = xsc;
    910       1.30  augustss 	struct ifnet		*ifp;
    911       1.30  augustss 
    912       1.30  augustss 	if (sc->cue_dying)
    913       1.30  augustss 		return;
    914       1.30  augustss 
    915       1.60    dyoung 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
    916        1.1  augustss 
    917        1.1  augustss 	ifp = GET_IFP(sc);
    918        1.1  augustss 
    919       1.15  augustss 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
    920       1.15  augustss 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
    921       1.15  augustss 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
    922        1.1  augustss 
    923       1.15  augustss 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
    924        1.1  augustss 		ifp->if_ierrors++;
    925        1.1  augustss }
    926        1.1  augustss 
    927       1.17  augustss Static int
    928       1.23  augustss cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
    929        1.1  augustss {
    930        1.1  augustss 	int			total_len;
    931        1.1  augustss 	struct cue_chain	*c;
    932        1.1  augustss 	usbd_status		err;
    933        1.1  augustss 
    934        1.1  augustss 	c = &sc->cue_cdata.cue_tx_chain[idx];
    935        1.1  augustss 
    936        1.1  augustss 	/*
    937        1.1  augustss 	 * Copy the mbuf data into a contiguous buffer, leaving two
    938        1.1  augustss 	 * bytes at the beginning to hold the frame length.
    939        1.1  augustss 	 */
    940        1.1  augustss 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
    941        1.1  augustss 	c->cue_mbuf = m;
    942        1.1  augustss 
    943        1.1  augustss 	total_len = m->m_pkthdr.len + 2;
    944        1.1  augustss 
    945       1.15  augustss 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    946       1.60    dyoung 		     device_xname(sc->cue_dev), __func__, total_len));
    947       1.15  augustss 
    948        1.1  augustss 	/* The first two bytes are the frame length */
    949   1.68.4.1     skrll 	c->cue_buf[0] = (uint8_t)m->m_pkthdr.len;
    950   1.68.4.1     skrll 	c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
    951        1.1  augustss 
    952       1.15  augustss 	/* XXX 10000 */
    953   1.68.4.8     skrll 	usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000,
    954   1.68.4.8     skrll 	    cue_txeof);
    955        1.1  augustss 
    956        1.1  augustss 	/* Transmit */
    957        1.1  augustss 	err = usbd_transfer(c->cue_xfer);
    958        1.1  augustss 	if (err != USBD_IN_PROGRESS) {
    959       1.60    dyoung 		printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev),
    960       1.21  augustss 		       usbd_errstr(err));
    961       1.31  augustss 		/* Stop the interface from process context. */
    962       1.47     joerg 		usb_add_task(sc->cue_udev, &sc->cue_stop_task,
    963       1.47     joerg 		    USB_TASKQ_DRIVER);
    964   1.68.4.4     skrll 		return EIO;
    965        1.1  augustss 	}
    966        1.1  augustss 
    967        1.1  augustss 	sc->cue_cdata.cue_tx_cnt++;
    968        1.1  augustss 
    969   1.68.4.4     skrll 	return 0;
    970        1.1  augustss }
    971        1.1  augustss 
    972       1.17  augustss Static void
    973       1.23  augustss cue_start(struct ifnet *ifp)
    974        1.1  augustss {
    975  1.68.4.13     skrll 	struct cue_softc *sc = ifp->if_softc;
    976  1.68.4.13     skrll 	KASSERT(ifp->if_extflags & IFEF_START_MPSAFE);
    977  1.68.4.13     skrll 
    978  1.68.4.13     skrll 	mutex_enter(&sc->cue_txlock);
    979  1.68.4.13     skrll 	cue_start_locked(ifp);
    980  1.68.4.13     skrll 	mutex_exit(&sc->cue_txlock);
    981  1.68.4.13     skrll }
    982  1.68.4.13     skrll 
    983  1.68.4.13     skrll Static void
    984  1.68.4.13     skrll cue_start_locked(struct ifnet *ifp)
    985  1.68.4.13     skrll {
    986        1.1  augustss 	struct cue_softc	*sc = ifp->if_softc;
    987        1.1  augustss 	struct mbuf		*m_head = NULL;
    988        1.1  augustss 
    989        1.4  augustss 	if (sc->cue_dying)
    990        1.4  augustss 		return;
    991        1.4  augustss 
    992       1.60    dyoung 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
    993       1.15  augustss 
    994        1.1  augustss 	if (ifp->if_flags & IFF_OACTIVE)
    995        1.1  augustss 		return;
    996        1.1  augustss 
    997       1.27   thorpej 	IFQ_POLL(&ifp->if_snd, m_head);
    998        1.1  augustss 	if (m_head == NULL)
    999        1.1  augustss 		return;
   1000        1.1  augustss 
   1001        1.1  augustss 	if (cue_send(sc, m_head, 0)) {
   1002        1.1  augustss 		ifp->if_flags |= IFF_OACTIVE;
   1003        1.1  augustss 		return;
   1004        1.1  augustss 	}
   1005        1.1  augustss 
   1006       1.27   thorpej 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1007       1.27   thorpej 
   1008        1.1  augustss 	/*
   1009        1.1  augustss 	 * If there's a BPF listener, bounce a copy of this frame
   1010        1.1  augustss 	 * to him.
   1011        1.1  augustss 	 */
   1012       1.59     joerg 	bpf_mtap(ifp, m_head);
   1013        1.1  augustss 
   1014        1.1  augustss 	ifp->if_flags |= IFF_OACTIVE;
   1015        1.1  augustss 
   1016        1.1  augustss 	/*
   1017        1.1  augustss 	 * Set a timeout in case the chip goes out to lunch.
   1018        1.1  augustss 	 */
   1019        1.1  augustss 	ifp->if_timer = 5;
   1020        1.1  augustss }
   1021        1.1  augustss 
   1022  1.68.4.13     skrll Static int
   1023  1.68.4.13     skrll cue_init(struct ifnet *ifp)
   1024        1.1  augustss {
   1025  1.68.4.13     skrll 	struct cue_softc *sc = ifp->if_softc;
   1026  1.68.4.13     skrll 
   1027  1.68.4.13     skrll 	mutex_enter(&sc->cue_lock);
   1028  1.68.4.13     skrll 	int ret = cue_init_locked(ifp);
   1029  1.68.4.13     skrll 	mutex_exit(&sc->cue_lock);
   1030  1.68.4.13     skrll 
   1031  1.68.4.13     skrll 	return ret;
   1032  1.68.4.13     skrll }
   1033  1.68.4.13     skrll 
   1034  1.68.4.13     skrll Static int
   1035  1.68.4.13     skrll cue_init_locked(struct ifnet *ifp)
   1036  1.68.4.13     skrll {
   1037  1.68.4.13     skrll 	struct cue_softc	*sc = ifp->if_softc;
   1038  1.68.4.13     skrll 	int			i, ctl, err = 0;
   1039       1.51    dyoung 	const u_char		*eaddr;
   1040        1.1  augustss 
   1041        1.4  augustss 	if (sc->cue_dying)
   1042  1.68.4.13     skrll 		return EIO;
   1043        1.4  augustss 
   1044       1.60    dyoung 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
   1045       1.15  augustss 
   1046        1.1  augustss 	if (ifp->if_flags & IFF_RUNNING)
   1047  1.68.4.13     skrll 		return 0;
   1048        1.1  augustss 
   1049        1.1  augustss 	/*
   1050        1.1  augustss 	 * Cancel pending I/O and free all RX/TX buffers.
   1051        1.1  augustss 	 */
   1052       1.15  augustss #if 1
   1053        1.1  augustss 	cue_reset(sc);
   1054        1.1  augustss #endif
   1055        1.1  augustss 
   1056       1.15  augustss 	/* Set advanced operation modes. */
   1057       1.15  augustss 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1058       1.15  augustss 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
   1059       1.15  augustss 
   1060       1.51    dyoung 	eaddr = CLLADDR(ifp->if_sadl);
   1061        1.1  augustss 	/* Set MAC address */
   1062        1.1  augustss 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1063       1.15  augustss 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
   1064        1.1  augustss 
   1065        1.1  augustss 	/* Enable RX logic. */
   1066       1.15  augustss 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
   1067        1.1  augustss 	if (ifp->if_flags & IFF_PROMISC)
   1068       1.15  augustss 		ctl |= CUE_ETHCTL_PROMISC;
   1069       1.15  augustss 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
   1070        1.1  augustss 
   1071        1.1  augustss 	/* Load the multicast filter. */
   1072        1.1  augustss 	cue_setmulti(sc);
   1073        1.1  augustss 
   1074        1.1  augustss 	/*
   1075        1.1  augustss 	 * Set the number of RX and TX buffers that we want
   1076        1.1  augustss 	 * to reserve inside the ASIC.
   1077        1.1  augustss 	 */
   1078       1.15  augustss 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
   1079       1.15  augustss 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
   1080        1.1  augustss 
   1081        1.1  augustss 	/* Set advanced operation modes. */
   1082       1.15  augustss 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
   1083       1.15  augustss 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
   1084        1.1  augustss 
   1085        1.1  augustss 	/* Program the LED operation. */
   1086       1.15  augustss 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
   1087        1.1  augustss 
   1088        1.1  augustss 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
   1089  1.68.4.13     skrll 		/* Open RX and TX pipes. */
   1090  1.68.4.13     skrll 		err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
   1091  1.68.4.13     skrll 		    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
   1092  1.68.4.13     skrll 		if (err) {
   1093  1.68.4.13     skrll 			printf("%s: open rx pipe failed: %s\n",
   1094  1.68.4.13     skrll 			device_xname(sc->cue_dev), usbd_errstr(err));
   1095  1.68.4.13     skrll 			goto fail;
   1096  1.68.4.13     skrll 		}
   1097  1.68.4.13     skrll 		err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
   1098  1.68.4.13     skrll 		    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
   1099  1.68.4.13     skrll 		if (err) {
   1100  1.68.4.13     skrll 			printf("%s: open tx pipe failed: %s\n",
   1101  1.68.4.13     skrll 			device_xname(sc->cue_dev), usbd_errstr(err));
   1102  1.68.4.13     skrll 			goto fail1;
   1103       1.15  augustss 		}
   1104       1.15  augustss 	}
   1105   1.68.4.8     skrll 	/* Init TX ring. */
   1106   1.68.4.8     skrll 	if (cue_tx_list_init(sc)) {
   1107   1.68.4.8     skrll 		printf("%s: tx list init failed\n", device_xname(sc->cue_dev));
   1108  1.68.4.13     skrll 		goto fail2;
   1109   1.68.4.8     skrll 	}
   1110   1.68.4.8     skrll 
   1111   1.68.4.8     skrll 	/* Init RX ring. */
   1112   1.68.4.8     skrll 	if (cue_rx_list_init(sc)) {
   1113   1.68.4.8     skrll 		printf("%s: rx list init failed\n", device_xname(sc->cue_dev));
   1114  1.68.4.13     skrll 		goto fail3;
   1115   1.68.4.8     skrll 	}
   1116   1.68.4.8     skrll 
   1117  1.68.4.13     skrll 	/* Start up the receive pipe. */
   1118  1.68.4.13     skrll 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
   1119  1.68.4.13     skrll 		struct cue_chain *c = &sc->cue_cdata.cue_rx_chain[i];
   1120  1.68.4.13     skrll 		usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
   1121  1.68.4.16     skrll 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
   1122  1.68.4.13     skrll 		usbd_transfer(c->cue_xfer);
   1123  1.68.4.13     skrll 	}
   1124       1.15  augustss 
   1125       1.15  augustss 	ifp->if_flags |= IFF_RUNNING;
   1126       1.15  augustss 	ifp->if_flags &= ~IFF_OACTIVE;
   1127       1.15  augustss 
   1128       1.60    dyoung 	callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc));
   1129  1.68.4.13     skrll 
   1130  1.68.4.13     skrll 	return 0;
   1131  1.68.4.13     skrll 
   1132  1.68.4.13     skrll fail3:
   1133  1.68.4.13     skrll 	cue_tx_list_free(sc);
   1134  1.68.4.13     skrll fail2:
   1135  1.68.4.13     skrll 	usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1136  1.68.4.13     skrll fail1:
   1137  1.68.4.13     skrll 	usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1138  1.68.4.13     skrll fail:
   1139  1.68.4.13     skrll 	return EIO;
   1140       1.15  augustss }
   1141       1.15  augustss 
   1142       1.17  augustss Static int
   1143  1.68.4.13     skrll cue_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1144       1.15  augustss {
   1145  1.68.4.13     skrll 	struct cue_softc	*sc = ifp->if_softc;
   1146  1.68.4.13     skrll 	int			s, error = 0;
   1147       1.15  augustss 
   1148  1.68.4.13     skrll 	if (sc->cue_dying)
   1149   1.68.4.4     skrll 		return EIO;
   1150        1.1  augustss 
   1151  1.68.4.13     skrll 	s = splnet();
   1152  1.68.4.13     skrll 	error = ether_ioctl(ifp, cmd, data);
   1153  1.68.4.13     skrll 	splx(s);
   1154   1.68.4.8     skrll 
   1155  1.68.4.13     skrll 	if (error == ENETRESET) {
   1156  1.68.4.13     skrll 		error = 0;
   1157  1.68.4.13     skrll 		if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   1158  1.68.4.13     skrll 			mutex_enter(&sc->cue_lock);
   1159  1.68.4.13     skrll 			cue_setmulti(sc);
   1160  1.68.4.13     skrll 			mutex_exit(&sc->cue_lock);
   1161  1.68.4.13     skrll 		}
   1162        1.1  augustss 	}
   1163  1.68.4.13     skrll 	return error;
   1164        1.1  augustss }
   1165        1.1  augustss 
   1166       1.17  augustss Static int
   1167  1.68.4.13     skrll cue_ifflags_cb(struct ethercom *ec)
   1168        1.1  augustss {
   1169  1.68.4.13     skrll 	struct ifnet *ifp = &ec->ec_if;
   1170  1.68.4.13     skrll 	struct cue_softc *sc = ifp->if_softc;
   1171  1.68.4.13     skrll 	int rc = 0;
   1172        1.1  augustss 
   1173  1.68.4.13     skrll 	mutex_enter(&sc->cue_lock);
   1174        1.4  augustss 
   1175  1.68.4.13     skrll 	int change = ifp->if_flags ^ sc->cue_if_flags;
   1176  1.68.4.13     skrll 	sc->cue_if_flags = ifp->if_flags;
   1177        1.1  augustss 
   1178  1.68.4.13     skrll 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   1179  1.68.4.13     skrll 		rc = ENETRESET;
   1180  1.68.4.13     skrll 		goto out;
   1181  1.68.4.13     skrll 	}
   1182        1.1  augustss 
   1183  1.68.4.13     skrll 	if ((change & IFF_PROMISC) != 0) {
   1184  1.68.4.13     skrll 		if (ifp->if_flags & IFF_PROMISC) {
   1185  1.68.4.13     skrll 			CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1186  1.68.4.13     skrll 		} else if (!(ifp->if_flags & IFF_PROMISC)) {
   1187  1.68.4.13     skrll 			CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
   1188        1.1  augustss 		}
   1189        1.1  augustss 		cue_setmulti(sc);
   1190        1.1  augustss 	}
   1191        1.1  augustss 
   1192  1.68.4.13     skrll out:
   1193  1.68.4.13     skrll 	mutex_exit(&sc->cue_lock);
   1194        1.1  augustss 
   1195  1.68.4.13     skrll 	return rc;
   1196        1.1  augustss }
   1197        1.1  augustss 
   1198       1.17  augustss Static void
   1199       1.23  augustss cue_watchdog(struct ifnet *ifp)
   1200        1.1  augustss {
   1201        1.1  augustss 	struct cue_softc	*sc = ifp->if_softc;
   1202       1.29  augustss 	struct cue_chain	*c;
   1203       1.29  augustss 	usbd_status		stat;
   1204       1.29  augustss 	int			s;
   1205        1.1  augustss 
   1206       1.60    dyoung 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
   1207        1.4  augustss 
   1208        1.4  augustss 	if (sc->cue_dying)
   1209        1.4  augustss 		return;
   1210        1.1  augustss 
   1211        1.1  augustss 	ifp->if_oerrors++;
   1212       1.60    dyoung 	printf("%s: watchdog timeout\n", device_xname(sc->cue_dev));
   1213        1.1  augustss 
   1214       1.29  augustss 	s = splusb();
   1215       1.29  augustss 	c = &sc->cue_cdata.cue_tx_chain[0];
   1216       1.29  augustss 	usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
   1217       1.29  augustss 	cue_txeof(c->cue_xfer, c, stat);
   1218        1.1  augustss 
   1219       1.27   thorpej 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1220        1.1  augustss 		cue_start(ifp);
   1221       1.29  augustss 	splx(s);
   1222        1.1  augustss }
   1223        1.1  augustss 
   1224        1.1  augustss /*
   1225        1.1  augustss  * Stop the adapter and free any mbufs allocated to the
   1226        1.1  augustss  * RX and TX lists.
   1227        1.1  augustss  */
   1228       1.17  augustss Static void
   1229       1.23  augustss cue_stop(struct cue_softc *sc)
   1230        1.1  augustss {
   1231  1.68.4.13     skrll 	mutex_enter(&sc->cue_lock);
   1232  1.68.4.13     skrll 	cue_stop_locked(sc);
   1233  1.68.4.13     skrll 	mutex_exit(&sc->cue_lock);
   1234  1.68.4.13     skrll }
   1235  1.68.4.13     skrll 
   1236  1.68.4.13     skrll Static void
   1237  1.68.4.13     skrll cue_stop_locked(struct cue_softc *sc)
   1238  1.68.4.13     skrll {
   1239        1.1  augustss 	usbd_status		err;
   1240        1.1  augustss 	struct ifnet		*ifp;
   1241        1.1  augustss 
   1242       1.60    dyoung 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
   1243       1.15  augustss 
   1244        1.1  augustss 	ifp = GET_IFP(sc);
   1245        1.1  augustss 	ifp->if_timer = 0;
   1246        1.1  augustss 
   1247       1.15  augustss 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
   1248        1.1  augustss 	cue_reset(sc);
   1249       1.60    dyoung 	callout_stop(&sc->cue_stat_ch);
   1250        1.1  augustss 
   1251        1.1  augustss 	/* Stop transfers. */
   1252        1.1  augustss 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1253        1.1  augustss 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1254        1.1  augustss 		if (err) {
   1255        1.1  augustss 			printf("%s: abort rx pipe failed: %s\n",
   1256       1.60    dyoung 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1257        1.1  augustss 		}
   1258        1.1  augustss 	}
   1259        1.1  augustss 
   1260        1.1  augustss 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1261        1.1  augustss 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1262        1.1  augustss 		if (err) {
   1263        1.1  augustss 			printf("%s: abort tx pipe failed: %s\n",
   1264       1.60    dyoung 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1265        1.1  augustss 		}
   1266        1.1  augustss 	}
   1267        1.1  augustss 
   1268        1.1  augustss 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1269        1.1  augustss 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1270        1.1  augustss 		if (err) {
   1271        1.1  augustss 			printf("%s: abort intr pipe failed: %s\n",
   1272       1.60    dyoung 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1273        1.1  augustss 		}
   1274        1.1  augustss 	}
   1275        1.1  augustss 
   1276   1.68.4.9     skrll 	/* Stop transfers. */
   1277   1.68.4.9     skrll 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
   1278   1.68.4.9     skrll 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
   1279   1.68.4.9     skrll 		if (err) {
   1280   1.68.4.9     skrll 			printf("%s: close rx pipe failed: %s\n",
   1281   1.68.4.9     skrll 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1282   1.68.4.9     skrll 		}
   1283   1.68.4.9     skrll 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
   1284   1.68.4.9     skrll 	}
   1285   1.68.4.9     skrll 
   1286   1.68.4.9     skrll 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
   1287   1.68.4.9     skrll 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
   1288   1.68.4.9     skrll 		if (err) {
   1289   1.68.4.9     skrll 			printf("%s: close tx pipe failed: %s\n",
   1290   1.68.4.9     skrll 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1291   1.68.4.9     skrll 		}
   1292   1.68.4.9     skrll 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
   1293   1.68.4.9     skrll 	}
   1294   1.68.4.9     skrll 
   1295   1.68.4.9     skrll 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
   1296   1.68.4.9     skrll 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
   1297   1.68.4.9     skrll 		if (err) {
   1298   1.68.4.9     skrll 			printf("%s: close intr pipe failed: %s\n",
   1299   1.68.4.9     skrll 			    device_xname(sc->cue_dev), usbd_errstr(err));
   1300   1.68.4.9     skrll 		}
   1301   1.68.4.9     skrll 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
   1302   1.68.4.9     skrll 	}
   1303   1.68.4.9     skrll 
   1304        1.1  augustss 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1305        1.1  augustss }
   1306