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