Home | History | Annotate | Line # | Download | only in usb
if_kue.c revision 1.13
      1 /*	$NetBSD: if_kue.c,v 1.13 2000/03/06 21:02:03 thorpej 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_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
     34  */
     35 
     36 /*
     37  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
     38  *
     39  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     40  * Electrical Engineering Department
     41  * Columbia University, New York City
     42  */
     43 
     44 /*
     45  * The KLSI USB to ethernet adapter chip contains an USB serial interface,
     46  * ethernet MAC and embedded microcontroller (called the QT Engine).
     47  * The chip must have firmware loaded into it before it will operate.
     48  * Packets are passed between the chip and host via bulk transfers.
     49  * There is an interrupt endpoint mentioned in the software spec, however
     50  * it's currently unused. This device is 10Mbps half-duplex only, hence
     51  * there is no media selection logic. The MAC supports a 128 entry
     52  * multicast filter, though the exact size of the filter can depend
     53  * on the firmware. Curiously, while the software spec describes various
     54  * ethernet statistics counters, my sample adapter and firmware combination
     55  * claims not to support any statistics counters at all.
     56  *
     57  * Note that once we load the firmware in the device, we have to be
     58  * careful not to load it again: if you restart your computer but
     59  * leave the adapter attached to the USB controller, it may remain
     60  * powered on and retain its firmware. In this case, we don't need
     61  * to load the firmware a second time.
     62  *
     63  * Special thanks to Rob Furr for providing an ADS Technologies
     64  * adapter for development and testing. No monkeys were harmed during
     65  * the development of this driver.
     66  */
     67 
     68 /*
     69  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     70  */
     71 
     72 /*
     73  * TODO:
     74  * only use kue_do_request for downloading firmware.
     75  * more DPRINTF
     76  * proper cleanup on errors
     77  */
     78 #if defined(__NetBSD__) || defined(__OpenBSD__)
     79 #include "opt_inet.h"
     80 #include "opt_ns.h"
     81 #include "bpfilter.h"
     82 #include "rnd.h"
     83 #endif
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/sockio.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/malloc.h>
     90 #include <sys/kernel.h>
     91 #include <sys/socket.h>
     92 
     93 #if defined(__FreeBSD__)
     94 
     95 #include <net/ethernet.h>
     96 #include <machine/clock.h>	/* for DELAY */
     97 #include <sys/bus.h>
     98 
     99 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    100 
    101 #include <sys/device.h>
    102 #if NRND > 0
    103 #include <sys/rnd.h>
    104 #endif
    105 
    106 #endif
    107 
    108 #include <net/if.h>
    109 #include <net/if_arp.h>
    110 #include <net/if_dl.h>
    111 
    112 #if defined(__NetBSD__) || defined(__OpenBSD__)
    113 #include <net/if_ether.h>
    114 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
    115 #else
    116 #define BPF_MTAP(ifp, m) bpf_mtap((ifp), (m))
    117 #endif
    118 
    119 #if defined(__FreeBSD__) || NBPFILTER > 0
    120 #include <net/bpf.h>
    121 #endif
    122 
    123 #if defined(__NetBSD__) || defined(__OpenBSD__)
    124 #ifdef INET
    125 #include <netinet/in.h>
    126 #include <netinet/if_inarp.h>
    127 #endif
    128 
    129 #ifdef NS
    130 #include <netns/ns.h>
    131 #include <netns/ns_if.h>
    132 #endif
    133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    134 
    135 #include <dev/usb/usb.h>
    136 #include <dev/usb/usbdi.h>
    137 #include <dev/usb/usbdi_util.h>
    138 #include <dev/usb/usbdevs.h>
    139 
    140 #ifdef __FreeBSD__
    141 #include <dev/usb/usb_ethersubr.h>
    142 #endif
    143 
    144 #include <dev/usb/if_kuereg.h>
    145 #include <dev/usb/kue_fw.h>
    146 
    147 #ifdef KUE_DEBUG
    148 #define DPRINTF(x)	if (kuedebug) logprintf x
    149 #define DPRINTFN(n,x)	if (kuedebug >= (n)) logprintf x
    150 int	kuedebug = 0;
    151 #else
    152 #define DPRINTF(x)
    153 #define DPRINTFN(n,x)
    154 #endif
    155 
    156 /*
    157  * Various supported device vendors/products.
    158  */
    159 static struct kue_type kue_devs[] = {
    160 	{ USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
    161 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
    162 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
    163 	{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
    164 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
    165 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
    166 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
    167 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
    168 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
    169 	{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
    170 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
    171 	{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
    172 	{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
    173 	{ 0, 0 }
    174 };
    175 
    176 USB_DECLARE_DRIVER(kue);
    177 
    178 static int kue_tx_list_init	__P((struct kue_softc *));
    179 static int kue_rx_list_init	__P((struct kue_softc *));
    180 static int kue_newbuf		__P((struct kue_softc *, struct kue_chain *,
    181 				    struct mbuf *));
    182 static int kue_send		__P((struct kue_softc *, struct mbuf *, int));
    183 static int kue_open_pipes	__P((struct kue_softc *));
    184 static void kue_rxeof		__P((usbd_xfer_handle,
    185 				    usbd_private_handle, usbd_status));
    186 static void kue_txeof		__P((usbd_xfer_handle,
    187 				    usbd_private_handle, usbd_status));
    188 static void kue_start		__P((struct ifnet *));
    189 static int kue_ioctl		__P((struct ifnet *, u_long, caddr_t));
    190 static void kue_init		__P((void *));
    191 static void kue_stop		__P((struct kue_softc *));
    192 static void kue_watchdog		__P((struct ifnet *));
    193 
    194 static void kue_setmulti	__P((struct kue_softc *));
    195 static void kue_reset		__P((struct kue_softc *));
    196 
    197 static usbd_status kue_do_request
    198 				__P((usbd_device_handle,
    199 				   usb_device_request_t *, void *, u_int16_t,
    200 				   u_int32_t *));
    201 static usbd_status kue_ctl_l	__P((struct kue_softc *, int, u_int8_t,
    202 				    u_int16_t, char *, u_int32_t,
    203 				    u_int32_t, u_int32_t *));
    204 #define kue_ctl(sc, rw, breq, val, data, len) \
    205 	kue_ctl_l(sc, rw, breq, val, data, len, 0, 0)
    206 static usbd_status kue_setword	__P((struct kue_softc *, u_int8_t, u_int16_t));
    207 static int kue_load_fw		__P((struct kue_softc *));
    208 
    209 #if defined(__FreeBSD__)
    210 #ifndef lint
    211 static const char rcsid[] =
    212   "$FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $";
    213 #endif
    214 
    215 static void kue_rxstart		__P((struct ifnet *));
    216 static void kue_shutdown	__P((device_t));
    217 
    218 static struct usb_qdat kue_qdat;
    219 
    220 static device_method_t kue_methods[] = {
    221 	/* Device interface */
    222 	DEVMETHOD(device_probe,		kue_match),
    223 	DEVMETHOD(device_attach,	kue_attach),
    224 	DEVMETHOD(device_detach,	kue_detach),
    225 	DEVMETHOD(device_shutdown,	kue_shutdown),
    226 
    227 	{ 0, 0 }
    228 };
    229 
    230 static driver_t kue_driver = {
    231 	"kue",
    232 	kue_methods,
    233 	sizeof(struct kue_softc)
    234 };
    235 
    236 static devclass_t kue_devclass;
    237 
    238 DRIVER_MODULE(if_kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
    239 
    240 #endif /* __FreeBSD__ */
    241 
    242 /*
    243  * We have a custom do_request function which is almost like the
    244  * regular do_request function, except it has a much longer timeout.
    245  * Why? Because we need to make requests over the control endpoint
    246  * to download the firmware to the device, which can take longer
    247  * than the default timeout.
    248  */
    249 static usbd_status
    250 kue_do_request(dev, req, data, flags, lenp)
    251 	usbd_device_handle	dev;
    252 	usb_device_request_t	*req;
    253 	void			*data;
    254 	u_int16_t		flags;
    255 	u_int32_t		*lenp;
    256 {
    257 	usbd_xfer_handle	xfer;
    258 	usbd_status		err;
    259 
    260 	DPRINTFN(15,("kue_do_request: enter\n"));
    261 
    262 	xfer = usbd_alloc_xfer(dev);
    263 	/* XXX 20000 */
    264 	usbd_setup_default_xfer(xfer, dev, 0, 20000, req,
    265 	    data, UGETW(req->wLength), flags, 0);
    266 	err = usbd_sync_transfer(xfer);
    267 	if (lenp != NULL)
    268 		usbd_get_xfer_status(xfer, NULL, NULL, lenp, NULL);
    269 	usbd_free_xfer(xfer);
    270 
    271 	return (err);
    272 }
    273 
    274 static usbd_status
    275 kue_setword(sc, breq, word)
    276 	struct kue_softc	*sc;
    277 	u_int8_t		breq;
    278 	u_int16_t		word;
    279 {
    280 	usb_device_request_t	req;
    281 	usbd_status		err;
    282 	int			s;
    283 
    284 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    285 
    286 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    287 	req.bRequest = breq;
    288 	USETW(req.wValue, word);
    289 	USETW(req.wIndex, 0);
    290 	USETW(req.wLength, 0);
    291 
    292 	s = splusb();
    293 	err = kue_do_request(sc->kue_udev, &req, NULL, sc->kue_xfer_flags, 0);
    294 	splx(s);
    295 
    296 	return (err);
    297 }
    298 
    299 static usbd_status
    300 kue_ctl_l(sc, rw, breq, val, data, len, flags, lenp)
    301 	struct kue_softc	*sc;
    302 	int			rw;
    303 	u_int8_t		breq;
    304 	u_int16_t		val;
    305 	char			*data;
    306 	u_int32_t		len;
    307 	u_int32_t		flags;
    308 	u_int32_t		*lenp;
    309 {
    310 	usb_device_request_t	req;
    311 	usbd_status		err;
    312 	int			s;
    313 
    314 	DPRINTFN(10,("%s: %s: enter, len=%d\n", USBDEVNAME(sc->kue_dev),
    315 		     __FUNCTION__, len));
    316 
    317 	if (rw == KUE_CTL_WRITE)
    318 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    319 	else
    320 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    321 
    322 	req.bRequest = breq;
    323 	USETW(req.wValue, val);
    324 	USETW(req.wIndex, 0);
    325 	USETW(req.wLength, len);
    326 
    327 	s = splusb();
    328 	err = kue_do_request(sc->kue_udev, &req, data,
    329 		  sc->kue_xfer_flags | flags, lenp);
    330 	splx(s);
    331 
    332 	return (err);
    333 }
    334 
    335 static int
    336 kue_load_fw(sc)
    337 	struct kue_softc	*sc;
    338 {
    339 	usbd_status		err;
    340 	u_char			eaddr[ETHER_ADDR_LEN];
    341 	u_int32_t		alen;
    342 
    343 	DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    344 
    345 	/*
    346 	 * First, check if we even need to load the firmware.
    347 	 * If the device was still attached when the system was
    348 	 * rebooted, it may already have firmware loaded in it.
    349 	 * If this is the case, we don't need to do it again.
    350 	 * And in fact, if we try to load it again, we'll hang,
    351 	 * so we have to avoid this condition if we don't want
    352 	 * to look stupid.
    353 	 *
    354 	 * We can test this quickly by trying to read the MAC
    355 	 * address; if this fails to return any data, the firmware
    356 	 * needs to be reloaded, otherwise the device is already
    357 	 * operational and we can just return.
    358 	 */
    359 	err = kue_ctl_l(sc, KUE_CTL_READ, KUE_CMD_GET_MAC, 0, (char *)&eaddr,
    360 		  ETHER_ADDR_LEN, USBD_SHORT_XFER_OK, &alen);
    361 
    362 	if (err) {
    363 		printf("%s: kue_load_fw: failed to read MAC: %s\n",
    364 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    365 			return (EIO);
    366 	}
    367 
    368 	if (alen == ETHER_ADDR_LEN) {
    369 		printf("%s: warm boot, no firmware download\n",
    370 		       USBDEVNAME(sc->kue_dev));
    371 		return (0);
    372 	}
    373 
    374 	printf("%s: cold boot, downloading firmware\n",
    375 	       USBDEVNAME(sc->kue_dev));
    376 
    377 	/* Load code segment */
    378 	DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
    379 		    USBDEVNAME(sc->kue_dev)));
    380 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    381 	    0, kue_code_seg, sizeof(kue_code_seg));
    382 	if (err) {
    383 		printf("%s: failed to load code segment: %s\n",
    384 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    385 			return (EIO);
    386 	}
    387 
    388 	/* Load fixup segment */
    389 	DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
    390 		    USBDEVNAME(sc->kue_dev)));
    391 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    392 	    0, kue_fix_seg, sizeof(kue_fix_seg));
    393 	if (err) {
    394 		printf("%s: failed to load fixup segment: %s\n",
    395 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    396 			return (EIO);
    397 	}
    398 
    399 	/* Send trigger command. */
    400 	DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
    401 		    USBDEVNAME(sc->kue_dev)));
    402 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
    403 	    0, kue_trig_seg, sizeof(kue_trig_seg));
    404 	if (err) {
    405 		printf("%s: failed to load trigger segment: %s\n",
    406 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
    407 			return (EIO);
    408 	}
    409 
    410 	usbd_delay_ms(sc->kue_udev, 10);
    411 
    412 	/*
    413 	 * Reload device descriptor.
    414 	 * Why? The chip without the firmware loaded returns
    415 	 * one revision code. The chip with the firmware
    416 	 * loaded and running returns a *different* revision
    417 	 * code. This confuses the quirk mechanism, which is
    418 	 * dependent on the revision data.
    419 	 */
    420 	(void)usbd_reload_device_desc(sc->kue_udev);
    421 
    422 	DPRINTFN(1,("%s: %s: done\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    423 
    424 	/* Reset the adapter. */
    425 	kue_reset(sc);
    426 
    427 	return (0);
    428 }
    429 
    430 static void
    431 kue_setmulti(sc)
    432 	struct kue_softc	*sc;
    433 {
    434 	struct ifnet		*ifp = GET_IFP(sc);
    435 #if defined(__FreeBSD__)
    436 	struct ifmultiaddr	*ifma;
    437 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    438 	struct ether_multi	*enm;
    439 	struct ether_multistep	step;
    440 #endif
    441 	int			i;
    442 
    443 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    444 
    445 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    446 		sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
    447 		sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
    448 		kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
    449 		return;
    450 	}
    451 
    452 	sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
    453 
    454 	i = 0;
    455 #if defined(__FreeBSD__)
    456 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
    457 	    ifma = ifma->ifma_link.le_next) {
    458 		if (ifma->ifma_addr->sa_family != AF_LINK)
    459 			continue;
    460 		/*
    461 		 * If there are too many addresses for the
    462 		 * internal filter, switch over to allmulti mode.
    463 		 */
    464 		if (i == KUE_MCFILTCNT(sc))
    465 			break;
    466 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
    467 		    KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
    468 		i++;
    469 	}
    470 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    471 	ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
    472 	while (enm != NULL) {
    473 		if (i == KUE_MCFILTCNT(sc))
    474 			break;
    475 #if 0
    476 		if (memcmp(enm->enm_addrlo,
    477 			   enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
    478 			ifp->if_flags |= IFF_ALLMULTI;
    479 			/* XXX what now? */
    480 			return;
    481 		}
    482 #endif
    483 		memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
    484 		ETHER_NEXT_MULTI(step, enm);
    485 		i++;
    486 	}
    487 #endif
    488 
    489 	if (i == KUE_MCFILTCNT(sc))
    490 		sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
    491 	else {
    492 		sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
    493 		kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
    494 		    i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
    495 	}
    496 
    497 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
    498 }
    499 
    500 /*
    501  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
    502  * done after the firmware is loaded into the adapter in order to
    503  * bring it into proper operation.
    504  */
    505 static void
    506 kue_reset(sc)
    507 	struct kue_softc	*sc;
    508 {
    509 	usbd_status		err;
    510 
    511 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    512 
    513 	err = usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0);
    514 	if (err)
    515 		printf("%s: reset failed\n", USBDEVNAME(sc->kue_dev));
    516 
    517 	/* Wait a little while for the chip to get its brains in order. */
    518 	usbd_delay_ms(sc->kue_udev, 10);
    519 }
    520 
    521 /*
    522  * Probe for a KLSI chip.
    523  */
    524 USB_MATCH(kue)
    525 {
    526 	USB_MATCH_START(kue, uaa);
    527 	struct kue_type			*t;
    528 
    529 	DPRINTFN(25,("kue_match: enter\n"));
    530 
    531 	if (uaa->iface != NULL)
    532 		return (UMATCH_NONE);
    533 
    534 	for (t = kue_devs; t->kue_vid != 0; t++)
    535 		if (uaa->vendor == t->kue_vid && uaa->product == t->kue_did)
    536 			return (UMATCH_VENDOR_PRODUCT);
    537 
    538 	return (UMATCH_NONE);
    539 }
    540 
    541 /*
    542  * Attach the interface. Allocate softc structures, do
    543  * setup and ethernet/BPF attach.
    544  */
    545 USB_ATTACH(kue)
    546 {
    547 	USB_ATTACH_START(kue, sc, uaa);
    548 	char			devinfo[1024];
    549 	int			s;
    550 	struct ifnet		*ifp;
    551 	usbd_device_handle	dev = uaa->device;
    552 	usbd_interface_handle	iface;
    553 	usbd_status		err;
    554 	usb_interface_descriptor_t	*id;
    555 	usb_endpoint_descriptor_t	*ed;
    556 	int			i;
    557 
    558 #ifdef __FreeBSD__
    559 	bzero(sc, sizeof(struct kue_softc));
    560 #endif
    561 
    562 	DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
    563 
    564 	usbd_devinfo(dev, 0, devinfo);
    565 	USB_ATTACH_SETUP;
    566 	printf("%s: %s\n", USBDEVNAME(sc->kue_dev), devinfo);
    567 
    568 	err = usbd_set_config_no(dev, KUE_CONFIG_NO, 0);
    569 	if (err) {
    570 		printf("%s: setting config no failed\n",
    571 		    USBDEVNAME(sc->kue_dev));
    572 		USB_ATTACH_ERROR_RETURN;
    573 	}
    574 
    575 	sc->kue_udev = dev;
    576 	sc->kue_product = uaa->product;
    577 	sc->kue_vendor = uaa->vendor;
    578 
    579 	/* Load the firmware into the NIC. */
    580 	if (kue_load_fw(sc)) {
    581 		printf("%s: loading firmware failed\n",
    582 		    USBDEVNAME(sc->kue_dev));
    583 		USB_ATTACH_ERROR_RETURN;
    584 	}
    585 
    586 	err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
    587 	if (err) {
    588 		printf("%s: getting interface handle failed\n",
    589 		    USBDEVNAME(sc->kue_dev));
    590 		USB_ATTACH_ERROR_RETURN;
    591 	}
    592 
    593 	sc->kue_iface = iface;
    594 	id = usbd_get_interface_descriptor(iface);
    595 
    596 	/* Find endpoints. */
    597 	for (i = 0; i < id->bNumEndpoints; i++) {
    598 		ed = usbd_interface2endpoint_descriptor(iface, i);
    599 		if (ed == NULL) {
    600 			printf("%s: couldn't get ep %d\n",
    601 			    USBDEVNAME(sc->kue_dev), i);
    602 			splx(s);
    603 			USB_ATTACH_ERROR_RETURN;
    604 		}
    605 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    606 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    607 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
    608 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    609 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    610 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
    611 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    612 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    613 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
    614 		}
    615 	}
    616 
    617 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
    618 		printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev));
    619 		USB_ATTACH_ERROR_RETURN;
    620 	}
    621 
    622 	/* Read ethernet descriptor */
    623 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
    624 	    0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
    625 	if (err) {
    626 		printf("%s: could not read Ethernet descriptor\n",
    627 		    USBDEVNAME(sc->kue_dev));
    628 		USB_ATTACH_ERROR_RETURN;
    629 	}
    630 
    631 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
    632 	    M_USBDEV, M_NOWAIT);
    633 	if (sc->kue_mcfilters == NULL) {
    634 		printf("%s: no memory for multicast filter buffer\n",
    635 		    USBDEVNAME(sc->kue_dev));
    636 		USB_ATTACH_ERROR_RETURN;
    637 	}
    638 
    639 	sc->kue_xfer_flags = USBD_NO_TSLEEP;
    640 
    641 	s = splimp();
    642 
    643 	/*
    644 	 * A KLSI chip was detected. Inform the world.
    645 	 */
    646 #if defined(__FreeBSD__)
    647 	printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->kue_dev),
    648 	    sc->kue_desc.kue_macaddr, ":");
    649 
    650 	bcopy(sc->kue_desc.kue_macaddr,
    651 	    (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
    652 
    653 	ifp = GET_IFP(sc);
    654 	ifp->if_softc = sc;
    655 	ifp->if_unit = sc->kue_unit;
    656 	ifp->if_name = "kue";
    657 	ifp->if_mtu = ETHERMTU;
    658 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    659 	ifp->if_ioctl = kue_ioctl;
    660 	ifp->if_output = ether_output;
    661 	ifp->if_start = kue_start;
    662 	ifp->if_watchdog = kue_watchdog;
    663 	ifp->if_init = kue_init;
    664 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    665 
    666 	kue_qdat.ifp = ifp;
    667 	kue_qdat.if_rxstart = kue_rxstart;
    668 
    669 	/*
    670 	 * Call MI attach routines.
    671 	 */
    672 	if_attach(ifp);
    673 	ether_ifattach(ifp);
    674 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    675 	usb_register_netisr();
    676 
    677 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    678 
    679 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->kue_dev),
    680 	    ether_sprintf(sc->kue_desc.kue_macaddr));
    681 
    682 	/* Initialize interface info.*/
    683 	ifp = GET_IFP(sc);
    684 	ifp->if_softc = sc;
    685 	ifp->if_mtu = ETHERMTU;
    686 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    687 	ifp->if_ioctl = kue_ioctl;
    688 	ifp->if_start = kue_start;
    689 	ifp->if_watchdog = kue_watchdog;
    690 	strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
    691 
    692 	/* Attach the interface. */
    693 	if_attach(ifp);
    694 	ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
    695 
    696 #if NBPFILTER > 0
    697 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    698 		  sizeof(struct ether_header));
    699 #endif
    700 #if NRND > 0
    701 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
    702 	    RND_TYPE_NET, 0);
    703 #endif
    704 
    705 #endif /* __NetBSD__ */
    706 	sc->kue_attached = 1;
    707 	splx(s);
    708 
    709 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
    710 			   USBDEV(sc->kue_dev));
    711 
    712 	USB_ATTACH_SUCCESS_RETURN;
    713 }
    714 
    715 USB_DETACH(kue)
    716 {
    717 	USB_DETACH_START(kue, sc);
    718 	struct ifnet		*ifp = GET_IFP(sc);
    719 	int			s;
    720 
    721 	s = splusb();		/* XXX why? */
    722 
    723 	if (sc->kue_mcfilters != NULL) {
    724 		free(sc->kue_mcfilters, M_USBDEV);
    725 		sc->kue_mcfilters = NULL;
    726 	}
    727 
    728 	if (!sc->kue_attached) {
    729 		/* Detached before attached finished, so just bail out. */
    730 		splx(s);
    731 		return (0);
    732 	}
    733 
    734 	if (ifp->if_flags & IFF_RUNNING)
    735 		kue_stop(sc);
    736 
    737 #if defined(__NetBSD__)
    738 #if NRND > 0
    739 	rnd_detach_source(&sc->rnd_source);
    740 #endif
    741 #if NBPFILTER > 0
    742 	bpfdetach(ifp);
    743 #endif
    744 	ether_ifdetach(ifp);
    745 #endif /* __NetBSD__ */
    746 
    747 	if_detach(ifp);
    748 
    749 #ifdef DIAGNOSTIC
    750 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
    751 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
    752 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
    753 		printf("%s: detach has active endpoints\n",
    754 		       USBDEVNAME(sc->kue_dev));
    755 #endif
    756 
    757 	sc->kue_attached = 0;
    758 	splx(s);
    759 
    760 	return (0);
    761 }
    762 
    763 #if defined(__NetBSD__) || defined(__OpenBSD__)
    764 int
    765 kue_activate(self, act)
    766 	device_ptr_t self;
    767 	enum devact act;
    768 {
    769 	struct kue_softc *sc = (struct kue_softc *)self;
    770 
    771 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    772 
    773 	switch (act) {
    774 	case DVACT_ACTIVATE:
    775 		return (EOPNOTSUPP);
    776 		break;
    777 
    778 	case DVACT_DEACTIVATE:
    779 		/* Deactivate the interface. */
    780 		if_deactivate(&sc->kue_ec.ec_if);
    781 		sc->kue_dying = 1;
    782 		break;
    783 	}
    784 	return (0);
    785 }
    786 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    787 
    788 /*
    789  * Initialize an RX descriptor and attach an MBUF cluster.
    790  */
    791 static int
    792 kue_newbuf(sc, c, m)
    793 	struct kue_softc	*sc;
    794 	struct kue_chain	*c;
    795 	struct mbuf		*m;
    796 {
    797 	struct mbuf		*m_new = NULL;
    798 
    799 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
    800 
    801 	if (m == NULL) {
    802 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    803 		if (m_new == NULL) {
    804 			printf("%s: no memory for rx list "
    805 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
    806 			return (ENOBUFS);
    807 		}
    808 
    809 		MCLGET(m_new, M_DONTWAIT);
    810 		if (!(m_new->m_flags & M_EXT)) {
    811 			printf("%s: no memory for rx list "
    812 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
    813 			m_freem(m_new);
    814 			return (ENOBUFS);
    815 		}
    816 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    817 	} else {
    818 		m_new = m;
    819 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    820 		m_new->m_data = m_new->m_ext.ext_buf;
    821 	}
    822 
    823 	c->kue_mbuf = m_new;
    824 
    825 	return (0);
    826 }
    827 
    828 static int
    829 kue_rx_list_init(sc)
    830 	struct kue_softc	*sc;
    831 {
    832 	struct kue_cdata	*cd;
    833 	struct kue_chain	*c;
    834 	int			i;
    835 
    836 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    837 
    838 	cd = &sc->kue_cdata;
    839 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
    840 		c = &cd->kue_rx_chain[i];
    841 		c->kue_sc = sc;
    842 		c->kue_idx = i;
    843 		if (kue_newbuf(sc, c, NULL) == ENOBUFS)
    844 			return (ENOBUFS);
    845 		if (c->kue_xfer == NULL) {
    846 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    847 			if (c->kue_xfer == NULL)
    848 				return (ENOBUFS);
    849 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    850 			if (c->kue_buf == NULL)
    851 				return (ENOBUFS); /* XXX free xfer */
    852 		}
    853 	}
    854 
    855 	return (0);
    856 }
    857 
    858 static int
    859 kue_tx_list_init(sc)
    860 	struct kue_softc	*sc;
    861 {
    862 	struct kue_cdata	*cd;
    863 	struct kue_chain	*c;
    864 	int			i;
    865 
    866 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __FUNCTION__));
    867 
    868 	cd = &sc->kue_cdata;
    869 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
    870 		c = &cd->kue_tx_chain[i];
    871 		c->kue_sc = sc;
    872 		c->kue_idx = i;
    873 		c->kue_mbuf = NULL;
    874 		if (c->kue_xfer == NULL) {
    875 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
    876 			if (c->kue_xfer == NULL)
    877 				return (ENOBUFS);
    878 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
    879 			if (c->kue_buf == NULL)
    880 				return (ENOBUFS);
    881 		}
    882 	}
    883 
    884 	return (0);
    885 }
    886 
    887 #ifdef __FreeBSD__
    888 static void
    889 kue_rxstart(ifp)
    890 	struct ifnet		*ifp;
    891 {
    892 	struct kue_softc	*sc;
    893 	struct kue_chain	*c;
    894 
    895 	sc = ifp->if_softc;
    896 	c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
    897 
    898 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
    899 		ifp->if_ierrors++;
    900 		return;
    901 	}
    902 
    903 	/* Setup new transfer. */
    904 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
    905 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    906 	    USBD_NO_TIMEOUT, kue_rxeof);
    907 	usbd_transfer(c->kue_xfer);
    908 }
    909 #endif
    910 
    911 /*
    912  * A frame has been uploaded: pass the resulting mbuf chain up to
    913  * the higher level protocols.
    914  */
    915 static void
    916 kue_rxeof(xfer, priv, status)
    917 	usbd_xfer_handle	xfer;
    918 	usbd_private_handle	priv;
    919 	usbd_status		status;
    920 {
    921 	struct kue_chain	*c = priv;
    922 	struct kue_softc	*sc = c->kue_sc;
    923 	struct ifnet		*ifp = GET_IFP(sc);
    924 	struct mbuf		*m;
    925 	int			total_len = 0;
    926 #if defined(__NetBSD__) || defined(__OpenBSD__)
    927 	int			s;
    928 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    929 
    930 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
    931 		     __FUNCTION__, status));
    932 
    933 	if (sc->kue_dying)
    934 		return;
    935 
    936 	if (!(ifp->if_flags & IFF_RUNNING))
    937 		return;
    938 
    939 	if (status != USBD_NORMAL_COMPLETION) {
    940 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    941 			return;
    942 		sc->kue_rx_errs++;
    943 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
    944 			printf("%s: %u usb errors on rx: %s\n",
    945 			    USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
    946 			    usbd_errstr(status));
    947 			sc->kue_rx_errs = 0;
    948 		}
    949 		if (status == USBD_STALLED)
    950 			usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
    951 		goto done;
    952 	}
    953 
    954 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    955 
    956 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
    957 		     __FUNCTION__, total_len,
    958 		     UGETW(mtod(c->kue_mbuf, u_int8_t *))));
    959 
    960 	m = c->kue_mbuf;
    961 	if (total_len <= 1)
    962 		goto done;
    963 
    964 	/* copy data to mbuf */
    965 	memcpy(mtod(m, char*), c->kue_buf, total_len);
    966 
    967 	/* No errors; receive the packet. */
    968 	total_len = UGETW(mtod(m, u_int8_t *));
    969 	m_adj(m, sizeof(u_int16_t));
    970 
    971 	if (total_len < sizeof(struct ether_header)) {
    972 		ifp->if_ierrors++;
    973 		goto done;
    974 	}
    975 
    976 	ifp->if_ipackets++;
    977 	m->m_pkthdr.len = m->m_len = total_len;
    978 
    979 #if defined(__FreeBSD__)
    980 	m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
    981 	/* Put the packet on the special USB input queue. */
    982 	usb_ether_input(m);
    983 
    984 	return;
    985 
    986 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    987 	m->m_pkthdr.rcvif = ifp;
    988 
    989 	s = splimp();
    990 
    991 	/* XXX ugly */
    992 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
    993 		ifp->if_ierrors++;
    994 		goto done1;
    995 	}
    996 
    997 #if NBPFILTER > 0
    998 	/*
    999 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1000 	 * don't pass it up to the ether_input() layer unless it's
   1001 	 * a broadcast packet, multicast packet, matches our ethernet
   1002 	 * address or the interface is in promiscuous mode.
   1003 	 */
   1004 	if (ifp->if_bpf) {
   1005 		struct ether_header *eh = mtod(m, struct ether_header *);
   1006 		BPF_MTAP(ifp, m);
   1007 		if ((ifp->if_flags & IFF_PROMISC) &&
   1008 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
   1009 			   ETHER_ADDR_LEN) &&
   1010 		    !(eh->ether_dhost[0] & 1)) {
   1011 			m_freem(m);
   1012 			goto done1;
   1013 		}
   1014 	}
   1015 #endif
   1016 
   1017 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
   1018 		    __FUNCTION__, m->m_len));
   1019 	(*ifp->if_input)(ifp, m);
   1020  done1:
   1021 	splx(s);
   1022 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1023 
   1024  done:
   1025 
   1026 	/* Setup new transfer. */
   1027 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
   1028 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1029 	    USBD_NO_TIMEOUT, kue_rxeof);
   1030 	usbd_transfer(c->kue_xfer);
   1031 
   1032 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
   1033 		    __FUNCTION__));
   1034 }
   1035 
   1036 /*
   1037  * A frame was downloaded to the chip. It's safe for us to clean up
   1038  * the list buffers.
   1039  */
   1040 
   1041 static void
   1042 kue_txeof(xfer, priv, status)
   1043 	usbd_xfer_handle	xfer;
   1044 	usbd_private_handle	priv;
   1045 	usbd_status		status;
   1046 {
   1047 	struct kue_chain	*c = priv;
   1048 	struct kue_softc	*sc = c->kue_sc;
   1049 	struct ifnet		*ifp = GET_IFP(sc);
   1050 	int			s;
   1051 
   1052 	if (sc->kue_dying)
   1053 		return;
   1054 
   1055 	s = splimp();
   1056 
   1057 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
   1058 		    __FUNCTION__, status));
   1059 
   1060 	ifp->if_timer = 0;
   1061 	ifp->if_flags &= ~IFF_OACTIVE;
   1062 
   1063 	if (status != USBD_NORMAL_COMPLETION) {
   1064 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1065 			splx(s);
   1066 			return;
   1067 		}
   1068 		ifp->if_oerrors++;
   1069 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
   1070 		    usbd_errstr(status));
   1071 		if (status == USBD_STALLED)
   1072 			usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
   1073 		splx(s);
   1074 		return;
   1075 	}
   1076 
   1077 	ifp->if_opackets++;
   1078 
   1079 #if defined(__FreeBSD__)
   1080 	c->kue_mbuf->m_pkthdr.rcvif = ifp;
   1081 	usb_tx_done(c->kue_mbuf);
   1082 	c->kue_mbuf = NULL;
   1083 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1084 	m_freem(c->kue_mbuf);
   1085 	c->kue_mbuf = NULL;
   1086 
   1087 	if (ifp->if_snd.ifq_head != NULL)
   1088 		kue_start(ifp);
   1089 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1090 
   1091 	splx(s);
   1092 }
   1093 
   1094 static int
   1095 kue_send(sc, m, idx)
   1096 	struct kue_softc	*sc;
   1097 	struct mbuf		*m;
   1098 	int			idx;
   1099 {
   1100 	int			total_len;
   1101 	struct kue_chain	*c;
   1102 	usbd_status		err;
   1103 
   1104 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1105 
   1106 	c = &sc->kue_cdata.kue_tx_chain[idx];
   1107 
   1108 	/*
   1109 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1110 	 * bytes at the beginning to hold the frame length.
   1111 	 */
   1112 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
   1113 	c->kue_mbuf = m;
   1114 
   1115 	total_len = m->m_pkthdr.len + 2;
   1116 	/* XXX what's this? */
   1117 	total_len += 64 - (total_len % 64);
   1118 
   1119 	/* Frame length is specified in the first 2 bytes of the buffer. */
   1120 	c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1121 	c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1122 
   1123 	/* XXX 10000 */
   1124 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
   1125 	    c, c->kue_buf, total_len, USBD_NO_COPY, 10000, kue_txeof);
   1126 
   1127 	/* Transmit */
   1128 	err = usbd_transfer(c->kue_xfer);
   1129 	if (err != USBD_IN_PROGRESS) {
   1130 		kue_stop(sc);
   1131 		return (EIO);
   1132 	}
   1133 
   1134 	sc->kue_cdata.kue_tx_cnt++;
   1135 
   1136 	return (0);
   1137 }
   1138 
   1139 static void
   1140 kue_start(ifp)
   1141 	struct ifnet		*ifp;
   1142 {
   1143 	struct kue_softc	*sc = ifp->if_softc;
   1144 	struct mbuf		*m_head = NULL;
   1145 
   1146 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1147 
   1148 	if (sc->kue_dying)
   1149 		return;
   1150 
   1151 	if (ifp->if_flags & IFF_OACTIVE)
   1152 		return;
   1153 
   1154 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1155 	if (m_head == NULL)
   1156 		return;
   1157 
   1158 	if (kue_send(sc, m_head, 0)) {
   1159 		IF_PREPEND(&ifp->if_snd, m_head);
   1160 		ifp->if_flags |= IFF_OACTIVE;
   1161 		return;
   1162 	}
   1163 
   1164 #if NBPFILTER > 0
   1165 	/*
   1166 	 * If there's a BPF listener, bounce a copy of this frame
   1167 	 * to him.
   1168 	 */
   1169 	if (ifp->if_bpf)
   1170 		BPF_MTAP(ifp, m_head);
   1171 #endif
   1172 
   1173 	ifp->if_flags |= IFF_OACTIVE;
   1174 
   1175 	/*
   1176 	 * Set a timeout in case the chip goes out to lunch.
   1177 	 */
   1178 	ifp->if_timer = 5;
   1179 }
   1180 
   1181 static void
   1182 kue_init(xsc)
   1183 	void			*xsc;
   1184 {
   1185 	struct kue_softc	*sc = xsc;
   1186 	struct ifnet		*ifp = GET_IFP(sc);
   1187 	int			s;
   1188 	u_char			*eaddr;
   1189 
   1190 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1191 
   1192 	if (ifp->if_flags & IFF_RUNNING)
   1193 		return;
   1194 
   1195 	s = splimp();
   1196 
   1197 #if defined(__FreeBSD__)
   1198 	eaddr = sc->arpcom.ac_enaddr;
   1199 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1200 	eaddr = LLADDR(ifp->if_sadl);
   1201 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1202 	/* Set MAC address */
   1203 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
   1204 
   1205 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
   1206 
   1207 	 /* If we want promiscuous mode, set the allframes bit. */
   1208 	if (ifp->if_flags & IFF_PROMISC)
   1209 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
   1210 
   1211 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
   1212 
   1213 	/* I'm not sure how to tune these. */
   1214 #if 0
   1215 	/*
   1216 	 * Leave this one alone for now; setting it
   1217 	 * wrong causes lockups on some machines/controllers.
   1218 	 */
   1219 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
   1220 #endif
   1221 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
   1222 
   1223 	/* Init TX ring. */
   1224 	if (kue_tx_list_init(sc) == ENOBUFS) {
   1225 		printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
   1226 		splx(s);
   1227 		return;
   1228 	}
   1229 
   1230 	/* Init RX ring. */
   1231 	if (kue_rx_list_init(sc) == ENOBUFS) {
   1232 		printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
   1233 		splx(s);
   1234 		return;
   1235 	}
   1236 
   1237 	/* Load the multicast filter. */
   1238 	kue_setmulti(sc);
   1239 
   1240 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
   1241 		if (kue_open_pipes(sc)) {
   1242 			splx(s);
   1243 			return;
   1244 		}
   1245 	}
   1246 
   1247 	ifp->if_flags |= IFF_RUNNING;
   1248 	ifp->if_flags &= ~IFF_OACTIVE;
   1249 
   1250 	splx(s);
   1251 }
   1252 
   1253 static int
   1254 kue_open_pipes(sc)
   1255 	struct kue_softc	*sc;
   1256 {
   1257 	usbd_status		err;
   1258 	struct kue_chain	*c;
   1259 	int			i;
   1260 
   1261 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1262 
   1263 	/* Open RX and TX pipes. */
   1264 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
   1265 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
   1266 	if (err) {
   1267 		printf("%s: open rx pipe failed: %s\n",
   1268 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1269 		return (EIO);
   1270 	}
   1271 
   1272 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
   1273 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
   1274 	if (err) {
   1275 		printf("%s: open tx pipe failed: %s\n",
   1276 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1277 		return (EIO);
   1278 	}
   1279 
   1280 	/* Start up the receive pipe. */
   1281 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1282 		c = &sc->kue_cdata.kue_rx_chain[i];
   1283 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
   1284 		    c, c->kue_buf, KUE_BUFSZ,
   1285 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1286 		    kue_rxeof);
   1287 		usbd_transfer(c->kue_xfer);
   1288 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
   1289 			    __FUNCTION__));
   1290 	}
   1291 
   1292 	return (0);
   1293 }
   1294 
   1295 static int
   1296 kue_ioctl(ifp, command, data)
   1297 	struct ifnet		*ifp;
   1298 	u_long			command;
   1299 	caddr_t			data;
   1300 {
   1301 	struct kue_softc	*sc = ifp->if_softc;
   1302 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1303 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1304 	struct ifreq		*ifr = (struct ifreq *)data;
   1305 #endif
   1306 	int			s, error = 0;
   1307 
   1308 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1309 
   1310 	if (sc->kue_dying)
   1311 		return (EIO);
   1312 
   1313 	s = splimp();
   1314 
   1315 	switch(command) {
   1316 #if defined(__FreeBSD__)
   1317 	case SIOCSIFADDR:
   1318 	case SIOCGIFADDR:
   1319 	case SIOCSIFMTU:
   1320 		error = ether_ioctl(ifp, command, data);
   1321 		break;
   1322 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1323 	case SIOCSIFADDR:
   1324 		ifp->if_flags |= IFF_UP;
   1325 		kue_init(sc);
   1326 
   1327 		switch (ifa->ifa_addr->sa_family) {
   1328 #ifdef INET
   1329 		case AF_INET:
   1330 			arp_ifinit(ifp, ifa);
   1331 			break;
   1332 #endif /* INET */
   1333 #ifdef NS
   1334 		case AF_NS:
   1335 		    {
   1336 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1337 
   1338 			if (ns_nullhost(*ina))
   1339 				ina->x_host = *(union ns_host *)
   1340 					LLADDR(ifp->if_sadl);
   1341 			else
   1342 				memcpy(LLADDR(ifp->if_sadl),
   1343 				       ina->x_host.c_host,
   1344 				       ifp->if_addrlen);
   1345 			break;
   1346 		    }
   1347 #endif /* NS */
   1348 		}
   1349 		break;
   1350 
   1351 	case SIOCSIFMTU:
   1352 		if (ifr->ifr_mtu > ETHERMTU)
   1353 			error = EINVAL;
   1354 		else
   1355 			ifp->if_mtu = ifr->ifr_mtu;
   1356 		break;
   1357 
   1358 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1359 
   1360 	case SIOCSIFFLAGS:
   1361 		if (ifp->if_flags & IFF_UP) {
   1362 			if (ifp->if_flags & IFF_RUNNING &&
   1363 			    ifp->if_flags & IFF_PROMISC &&
   1364 			    !(sc->kue_if_flags & IFF_PROMISC)) {
   1365 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
   1366 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1367 				    sc->kue_rxfilt);
   1368 			} else if (ifp->if_flags & IFF_RUNNING &&
   1369 			    !(ifp->if_flags & IFF_PROMISC) &&
   1370 			    sc->kue_if_flags & IFF_PROMISC) {
   1371 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
   1372 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
   1373 				    sc->kue_rxfilt);
   1374 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1375 				kue_init(sc);
   1376 		} else {
   1377 			if (ifp->if_flags & IFF_RUNNING)
   1378 				kue_stop(sc);
   1379 		}
   1380 		sc->kue_if_flags = ifp->if_flags;
   1381 		error = 0;
   1382 		break;
   1383 	case SIOCADDMULTI:
   1384 	case SIOCDELMULTI:
   1385 		kue_setmulti(sc);
   1386 		error = 0;
   1387 		break;
   1388 	default:
   1389 		error = EINVAL;
   1390 		break;
   1391 	}
   1392 
   1393 	splx(s);
   1394 
   1395 	return (error);
   1396 }
   1397 
   1398 static void
   1399 kue_watchdog(ifp)
   1400 	struct ifnet		*ifp;
   1401 {
   1402 	struct kue_softc	*sc = ifp->if_softc;
   1403 
   1404 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1405 
   1406 	if (sc->kue_dying)
   1407 		return;
   1408 
   1409 	ifp->if_oerrors++;
   1410 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
   1411 
   1412 	/*
   1413 	 * The polling business is a kludge to avoid allowing the
   1414 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1415 	 * kill us since the watchdog routine is invoked from
   1416 	 * interrupt context.
   1417 	 */
   1418 	usbd_set_polling(sc->kue_udev, 1);
   1419 	kue_stop(sc);
   1420 	kue_init(sc);
   1421 	usbd_set_polling(sc->kue_udev, 0);
   1422 
   1423 	if (ifp->if_snd.ifq_head != NULL)
   1424 		kue_start(ifp);
   1425 }
   1426 
   1427 /*
   1428  * Stop the adapter and free any mbufs allocated to the
   1429  * RX and TX lists.
   1430  */
   1431 static void
   1432 kue_stop(sc)
   1433 	struct kue_softc	*sc;
   1434 {
   1435 	usbd_status		err;
   1436 	struct ifnet		*ifp;
   1437 	int			i;
   1438 
   1439 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__FUNCTION__));
   1440 
   1441 	ifp = GET_IFP(sc);
   1442 	ifp->if_timer = 0;
   1443 
   1444 	/* Stop transfers. */
   1445 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
   1446 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1447 		if (err) {
   1448 			printf("%s: abort rx pipe failed: %s\n",
   1449 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1450 		}
   1451 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
   1452 		if (err) {
   1453 			printf("%s: close rx pipe failed: %s\n",
   1454 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1455 		}
   1456 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
   1457 	}
   1458 
   1459 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
   1460 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1461 		if (err) {
   1462 			printf("%s: abort tx pipe failed: %s\n",
   1463 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1464 		}
   1465 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
   1466 		if (err) {
   1467 			printf("%s: close tx pipe failed: %s\n",
   1468 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1469 		}
   1470 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
   1471 	}
   1472 
   1473 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
   1474 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1475 		if (err) {
   1476 			printf("%s: abort intr pipe failed: %s\n",
   1477 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1478 		}
   1479 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
   1480 		if (err) {
   1481 			printf("%s: close intr pipe failed: %s\n",
   1482 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
   1483 		}
   1484 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
   1485 	}
   1486 
   1487 	/* Free RX resources. */
   1488 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
   1489 		if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
   1490 			m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
   1491 			sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
   1492 		}
   1493 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
   1494 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
   1495 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
   1496 		}
   1497 	}
   1498 
   1499 	/* Free TX resources. */
   1500 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
   1501 		if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
   1502 			m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
   1503 			sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
   1504 		}
   1505 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
   1506 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
   1507 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
   1508 		}
   1509 	}
   1510 
   1511 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1512 }
   1513 
   1514 #ifdef __FreeBSD__
   1515 /*
   1516  * Stop all chip I/O so that the kernel's probe routines don't
   1517  * get confused by errant DMAs when rebooting.
   1518  */
   1519 static void
   1520 kue_shutdown(dev)
   1521 	device_t		dev;
   1522 {
   1523 	struct kue_softc	*sc;
   1524 
   1525 	sc = device_get_softc(dev);
   1526 
   1527 	kue_stop(sc);
   1528 }
   1529 #endif
   1530