Home | History | Annotate | Line # | Download | only in usb
if_upl.c revision 1.3
      1 /*	$NetBSD: if_upl.c,v 1.3 2000/04/27 15:26:46 augustss Exp $	*/
      2 /*
      3  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Lennart Augustsson (lennart (at) augustsson.net) at
      8  * Carlstedt Research & Technology.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Prolific PL2301/PL2302 driver
     41  */
     42 
     43 #include "opt_inet.h"
     44 #include "opt_ns.h"
     45 #include "bpfilter.h"
     46 #include "rnd.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/callout.h>
     51 #include <sys/sockio.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/malloc.h>
     54 #include <sys/kernel.h>
     55 #include <sys/socket.h>
     56 
     57 #include <sys/device.h>
     58 #if NRND > 0
     59 #include <sys/rnd.h>
     60 #endif
     61 
     62 #include <net/if.h>
     63 #include <net/if_types.h>
     64 #include <net/if_dl.h>
     65 #include <net/netisr.h>
     66 
     67 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
     68 
     69 #if NBPFILTER > 0
     70 #include <net/bpf.h>
     71 #endif
     72 
     73 #ifdef INET
     74 #include <netinet/in.h>
     75 #include <netinet/in_var.h>
     76 #include <netinet/if_inarp.h>
     77 #endif
     78 
     79 #ifdef NS
     80 #include <netns/ns.h>
     81 #include <netns/ns_if.h>
     82 #endif
     83 
     84 #include <dev/usb/usb.h>
     85 #include <dev/usb/usbdi.h>
     86 #include <dev/usb/usbdi_util.h>
     87 #include <dev/usb/usbdevs.h>
     88 
     89 /*
     90  * 7  6  5  4  3  2  1  0
     91  *  tx rx 1  0
     92  * 1110 0000 rxdata
     93  * 1010 0000 idle
     94  * 0010 0000 tx over
     95  * 0110      tx over + rxd
     96  */
     97 
     98 #define UPL_RXDATA		0x40
     99 #define UPL_TXOK		0x80
    100 
    101 #define UPL_INTR_PKTLEN		1
    102 
    103 #define UPL_CONFIG_NO		1
    104 #define UPL_IFACE_IDX		0
    105 
    106 /***/
    107 
    108 #define UPL_INTR_INTERVAL	20
    109 
    110 #define UPL_BUFSZ		1024
    111 
    112 #define UPL_RX_FRAMES		1
    113 #define UPL_TX_FRAMES		1
    114 
    115 #define UPL_RX_LIST_CNT		1
    116 #define UPL_TX_LIST_CNT		1
    117 
    118 #define UPL_ENDPT_RX		0x0
    119 #define UPL_ENDPT_TX		0x1
    120 #define UPL_ENDPT_INTR		0x2
    121 #define UPL_ENDPT_MAX		0x3
    122 
    123 struct upl_type {
    124 	u_int16_t		upl_vid;
    125 	u_int16_t		upl_did;
    126 };
    127 
    128 struct upl_softc;
    129 
    130 struct upl_chain {
    131 	struct upl_softc	*upl_sc;
    132 	usbd_xfer_handle	upl_xfer;
    133 	char			*upl_buf;
    134 	struct mbuf		*upl_mbuf;
    135 	int			upl_idx;
    136 };
    137 
    138 struct upl_cdata {
    139 	struct upl_chain	upl_tx_chain[UPL_TX_LIST_CNT];
    140 	struct upl_chain	upl_rx_chain[UPL_RX_LIST_CNT];
    141 	int			upl_tx_prod;
    142 	int			upl_tx_cons;
    143 	int			upl_tx_cnt;
    144 	int			upl_rx_prod;
    145 };
    146 
    147 struct upl_softc {
    148 	USBBASEDEVICE		sc_dev;
    149 
    150 	struct ifnet		sc_if;
    151 #if NRND > 0
    152 	rndsource_element_t	sc_rnd_source;
    153 #endif
    154 
    155 	usb_callout_t		sc_stat_ch;
    156 
    157 	usbd_device_handle	sc_udev;
    158 	usbd_interface_handle	sc_iface;
    159 	u_int16_t		sc_vendor;
    160 	u_int16_t		sc_product;
    161 	int			sc_ed[UPL_ENDPT_MAX];
    162 	usbd_pipe_handle	sc_ep[UPL_ENDPT_MAX];
    163 	struct upl_cdata	sc_cdata;
    164 
    165 	uByte			sc_ibuf;
    166 
    167 	char			sc_dying;
    168 	char			sc_attached;
    169 	u_int			sc_rx_errs;
    170 	struct timeval		sc_rx_notice;
    171 	u_int			sc_intr_errs;
    172 };
    173 
    174 #ifdef UPL_DEBUG
    175 #define DPRINTF(x)	if (upldebug) logprintf x
    176 #define DPRINTFN(n,x)	if (upldebug >= (n)) logprintf x
    177 int	upldebug = 0;
    178 #else
    179 #define DPRINTF(x)
    180 #define DPRINTFN(n,x)
    181 #endif
    182 
    183 /*
    184  * Various supported device vendors/products.
    185  */
    186 Static struct upl_type sc_devs[] = {
    187 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 },
    188 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 },
    189 	{ 0, 0 }
    190 };
    191 
    192 USB_DECLARE_DRIVER(upl);
    193 
    194 Static int upl_openpipes	__P((struct upl_softc *));
    195 Static int upl_tx_list_init	__P((struct upl_softc *));
    196 Static int upl_rx_list_init	__P((struct upl_softc *));
    197 Static int upl_newbuf		__P((struct upl_softc *, struct upl_chain *,
    198 				    struct mbuf *));
    199 Static int upl_send		__P((struct upl_softc *, struct mbuf *, int));
    200 Static void upl_intr		__P((usbd_xfer_handle,
    201 				    usbd_private_handle, usbd_status));
    202 Static void upl_rxeof		__P((usbd_xfer_handle,
    203 				    usbd_private_handle, usbd_status));
    204 Static void upl_txeof		__P((usbd_xfer_handle,
    205 				    usbd_private_handle, usbd_status));
    206 Static void upl_start		__P((struct ifnet *));
    207 Static int upl_ioctl		__P((struct ifnet *, u_long, caddr_t));
    208 Static void upl_init		__P((void *));
    209 Static void upl_stop		__P((struct upl_softc *));
    210 Static void upl_watchdog		__P((struct ifnet *));
    211 
    212 Static int upl_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
    213 			   struct rtentry *));
    214 Static void upl_input __P((struct ifnet *, struct mbuf *));
    215 
    216 /*
    217  * Probe for a Prolific chip.
    218  */
    219 USB_MATCH(upl)
    220 {
    221 	USB_MATCH_START(upl, uaa);
    222 	struct upl_type			*t;
    223 
    224 	if (uaa->iface != NULL)
    225 		return (UMATCH_NONE);
    226 
    227 	for (t = sc_devs; t->upl_vid != 0; t++)
    228 		if (uaa->vendor == t->upl_vid && uaa->product == t->upl_did)
    229 			return (UMATCH_VENDOR_PRODUCT);
    230 
    231 	return (UMATCH_NONE);
    232 }
    233 
    234 USB_ATTACH(upl)
    235 {
    236 	USB_ATTACH_START(upl, sc, uaa);
    237 	char			devinfo[1024];
    238 	int			s;
    239 	usbd_device_handle	dev = uaa->device;
    240 	usbd_interface_handle	iface;
    241 	usbd_status		err;
    242 	struct ifnet		*ifp;
    243 	usb_interface_descriptor_t	*id;
    244 	usb_endpoint_descriptor_t	*ed;
    245 	int			i;
    246 
    247 	DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev));
    248 
    249 	usbd_devinfo(dev, 0, devinfo);
    250 	USB_ATTACH_SETUP;
    251 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    252 
    253 	err = usbd_set_config_no(dev, UPL_CONFIG_NO, 0);
    254 	if (err) {
    255 		printf("%s: setting config no failed\n",
    256 		    USBDEVNAME(sc->sc_dev));
    257 		USB_ATTACH_ERROR_RETURN;
    258 	}
    259 
    260 	sc->sc_udev = dev;
    261 	sc->sc_product = uaa->product;
    262 	sc->sc_vendor = uaa->vendor;
    263 
    264 	err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface);
    265 	if (err) {
    266 		printf("%s: getting interface handle failed\n",
    267 		    USBDEVNAME(sc->sc_dev));
    268 		USB_ATTACH_ERROR_RETURN;
    269 	}
    270 
    271 	sc->sc_iface = iface;
    272 	id = usbd_get_interface_descriptor(iface);
    273 
    274 	/* Find endpoints. */
    275 	for (i = 0; i < id->bNumEndpoints; i++) {
    276 		ed = usbd_interface2endpoint_descriptor(iface, i);
    277 		if (ed == NULL) {
    278 			printf("%s: couldn't get ep %d\n",
    279 			    USBDEVNAME(sc->sc_dev), i);
    280 			USB_ATTACH_ERROR_RETURN;
    281 		}
    282 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    283 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    284 			sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress;
    285 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    286 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    287 			sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress;
    288 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    289 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    290 			sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress;
    291 		}
    292 	}
    293 
    294 	if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 ||
    295 	    sc->sc_ed[UPL_ENDPT_INTR] == 0) {
    296 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    297 		USB_ATTACH_ERROR_RETURN;
    298 	}
    299 
    300 	s = splimp();
    301 
    302 	/* Initialize interface info.*/
    303 	ifp = &sc->sc_if;
    304 	ifp->if_softc = sc;
    305 	ifp->if_mtu = UPL_BUFSZ;
    306 	ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX;
    307 	ifp->if_ioctl = upl_ioctl;
    308 	ifp->if_start = upl_start;
    309 	ifp->if_watchdog = upl_watchdog;
    310 	strncpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
    311 
    312 	ifp->if_type = IFT_OTHER;
    313 	ifp->if_addrlen = 0;
    314 	ifp->if_hdrlen = 0;
    315 	ifp->if_output = upl_output;
    316 	ifp->if_input = upl_input;
    317 	ifp->if_baudrate = 12000000;
    318 
    319 	/* Attach the interface. */
    320 	if_attach(ifp);
    321 
    322 #if NBPFILTER > 0
    323 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, 0);
    324 #endif
    325 #if NRND > 0
    326 	rnd_attach_source(&sc->sc_rnd_source, USBDEVNAME(sc->sc_dev),
    327 	    RND_TYPE_NET, 0);
    328 #endif
    329 
    330 	sc->sc_attached = 1;
    331 	splx(s);
    332 
    333 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    334 	    USBDEV(sc->sc_dev));
    335 
    336 	USB_ATTACH_SUCCESS_RETURN;
    337 }
    338 
    339 USB_DETACH(upl)
    340 {
    341 	USB_DETACH_START(upl, sc);
    342 	struct ifnet		*ifp = &sc->sc_if;
    343 	int			s;
    344 
    345 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
    346 
    347 	s = splusb();
    348 
    349 	if (!sc->sc_attached) {
    350 		/* Detached before attached finished, so just bail out. */
    351 		splx(s);
    352 		return (0);
    353 	}
    354 
    355 	if (ifp->if_flags & IFF_RUNNING)
    356 		upl_stop(sc);
    357 
    358 #if NRND > 0
    359 	rnd_detach_source(&sc->sc_rnd_source);
    360 #endif
    361 #if NBPFILTER > 0
    362 	bpfdetach(ifp);
    363 #endif
    364 	ether_ifdetach(ifp);
    365 
    366 	if_detach(ifp);
    367 
    368 #ifdef DIAGNOSTIC
    369 	if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
    370 	    sc->sc_ep[UPL_ENDPT_RX] != NULL ||
    371 	    sc->sc_ep[UPL_ENDPT_INTR] != NULL)
    372 		printf("%s: detach has active endpoints\n",
    373 		       USBDEVNAME(sc->sc_dev));
    374 #endif
    375 
    376 	sc->sc_attached = 0;
    377 	splx(s);
    378 
    379 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    380 	    USBDEV(sc->sc_dev));
    381 
    382 	return (0);
    383 }
    384 
    385 int
    386 upl_activate(self, act)
    387 	device_ptr_t self;
    388 	enum devact act;
    389 {
    390 	struct upl_softc *sc = (struct upl_softc *)self;
    391 
    392 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
    393 
    394 	switch (act) {
    395 	case DVACT_ACTIVATE:
    396 		return (EOPNOTSUPP);
    397 		break;
    398 
    399 	case DVACT_DEACTIVATE:
    400 		/* Deactivate the interface. */
    401 		if_deactivate(&sc->sc_if);
    402 		sc->sc_dying = 1;
    403 		break;
    404 	}
    405 	return (0);
    406 }
    407 
    408 /*
    409  * Initialize an RX descriptor and attach an MBUF cluster.
    410  */
    411 Static int
    412 upl_newbuf(sc, c, m)
    413 	struct upl_softc	*sc;
    414 	struct upl_chain	*c;
    415 	struct mbuf		*m;
    416 {
    417 	struct mbuf		*m_new = NULL;
    418 
    419 	DPRINTFN(8,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
    420 
    421 	if (m == NULL) {
    422 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    423 		if (m_new == NULL) {
    424 			printf("%s: no memory for rx list "
    425 			    "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
    426 			return (ENOBUFS);
    427 		}
    428 
    429 		MCLGET(m_new, M_DONTWAIT);
    430 		if (!(m_new->m_flags & M_EXT)) {
    431 			printf("%s: no memory for rx list "
    432 			    "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
    433 			m_freem(m_new);
    434 			return (ENOBUFS);
    435 		}
    436 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    437 	} else {
    438 		m_new = m;
    439 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    440 		m_new->m_data = m_new->m_ext.ext_buf;
    441 	}
    442 
    443 	c->upl_mbuf = m_new;
    444 
    445 	return (0);
    446 }
    447 
    448 Static int
    449 upl_rx_list_init(sc)
    450 	struct upl_softc	*sc;
    451 {
    452 	struct upl_cdata	*cd;
    453 	struct upl_chain	*c;
    454 	int			i;
    455 
    456 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
    457 
    458 	cd = &sc->sc_cdata;
    459 	for (i = 0; i < UPL_RX_LIST_CNT; i++) {
    460 		c = &cd->upl_rx_chain[i];
    461 		c->upl_sc = sc;
    462 		c->upl_idx = i;
    463 		if (upl_newbuf(sc, c, NULL) == ENOBUFS)
    464 			return (ENOBUFS);
    465 		if (c->upl_xfer == NULL) {
    466 			c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
    467 			if (c->upl_xfer == NULL)
    468 				return (ENOBUFS);
    469 			c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
    470 			if (c->upl_buf == NULL) {
    471 				usbd_free_xfer(c->upl_xfer);
    472 				return (ENOBUFS);
    473 			}
    474 		}
    475 	}
    476 
    477 	return (0);
    478 }
    479 
    480 Static int
    481 upl_tx_list_init(sc)
    482 	struct upl_softc	*sc;
    483 {
    484 	struct upl_cdata	*cd;
    485 	struct upl_chain	*c;
    486 	int			i;
    487 
    488 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
    489 
    490 	cd = &sc->sc_cdata;
    491 	for (i = 0; i < UPL_TX_LIST_CNT; i++) {
    492 		c = &cd->upl_tx_chain[i];
    493 		c->upl_sc = sc;
    494 		c->upl_idx = i;
    495 		c->upl_mbuf = NULL;
    496 		if (c->upl_xfer == NULL) {
    497 			c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
    498 			if (c->upl_xfer == NULL)
    499 				return (ENOBUFS);
    500 			c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
    501 			if (c->upl_buf == NULL) {
    502 				usbd_free_xfer(c->upl_xfer);
    503 				return (ENOBUFS);
    504 			}
    505 		}
    506 	}
    507 
    508 	return (0);
    509 }
    510 
    511 /*
    512  * A frame has been uploaded: pass the resulting mbuf chain up to
    513  * the higher level protocols.
    514  */
    515 Static void
    516 upl_rxeof(xfer, priv, status)
    517 	usbd_xfer_handle	xfer;
    518 	usbd_private_handle	priv;
    519 	usbd_status		status;
    520 {
    521 	struct upl_chain	*c = priv;
    522 	struct upl_softc	*sc = c->upl_sc;
    523 	struct ifnet		*ifp = &sc->sc_if;
    524 	struct mbuf		*m;
    525 	int			total_len = 0;
    526 	int			s;
    527 
    528 	if (sc->sc_dying)
    529 		return;
    530 
    531 	if (!(ifp->if_flags & IFF_RUNNING))
    532 		return;
    533 
    534 	if (status != USBD_NORMAL_COMPLETION) {
    535 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    536 			return;
    537 		sc->sc_rx_errs++;
    538 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
    539 			printf("%s: %u usb errors on rx: %s\n",
    540 			    USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
    541 			    usbd_errstr(status));
    542 			sc->sc_rx_errs = 0;
    543 		}
    544 		if (status == USBD_STALLED)
    545 			usbd_clear_endpoint_stall(sc->sc_ep[UPL_ENDPT_RX]);
    546 		goto done;
    547 	}
    548 
    549 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    550 
    551 	DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
    552 		    USBDEVNAME(sc->sc_dev), __FUNCTION__, status, total_len));
    553 
    554 	m = c->upl_mbuf;
    555 	memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len);
    556 
    557 	ifp->if_ipackets++;
    558 	m->m_pkthdr.len = m->m_len = total_len;
    559 
    560 	m->m_pkthdr.rcvif = ifp;
    561 
    562 	s = splimp();
    563 
    564 	/* XXX ugly */
    565 	if (upl_newbuf(sc, c, NULL) == ENOBUFS) {
    566 		ifp->if_ierrors++;
    567 		goto done1;
    568 	}
    569 
    570 #if NBPFILTER > 0
    571 	/*
    572 	 * Handle BPF listeners. Let the BPF user see the packet, but
    573 	 * don't pass it up to the ether_input() layer unless it's
    574 	 * a broadcast packet, multicast packet, matches our ethernet
    575 	 * address or the interface is in promiscuous mode.
    576 	 */
    577 	if (ifp->if_bpf) {
    578 		BPF_MTAP(ifp, m);
    579 	}
    580 #endif
    581 
    582 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
    583 		    __FUNCTION__, m->m_len));
    584 
    585 	IF_INPUT(ifp, m);
    586 
    587  done1:
    588 	splx(s);
    589 
    590  done:
    591 #if 1
    592 	/* Setup new transfer. */
    593 	usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
    594 	    c, c->upl_buf, UPL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    595 	    USBD_NO_TIMEOUT, upl_rxeof);
    596 	usbd_transfer(c->upl_xfer);
    597 
    598 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev),
    599 		    __FUNCTION__));
    600 #endif
    601 }
    602 
    603 /*
    604  * A frame was downloaded to the chip. It's safe for us to clean up
    605  * the list buffers.
    606  */
    607 Static void
    608 upl_txeof(xfer, priv, status)
    609 	usbd_xfer_handle	xfer;
    610 	usbd_private_handle	priv;
    611 	usbd_status		status;
    612 {
    613 	struct upl_chain	*c = priv;
    614 	struct upl_softc	*sc = c->upl_sc;
    615 	struct ifnet		*ifp = &sc->sc_if;
    616 	int			s;
    617 
    618 	if (sc->sc_dying)
    619 		return;
    620 
    621 	s = splimp();
    622 
    623 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->sc_dev),
    624 		    __FUNCTION__, status));
    625 
    626 	ifp->if_timer = 0;
    627 	ifp->if_flags &= ~IFF_OACTIVE;
    628 
    629 	if (status != USBD_NORMAL_COMPLETION) {
    630 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    631 			splx(s);
    632 			return;
    633 		}
    634 		ifp->if_oerrors++;
    635 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
    636 		    usbd_errstr(status));
    637 		if (status == USBD_STALLED)
    638 			usbd_clear_endpoint_stall(sc->sc_ep[UPL_ENDPT_TX]);
    639 		splx(s);
    640 		return;
    641 	}
    642 
    643 	ifp->if_opackets++;
    644 
    645 	m_freem(c->upl_mbuf);
    646 	c->upl_mbuf = NULL;
    647 
    648 	if (ifp->if_snd.ifq_head != NULL)
    649 		upl_start(ifp);
    650 
    651 	splx(s);
    652 }
    653 
    654 Static int
    655 upl_send(sc, m, idx)
    656 	struct upl_softc	*sc;
    657 	struct mbuf		*m;
    658 	int			idx;
    659 {
    660 	int			total_len;
    661 	struct upl_chain	*c;
    662 	usbd_status		err;
    663 
    664 	c = &sc->sc_cdata.upl_tx_chain[idx];
    665 
    666 	/*
    667 	 * Copy the mbuf data into a contiguous buffer, leaving two
    668 	 * bytes at the beginning to hold the frame length.
    669 	 */
    670 	m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf);
    671 	c->upl_mbuf = m;
    672 
    673 	total_len = m->m_pkthdr.len;
    674 
    675 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    676 		     USBDEVNAME(sc->sc_dev), __FUNCTION__, total_len));
    677 
    678 	usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_TX],
    679 	    c, c->upl_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
    680 	    upl_txeof);
    681 
    682 	/* Transmit */
    683 	err = usbd_transfer(c->upl_xfer);
    684 	if (err != USBD_IN_PROGRESS) {
    685 		printf("%s: upl_send error=%s\n", USBDEVNAME(sc->sc_dev),
    686 		       usbd_errstr(err));
    687 		upl_stop(sc);
    688 		return (EIO);
    689 	}
    690 
    691 	sc->sc_cdata.upl_tx_cnt++;
    692 
    693 	return (0);
    694 }
    695 
    696 Static void
    697 upl_start(ifp)
    698 	struct ifnet		*ifp;
    699 {
    700 	struct upl_softc	*sc = ifp->if_softc;
    701 	struct mbuf		*m_head = NULL;
    702 
    703 	if (sc->sc_dying)
    704 		return;
    705 
    706 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
    707 
    708 	if (ifp->if_flags & IFF_OACTIVE)
    709 		return;
    710 
    711 	IF_DEQUEUE(&ifp->if_snd, m_head);
    712 	if (m_head == NULL)
    713 		return;
    714 
    715 	if (upl_send(sc, m_head, 0)) {
    716 		IF_PREPEND(&ifp->if_snd, m_head);
    717 		ifp->if_flags |= IFF_OACTIVE;
    718 		return;
    719 	}
    720 
    721 #if NBPFILTER > 0
    722 	/*
    723 	 * If there's a BPF listener, bounce a copy of this frame
    724 	 * to him.
    725 	 */
    726 	if (ifp->if_bpf)
    727 		BPF_MTAP(ifp, m_head);
    728 #endif
    729 
    730 	ifp->if_flags |= IFF_OACTIVE;
    731 
    732 	/*
    733 	 * Set a timeout in case the chip goes out to lunch.
    734 	 */
    735 	ifp->if_timer = 5;
    736 }
    737 
    738 Static void
    739 upl_init(xsc)
    740 	void			*xsc;
    741 {
    742 	struct upl_softc	*sc = xsc;
    743 	struct ifnet		*ifp = &sc->sc_if;
    744 	int			s;
    745 
    746 	if (sc->sc_dying)
    747 		return;
    748 
    749 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
    750 
    751 	if (ifp->if_flags & IFF_RUNNING)
    752 		return;
    753 
    754 	s = splimp();
    755 
    756 	/* Init TX ring. */
    757 	if (upl_tx_list_init(sc) == ENOBUFS) {
    758 		printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
    759 		splx(s);
    760 		return;
    761 	}
    762 
    763 	/* Init RX ring. */
    764 	if (upl_rx_list_init(sc) == ENOBUFS) {
    765 		printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
    766 		splx(s);
    767 		return;
    768 	}
    769 
    770 	if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
    771 		if (upl_openpipes(sc)) {
    772 			splx(s);
    773 			return;
    774 		}
    775 	}
    776 
    777 	ifp->if_flags |= IFF_RUNNING;
    778 	ifp->if_flags &= ~IFF_OACTIVE;
    779 
    780 	splx(s);
    781 }
    782 
    783 Static int
    784 upl_openpipes(sc)
    785 	struct upl_softc	*sc;
    786 {
    787 	struct upl_chain	*c;
    788 	usbd_status		err;
    789 	int			i;
    790 
    791 	/* Open RX and TX pipes. */
    792 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
    793 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
    794 	if (err) {
    795 		printf("%s: open rx pipe failed: %s\n",
    796 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    797 		return (EIO);
    798 	}
    799 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
    800 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
    801 	if (err) {
    802 		printf("%s: open tx pipe failed: %s\n",
    803 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    804 		return (EIO);
    805 	}
    806 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
    807 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
    808 	    &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
    809 	    UPL_INTR_INTERVAL);
    810 	if (err) {
    811 		printf("%s: open intr pipe failed: %s\n",
    812 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    813 		return (EIO);
    814 	}
    815 
    816 
    817 #if 1
    818 	/* Start up the receive pipe. */
    819 	for (i = 0; i < UPL_RX_LIST_CNT; i++) {
    820 		c = &sc->sc_cdata.upl_rx_chain[i];
    821 		usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
    822 		    c, c->upl_buf, UPL_BUFSZ,
    823 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
    824 		    upl_rxeof);
    825 		usbd_transfer(c->upl_xfer);
    826 	}
    827 #endif
    828 
    829 	return (0);
    830 }
    831 
    832 Static void
    833 upl_intr(xfer, priv, status)
    834 	usbd_xfer_handle	xfer;
    835 	usbd_private_handle	priv;
    836 	usbd_status		status;
    837 {
    838 	struct upl_softc	*sc = priv;
    839 	struct ifnet		*ifp = &sc->sc_if;
    840 	uByte			stat;
    841 
    842 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
    843 
    844 	if (sc->sc_dying)
    845 		return;
    846 
    847 	if (!(ifp->if_flags & IFF_RUNNING))
    848 		return;
    849 
    850 	if (status != USBD_NORMAL_COMPLETION) {
    851 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    852 			return;
    853 		}
    854 		sc->sc_intr_errs++;
    855 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
    856 			printf("%s: %u usb errors on intr: %s\n",
    857 			    USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
    858 			    usbd_errstr(status));
    859 			sc->sc_intr_errs = 0;
    860 		}
    861 		if (status == USBD_STALLED)
    862 			usbd_clear_endpoint_stall(sc->sc_ep[UPL_ENDPT_RX]);
    863 		return;
    864 	}
    865 
    866 	stat = sc->sc_ibuf;
    867 
    868 	if (stat == 0)
    869 		return;
    870 
    871 	DPRINTFN(10,("%s: %s: stat=0x%02x\n", USBDEVNAME(sc->sc_dev),
    872 		     __FUNCTION__, stat));
    873 
    874 }
    875 
    876 Static int
    877 upl_ioctl(ifp, command, data)
    878 	struct ifnet		*ifp;
    879 	u_long			command;
    880 	caddr_t			data;
    881 {
    882 	struct upl_softc	*sc = ifp->if_softc;
    883 	struct ifaddr 		*ifa = (struct ifaddr *)data;
    884 	struct ifreq		*ifr = (struct ifreq *)data;
    885 	int			s, error = 0;
    886 
    887 	if (sc->sc_dying)
    888 		return (EIO);
    889 
    890 	DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
    891 		    USBDEVNAME(sc->sc_dev), __FUNCTION__, command));
    892 
    893 	s = splimp();
    894 
    895 	switch(command) {
    896 	case SIOCSIFADDR:
    897 		ifp->if_flags |= IFF_UP;
    898 		upl_init(sc);
    899 
    900 		switch (ifa->ifa_addr->sa_family) {
    901 #ifdef INET
    902 		case AF_INET:
    903 			break;
    904 #endif /* INET */
    905 #ifdef NS
    906 		case AF_NS:
    907 		    {
    908 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    909 
    910 			if (ns_nullhost(*ina))
    911 				ina->x_host = *(union ns_host *)
    912 					LLADDR(ifp->if_sadl);
    913 			else
    914 				memcpy(LLADDR(ifp->if_sadl),
    915 				       ina->x_host.c_host,
    916 				       ifp->if_addrlen);
    917 			break;
    918 		    }
    919 #endif /* NS */
    920 		}
    921 		break;
    922 
    923 	case SIOCSIFMTU:
    924 		if (ifr->ifr_mtu > UPL_BUFSZ)
    925 			error = EINVAL;
    926 		else
    927 			ifp->if_mtu = ifr->ifr_mtu;
    928 		break;
    929 
    930 	case SIOCSIFFLAGS:
    931 		if (ifp->if_flags & IFF_UP) {
    932 			if (!(ifp->if_flags & IFF_RUNNING))
    933 				upl_init(sc);
    934 		} else {
    935 			if (ifp->if_flags & IFF_RUNNING)
    936 				upl_stop(sc);
    937 		}
    938 		error = 0;
    939 		break;
    940 	default:
    941 		error = EINVAL;
    942 		break;
    943 	}
    944 
    945 	splx(s);
    946 
    947 	return (error);
    948 }
    949 
    950 Static void
    951 upl_watchdog(ifp)
    952 	struct ifnet		*ifp;
    953 {
    954 	struct upl_softc	*sc = ifp->if_softc;
    955 
    956 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
    957 
    958 	if (sc->sc_dying)
    959 		return;
    960 
    961 	ifp->if_oerrors++;
    962 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
    963 
    964 	upl_stop(sc);
    965 	upl_init(sc);
    966 
    967 	if (ifp->if_snd.ifq_head != NULL)
    968 		upl_start(ifp);
    969 }
    970 
    971 /*
    972  * Stop the adapter and free any mbufs allocated to the
    973  * RX and TX lists.
    974  */
    975 Static void
    976 upl_stop(sc)
    977 	struct upl_softc	*sc;
    978 {
    979 	usbd_status		err;
    980 	struct ifnet		*ifp;
    981 	int			i;
    982 
    983 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
    984 
    985 	ifp = &sc->sc_if;
    986 	ifp->if_timer = 0;
    987 
    988 	/* Stop transfers. */
    989 	if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
    990 		err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
    991 		if (err) {
    992 			printf("%s: abort rx pipe failed: %s\n",
    993 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    994 		}
    995 		err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
    996 		if (err) {
    997 			printf("%s: close rx pipe failed: %s\n",
    998 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    999 		}
   1000 		sc->sc_ep[UPL_ENDPT_RX] = NULL;
   1001 	}
   1002 
   1003 	if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
   1004 		err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
   1005 		if (err) {
   1006 			printf("%s: abort tx pipe failed: %s\n",
   1007 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1008 		}
   1009 		err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
   1010 		if (err) {
   1011 			printf("%s: close tx pipe failed: %s\n",
   1012 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1013 		}
   1014 		sc->sc_ep[UPL_ENDPT_TX] = NULL;
   1015 	}
   1016 
   1017 	if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
   1018 		err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
   1019 		if (err) {
   1020 			printf("%s: abort intr pipe failed: %s\n",
   1021 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1022 		}
   1023 		err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
   1024 		if (err) {
   1025 			printf("%s: close intr pipe failed: %s\n",
   1026 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1027 		}
   1028 		sc->sc_ep[UPL_ENDPT_INTR] = NULL;
   1029 	}
   1030 
   1031 	/* Free RX resources. */
   1032 	for (i = 0; i < UPL_RX_LIST_CNT; i++) {
   1033 		if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) {
   1034 			m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf);
   1035 			sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL;
   1036 		}
   1037 		if (sc->sc_cdata.upl_rx_chain[i].upl_xfer != NULL) {
   1038 			usbd_free_xfer(sc->sc_cdata.upl_rx_chain[i].upl_xfer);
   1039 			sc->sc_cdata.upl_rx_chain[i].upl_xfer = NULL;
   1040 		}
   1041 	}
   1042 
   1043 	/* Free TX resources. */
   1044 	for (i = 0; i < UPL_TX_LIST_CNT; i++) {
   1045 		if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) {
   1046 			m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf);
   1047 			sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL;
   1048 		}
   1049 		if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) {
   1050 			usbd_free_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer);
   1051 			sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL;
   1052 		}
   1053 	}
   1054 
   1055 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1056 }
   1057 
   1058 Static int
   1059 upl_output(ifp, m, dst, rt0)
   1060 	struct ifnet *ifp;
   1061 	struct mbuf *m;
   1062 	struct sockaddr *dst;
   1063 	struct rtentry *rt0;
   1064 {
   1065 	int s;
   1066 
   1067 	DPRINTFN(10,("%s: %s: enter\n",
   1068 		     USBDEVNAME(((struct upl_softc *)ifp->if_softc)->sc_dev),
   1069 		     __FUNCTION__));
   1070 
   1071 	s = splimp();
   1072 	/*
   1073 	 * Queue message on interface, and start output if interface
   1074 	 * not yet active.
   1075 	 */
   1076 	if (IF_QFULL(&ifp->if_snd)) {
   1077 		IF_DROP(&ifp->if_snd);
   1078 		splx(s);
   1079 		return (ENOBUFS);
   1080 	}
   1081 	ifp->if_obytes += m->m_pkthdr.len;
   1082 	IF_ENQUEUE(&ifp->if_snd, m);
   1083 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   1084 		(*ifp->if_start)(ifp);
   1085 	splx(s);
   1086 
   1087 	return (0);
   1088 }
   1089 
   1090 Static void
   1091 upl_input(ifp, m)
   1092 	struct ifnet *ifp;
   1093 	struct mbuf *m;
   1094 {
   1095 	struct ifqueue *inq;
   1096 	int s;
   1097 
   1098 	/* XXX Assume all traffic is IP */
   1099 
   1100 	schednetisr(NETISR_IP);
   1101 	inq = &ipintrq;
   1102 
   1103 	if (IF_QFULL(inq)) {
   1104 		IF_DROP(inq);
   1105 		splx(s);
   1106 		//if (sc->sc_flags & SC_DEBUG)
   1107 		//printf("%s: input queue full\n", ifp->if_xname);
   1108 		ifp->if_iqdrops++;
   1109 		return;
   1110 	}
   1111 	IF_ENQUEUE(inq, m);
   1112 	splx(s);
   1113 	ifp->if_ipackets++;
   1114 	ifp->if_ibytes += m->m_len;
   1115 	ifp->if_lastchange = time;
   1116 }
   1117