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