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