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