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