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