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