Home | History | Annotate | Line # | Download | only in usb
if_upl.c revision 1.27.4.2
      1  1.27.4.1     itohy /*	$NetBSD: if_upl.c,v 1.27.4.2 2007/06/13 03:59:16 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.1     itohy __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1.27.4.2 2007/06/13 03:59:16 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.4  augustss Static int upl_ioctl(struct ifnet *, u_long, caddr_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.1  augustss 	if (uaa->iface != NULL)
    208       1.1  augustss 		return (UMATCH_NONE);
    209       1.1  augustss 
    210       1.1  augustss 	for (t = sc_devs; t->upl_vid != 0; t++)
    211       1.1  augustss 		if (uaa->vendor == t->upl_vid && uaa->product == t->upl_did)
    212       1.1  augustss 			return (UMATCH_VENDOR_PRODUCT);
    213       1.1  augustss 
    214       1.1  augustss 	return (UMATCH_NONE);
    215       1.1  augustss }
    216       1.1  augustss 
    217       1.1  augustss USB_ATTACH(upl)
    218       1.1  augustss {
    219       1.1  augustss 	USB_ATTACH_START(upl, sc, uaa);
    220      1.22  augustss 	char			*devinfop;
    221       1.1  augustss 	int			s;
    222       1.1  augustss 	usbd_device_handle	dev = uaa->device;
    223       1.1  augustss 	usbd_interface_handle	iface;
    224       1.1  augustss 	usbd_status		err;
    225       1.1  augustss 	struct ifnet		*ifp;
    226       1.1  augustss 	usb_interface_descriptor_t	*id;
    227       1.1  augustss 	usb_endpoint_descriptor_t	*ed;
    228       1.1  augustss 	int			i;
    229       1.1  augustss 
    230       1.1  augustss 	DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev));
    231       1.1  augustss 
    232      1.22  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    233       1.1  augustss 	USB_ATTACH_SETUP;
    234      1.22  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    235      1.22  augustss 	usbd_devinfo_free(devinfop);
    236       1.1  augustss 
    237       1.9  augustss 	err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1);
    238       1.1  augustss 	if (err) {
    239       1.1  augustss 		printf("%s: setting config no failed\n",
    240       1.1  augustss 		    USBDEVNAME(sc->sc_dev));
    241       1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    242       1.1  augustss 	}
    243       1.1  augustss 
    244       1.1  augustss 	sc->sc_udev = dev;
    245       1.1  augustss 	sc->sc_product = uaa->product;
    246       1.1  augustss 	sc->sc_vendor = uaa->vendor;
    247       1.1  augustss 
    248       1.1  augustss 	err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface);
    249       1.1  augustss 	if (err) {
    250       1.1  augustss 		printf("%s: getting interface handle failed\n",
    251       1.1  augustss 		    USBDEVNAME(sc->sc_dev));
    252       1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    253       1.1  augustss 	}
    254       1.1  augustss 
    255       1.1  augustss 	sc->sc_iface = iface;
    256       1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    257       1.1  augustss 
    258       1.1  augustss 	/* Find endpoints. */
    259       1.1  augustss 	for (i = 0; i < id->bNumEndpoints; i++) {
    260       1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    261       1.1  augustss 		if (ed == NULL) {
    262       1.1  augustss 			printf("%s: couldn't get ep %d\n",
    263       1.1  augustss 			    USBDEVNAME(sc->sc_dev), i);
    264       1.1  augustss 			USB_ATTACH_ERROR_RETURN;
    265       1.1  augustss 		}
    266       1.1  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    267       1.1  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    268       1.1  augustss 			sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress;
    269       1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    270       1.1  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    271       1.1  augustss 			sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress;
    272       1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    273       1.1  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    274       1.1  augustss 			sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress;
    275       1.1  augustss 		}
    276       1.1  augustss 	}
    277       1.1  augustss 
    278       1.1  augustss 	if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 ||
    279       1.1  augustss 	    sc->sc_ed[UPL_ENDPT_INTR] == 0) {
    280       1.1  augustss 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    281       1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    282       1.1  augustss 	}
    283       1.1  augustss 
    284      1.14   thorpej 	s = splnet();
    285       1.1  augustss 
    286       1.1  augustss 	/* Initialize interface info.*/
    287       1.1  augustss 	ifp = &sc->sc_if;
    288       1.1  augustss 	ifp->if_softc = sc;
    289       1.1  augustss 	ifp->if_mtu = UPL_BUFSZ;
    290       1.1  augustss 	ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX;
    291       1.1  augustss 	ifp->if_ioctl = upl_ioctl;
    292       1.1  augustss 	ifp->if_start = upl_start;
    293  1.27.4.2     itohy 	ifp->if_init = upl_init;
    294  1.27.4.2     itohy 	ifp->if_stop = upl_stop;
    295       1.1  augustss 	ifp->if_watchdog = upl_watchdog;
    296       1.1  augustss 	strncpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
    297       1.1  augustss 
    298       1.1  augustss 	ifp->if_type = IFT_OTHER;
    299       1.1  augustss 	ifp->if_addrlen = 0;
    300       1.1  augustss 	ifp->if_hdrlen = 0;
    301       1.1  augustss 	ifp->if_output = upl_output;
    302       1.1  augustss 	ifp->if_input = upl_input;
    303       1.1  augustss 	ifp->if_baudrate = 12000000;
    304      1.12  augustss 	ifp->if_dlt = DLT_RAW;
    305      1.17    itojun 	IFQ_SET_READY(&ifp->if_snd);
    306       1.1  augustss 
    307       1.1  augustss 	/* Attach the interface. */
    308       1.1  augustss 	if_attach(ifp);
    309      1.12  augustss 	if_alloc_sadl(ifp);
    310       1.1  augustss 
    311       1.1  augustss #if NBPFILTER > 0
    312      1.12  augustss 	bpfattach(ifp, DLT_RAW, 0);
    313       1.1  augustss #endif
    314       1.1  augustss #if NRND > 0
    315       1.1  augustss 	rnd_attach_source(&sc->sc_rnd_source, USBDEVNAME(sc->sc_dev),
    316       1.1  augustss 	    RND_TYPE_NET, 0);
    317       1.1  augustss #endif
    318       1.1  augustss 
    319       1.1  augustss 	sc->sc_attached = 1;
    320       1.1  augustss 	splx(s);
    321       1.1  augustss 
    322       1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    323       1.1  augustss 	    USBDEV(sc->sc_dev));
    324       1.1  augustss 
    325       1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    326       1.1  augustss }
    327       1.1  augustss 
    328       1.1  augustss USB_DETACH(upl)
    329       1.1  augustss {
    330       1.1  augustss 	USB_DETACH_START(upl, sc);
    331       1.1  augustss 	struct ifnet		*ifp = &sc->sc_if;
    332       1.1  augustss 	int			s;
    333       1.1  augustss 
    334      1.18  augustss 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    335       1.1  augustss 
    336       1.1  augustss 	s = splusb();
    337       1.1  augustss 
    338       1.1  augustss 	if (!sc->sc_attached) {
    339       1.1  augustss 		/* Detached before attached finished, so just bail out. */
    340       1.1  augustss 		splx(s);
    341       1.1  augustss 		return (0);
    342       1.1  augustss 	}
    343       1.1  augustss 
    344       1.1  augustss 	if (ifp->if_flags & IFF_RUNNING)
    345  1.27.4.2     itohy 		upl_stop(ifp, 1);
    346       1.1  augustss 
    347       1.1  augustss #if NRND > 0
    348       1.1  augustss 	rnd_detach_source(&sc->sc_rnd_source);
    349       1.1  augustss #endif
    350       1.1  augustss #if NBPFILTER > 0
    351       1.1  augustss 	bpfdetach(ifp);
    352       1.1  augustss #endif
    353       1.1  augustss 
    354       1.1  augustss 	if_detach(ifp);
    355       1.1  augustss 
    356       1.1  augustss #ifdef DIAGNOSTIC
    357       1.1  augustss 	if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
    358       1.1  augustss 	    sc->sc_ep[UPL_ENDPT_RX] != NULL ||
    359       1.1  augustss 	    sc->sc_ep[UPL_ENDPT_INTR] != NULL)
    360       1.1  augustss 		printf("%s: detach has active endpoints\n",
    361       1.1  augustss 		       USBDEVNAME(sc->sc_dev));
    362       1.1  augustss #endif
    363       1.1  augustss 
    364       1.1  augustss 	sc->sc_attached = 0;
    365       1.1  augustss 	splx(s);
    366       1.1  augustss 
    367       1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    368       1.1  augustss 	    USBDEV(sc->sc_dev));
    369       1.1  augustss 
    370       1.1  augustss 	return (0);
    371       1.1  augustss }
    372       1.1  augustss 
    373       1.1  augustss int
    374       1.4  augustss upl_activate(device_ptr_t self, enum devact act)
    375       1.1  augustss {
    376       1.1  augustss 	struct upl_softc *sc = (struct upl_softc *)self;
    377       1.1  augustss 
    378      1.18  augustss 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    379       1.1  augustss 
    380       1.1  augustss 	switch (act) {
    381       1.1  augustss 	case DVACT_ACTIVATE:
    382       1.1  augustss 		return (EOPNOTSUPP);
    383       1.1  augustss 		break;
    384       1.1  augustss 
    385       1.1  augustss 	case DVACT_DEACTIVATE:
    386       1.1  augustss 		/* Deactivate the interface. */
    387       1.1  augustss 		if_deactivate(&sc->sc_if);
    388       1.1  augustss 		sc->sc_dying = 1;
    389       1.1  augustss 		break;
    390       1.1  augustss 	}
    391       1.1  augustss 	return (0);
    392       1.1  augustss }
    393       1.1  augustss 
    394       1.1  augustss /*
    395       1.1  augustss  * A frame has been uploaded: pass the resulting mbuf chain up to
    396       1.1  augustss  * the higher level protocols.
    397       1.1  augustss  */
    398       1.1  augustss Static void
    399       1.4  augustss upl_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    400       1.1  augustss {
    401  1.27.4.2     itohy 	struct ue_chain		*c = priv;
    402  1.27.4.2     itohy 	struct upl_softc	*sc = (void *)c->ue_dev;
    403       1.1  augustss 	struct ifnet		*ifp = &sc->sc_if;
    404       1.1  augustss 	struct mbuf		*m;
    405       1.1  augustss 	int			total_len = 0;
    406       1.1  augustss 	int			s;
    407       1.1  augustss 
    408       1.1  augustss 	if (sc->sc_dying)
    409       1.1  augustss 		return;
    410       1.1  augustss 
    411       1.1  augustss 	if (!(ifp->if_flags & IFF_RUNNING))
    412       1.1  augustss 		return;
    413       1.1  augustss 
    414  1.27.4.2     itohy 	usbd_unmap_buffer(xfer);
    415  1.27.4.2     itohy 
    416       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    417       1.1  augustss 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    418       1.1  augustss 			return;
    419       1.1  augustss 		sc->sc_rx_errs++;
    420       1.1  augustss 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
    421       1.1  augustss 			printf("%s: %u usb errors on rx: %s\n",
    422       1.1  augustss 			    USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
    423       1.1  augustss 			    usbd_errstr(status));
    424       1.1  augustss 			sc->sc_rx_errs = 0;
    425       1.1  augustss 		}
    426       1.1  augustss 		if (status == USBD_STALLED)
    427      1.23  augustss 			usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
    428       1.1  augustss 		goto done;
    429       1.1  augustss 	}
    430       1.1  augustss 
    431       1.1  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    432       1.1  augustss 
    433       1.1  augustss 	DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
    434      1.18  augustss 		    USBDEVNAME(sc->sc_dev), __func__, status, total_len));
    435       1.1  augustss 
    436  1.27.4.2     itohy 	m = c->ue_mbuf;
    437  1.27.4.2     itohy 
    438  1.27.4.2     itohy 	/*
    439  1.27.4.2     itohy 	 * Allocate new mbuf cluster for the next transfer.
    440  1.27.4.2     itohy 	 * If that failed, discard current packet and recycle the mbuf.
    441  1.27.4.2     itohy 	 */
    442  1.27.4.2     itohy 	if ((c->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
    443  1.27.4.2     itohy 		printf("%s: no memory for rx list -- packet dropped!\n",
    444  1.27.4.2     itohy 		    USBDEVNAME(sc->sc_dev));
    445  1.27.4.2     itohy 		ifp->if_ierrors++;
    446  1.27.4.2     itohy 		c->ue_mbuf = usb_ether_newbuf(m);
    447  1.27.4.2     itohy 		goto done;
    448  1.27.4.2     itohy 	}
    449       1.1  augustss 
    450       1.1  augustss 	ifp->if_ipackets++;
    451       1.1  augustss 	m->m_pkthdr.len = m->m_len = total_len;
    452       1.1  augustss 
    453       1.1  augustss 	m->m_pkthdr.rcvif = ifp;
    454       1.1  augustss 
    455      1.14   thorpej 	s = splnet();
    456       1.1  augustss 
    457       1.1  augustss #if NBPFILTER > 0
    458       1.1  augustss 	/*
    459       1.1  augustss 	 * Handle BPF listeners. Let the BPF user see the packet, but
    460       1.1  augustss 	 * don't pass it up to the ether_input() layer unless it's
    461       1.1  augustss 	 * a broadcast packet, multicast packet, matches our ethernet
    462       1.1  augustss 	 * address or the interface is in promiscuous mode.
    463       1.1  augustss 	 */
    464       1.1  augustss 	if (ifp->if_bpf) {
    465       1.1  augustss 		BPF_MTAP(ifp, m);
    466       1.1  augustss 	}
    467       1.1  augustss #endif
    468       1.1  augustss 
    469       1.1  augustss 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
    470      1.18  augustss 		    __func__, m->m_len));
    471       1.1  augustss 
    472       1.1  augustss 	IF_INPUT(ifp, m);
    473       1.1  augustss 
    474       1.1  augustss 	splx(s);
    475       1.1  augustss 
    476       1.1  augustss  done:
    477       1.1  augustss #if 1
    478       1.1  augustss 	/* Setup new transfer. */
    479  1.27.4.2     itohy 	(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
    480  1.27.4.2     itohy 	usbd_setup_xfer(c->ue_xfer, sc->sc_ep[UPL_ENDPT_RX],
    481  1.27.4.2     itohy 	    c, NULL /* XXX buf */, UPL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    482       1.1  augustss 	    USBD_NO_TIMEOUT, upl_rxeof);
    483  1.27.4.2     itohy 	usbd_transfer(c->ue_xfer);
    484       1.1  augustss 
    485       1.1  augustss 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev),
    486      1.18  augustss 		    __func__));
    487       1.1  augustss #endif
    488       1.1  augustss }
    489       1.1  augustss 
    490       1.1  augustss /*
    491       1.1  augustss  * A frame was downloaded to the chip. It's safe for us to clean up
    492       1.1  augustss  * the list buffers.
    493       1.1  augustss  */
    494       1.1  augustss Static void
    495      1.26  christos upl_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    496      1.25  christos     usbd_status status)
    497       1.1  augustss {
    498  1.27.4.2     itohy 	struct ue_chain		*c = priv;
    499  1.27.4.2     itohy 	struct upl_softc	*sc = (void *)c->ue_dev;
    500       1.1  augustss 	struct ifnet		*ifp = &sc->sc_if;
    501       1.1  augustss 	int			s;
    502       1.1  augustss 
    503       1.1  augustss 	if (sc->sc_dying)
    504       1.1  augustss 		return;
    505       1.1  augustss 
    506      1.14   thorpej 	s = splnet();
    507       1.1  augustss 
    508       1.1  augustss 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->sc_dev),
    509      1.18  augustss 		    __func__, status));
    510       1.1  augustss 
    511       1.1  augustss 	ifp->if_timer = 0;
    512       1.1  augustss 	ifp->if_flags &= ~IFF_OACTIVE;
    513       1.1  augustss 
    514  1.27.4.2     itohy 	usbd_unmap_buffer(xfer);
    515  1.27.4.2     itohy 
    516       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    517       1.1  augustss 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    518       1.1  augustss 			splx(s);
    519       1.1  augustss 			return;
    520       1.1  augustss 		}
    521       1.1  augustss 		ifp->if_oerrors++;
    522       1.1  augustss 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
    523       1.1  augustss 		    usbd_errstr(status));
    524       1.1  augustss 		if (status == USBD_STALLED)
    525      1.23  augustss 			usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]);
    526       1.1  augustss 		splx(s);
    527       1.1  augustss 		return;
    528       1.1  augustss 	}
    529       1.1  augustss 
    530       1.1  augustss 	ifp->if_opackets++;
    531       1.1  augustss 
    532  1.27.4.2     itohy 	m_freem(c->ue_mbuf);
    533  1.27.4.2     itohy 	c->ue_mbuf = NULL;
    534       1.1  augustss 
    535      1.17    itojun 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    536       1.1  augustss 		upl_start(ifp);
    537       1.1  augustss 
    538       1.1  augustss 	splx(s);
    539       1.1  augustss }
    540       1.1  augustss 
    541       1.1  augustss Static int
    542       1.4  augustss upl_send(struct upl_softc *sc, struct mbuf *m, int idx)
    543       1.1  augustss {
    544       1.1  augustss 	int			total_len;
    545  1.27.4.2     itohy 	struct ue_chain		*c;
    546       1.1  augustss 	usbd_status		err;
    547  1.27.4.2     itohy 	int			ret;
    548       1.1  augustss 
    549       1.1  augustss 	c = &sc->sc_cdata.upl_tx_chain[idx];
    550       1.1  augustss 
    551  1.27.4.2     itohy 	ret = usb_ether_map_tx_buffer_mbuf(c, m);
    552  1.27.4.2     itohy 	if (ret)
    553  1.27.4.2     itohy 		return (ret);
    554       1.1  augustss 
    555       1.1  augustss 	total_len = m->m_pkthdr.len;
    556       1.1  augustss 
    557       1.1  augustss 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    558      1.18  augustss 		     USBDEVNAME(sc->sc_dev), __func__, total_len));
    559       1.1  augustss 
    560  1.27.4.2     itohy 	usbd_setup_xfer(c->ue_xfer, sc->sc_ep[UPL_ENDPT_TX],
    561  1.27.4.2     itohy 	    c, NULL /* XXX buf */, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
    562       1.1  augustss 	    upl_txeof);
    563       1.1  augustss 
    564       1.1  augustss 	/* Transmit */
    565  1.27.4.2     itohy 	err = usbd_transfer(c->ue_xfer);
    566       1.1  augustss 	if (err != USBD_IN_PROGRESS) {
    567       1.1  augustss 		printf("%s: upl_send error=%s\n", USBDEVNAME(sc->sc_dev),
    568       1.1  augustss 		       usbd_errstr(err));
    569  1.27.4.2     itohy 		upl_stop(&sc->sc_if, 0);
    570       1.1  augustss 		return (EIO);
    571       1.1  augustss 	}
    572       1.1  augustss 
    573       1.1  augustss 	sc->sc_cdata.upl_tx_cnt++;
    574       1.1  augustss 
    575       1.1  augustss 	return (0);
    576       1.1  augustss }
    577       1.1  augustss 
    578       1.1  augustss Static void
    579       1.4  augustss upl_start(struct ifnet *ifp)
    580       1.1  augustss {
    581       1.1  augustss 	struct upl_softc	*sc = ifp->if_softc;
    582       1.1  augustss 	struct mbuf		*m_head = NULL;
    583       1.1  augustss 
    584       1.1  augustss 	if (sc->sc_dying)
    585       1.1  augustss 		return;
    586       1.1  augustss 
    587      1.18  augustss 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
    588       1.1  augustss 
    589       1.1  augustss 	if (ifp->if_flags & IFF_OACTIVE)
    590       1.1  augustss 		return;
    591       1.1  augustss 
    592      1.17    itojun 	IFQ_POLL(&ifp->if_snd, m_head);
    593       1.1  augustss 	if (m_head == NULL)
    594       1.1  augustss 		return;
    595       1.1  augustss 
    596       1.1  augustss 	if (upl_send(sc, m_head, 0)) {
    597       1.1  augustss 		ifp->if_flags |= IFF_OACTIVE;
    598       1.1  augustss 		return;
    599       1.1  augustss 	}
    600       1.1  augustss 
    601      1.17    itojun 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    602      1.17    itojun 
    603       1.1  augustss #if NBPFILTER > 0
    604       1.1  augustss 	/*
    605       1.1  augustss 	 * If there's a BPF listener, bounce a copy of this frame
    606       1.1  augustss 	 * to him.
    607       1.1  augustss 	 */
    608       1.1  augustss 	if (ifp->if_bpf)
    609       1.1  augustss 		BPF_MTAP(ifp, m_head);
    610       1.1  augustss #endif
    611       1.1  augustss 
    612       1.1  augustss 	ifp->if_flags |= IFF_OACTIVE;
    613       1.1  augustss 
    614       1.1  augustss 	/*
    615       1.1  augustss 	 * Set a timeout in case the chip goes out to lunch.
    616       1.1  augustss 	 */
    617       1.1  augustss 	ifp->if_timer = 5;
    618       1.1  augustss }
    619       1.1  augustss 
    620  1.27.4.2     itohy Static int
    621  1.27.4.2     itohy upl_init(struct ifnet *ifp)
    622       1.1  augustss {
    623  1.27.4.2     itohy 	struct upl_softc	*sc = ifp->if_softc;
    624       1.1  augustss 	int			s;
    625  1.27.4.2     itohy 	struct ue_chain		*c;
    626  1.27.4.2     itohy 	int			i;
    627       1.1  augustss 
    628       1.1  augustss 	if (sc->sc_dying)
    629  1.27.4.2     itohy 		return (EIO);
    630       1.1  augustss 
    631      1.18  augustss 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
    632       1.1  augustss 
    633       1.1  augustss 	if (ifp->if_flags & IFF_RUNNING)
    634  1.27.4.2     itohy 		return (EIO);
    635       1.1  augustss 
    636      1.14   thorpej 	s = splnet();
    637       1.1  augustss 
    638  1.27.4.2     itohy 	if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
    639  1.27.4.2     itohy 		if (upl_openpipes(sc)) {
    640  1.27.4.2     itohy 			splx(s);
    641  1.27.4.2     itohy 			return (EIO);
    642  1.27.4.2     itohy 		}
    643  1.27.4.2     itohy 	}
    644  1.27.4.2     itohy 
    645       1.1  augustss 	/* Init TX ring. */
    646  1.27.4.2     itohy 	if ((i = usb_ether_tx_list_init(USBDEV(sc->sc_dev),
    647  1.27.4.2     itohy 	    sc->sc_cdata.upl_tx_chain, UPL_TX_LIST_CNT,
    648  1.27.4.2     itohy 	    sc->sc_udev, sc->sc_ep[UPL_ENDPT_TX], NULL))) {
    649       1.1  augustss 		printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
    650       1.1  augustss 		splx(s);
    651  1.27.4.2     itohy 		return (i);
    652       1.1  augustss 	}
    653       1.1  augustss 
    654       1.1  augustss 	/* Init RX ring. */
    655  1.27.4.2     itohy 	if ((i = usb_ether_rx_list_init(USBDEV(sc->sc_dev),
    656  1.27.4.2     itohy 	    sc->sc_cdata.upl_rx_chain, UPL_RX_LIST_CNT,
    657  1.27.4.2     itohy 	    sc->sc_udev, sc->sc_ep[UPL_ENDPT_RX]))) {
    658       1.1  augustss 		printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
    659       1.1  augustss 		splx(s);
    660  1.27.4.2     itohy 		return (i);
    661       1.1  augustss 	}
    662       1.1  augustss 
    663  1.27.4.2     itohy 	/* Start up the receive pipe. */
    664  1.27.4.2     itohy 	for (i = 0; i < UPL_RX_LIST_CNT; i++) {
    665  1.27.4.2     itohy 		c = &sc->sc_cdata.upl_rx_chain[i];
    666  1.27.4.2     itohy 		(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
    667  1.27.4.2     itohy 		usbd_setup_xfer(c->ue_xfer, sc->sc_ep[UPL_ENDPT_RX],
    668  1.27.4.2     itohy 		    c, NULL /* XXX buf */, UPL_BUFSZ,
    669  1.27.4.2     itohy 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
    670  1.27.4.2     itohy 		    upl_rxeof);
    671  1.27.4.2     itohy 		usbd_transfer(c->ue_xfer);
    672       1.1  augustss 	}
    673       1.1  augustss 
    674       1.1  augustss 	ifp->if_flags |= IFF_RUNNING;
    675       1.1  augustss 	ifp->if_flags &= ~IFF_OACTIVE;
    676       1.1  augustss 
    677       1.1  augustss 	splx(s);
    678  1.27.4.2     itohy 	return (0);
    679       1.1  augustss }
    680       1.1  augustss 
    681       1.1  augustss Static int
    682       1.4  augustss upl_openpipes(struct upl_softc *sc)
    683       1.1  augustss {
    684       1.1  augustss 	usbd_status		err;
    685       1.1  augustss 
    686       1.1  augustss 	/* Open RX and TX pipes. */
    687       1.1  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
    688       1.1  augustss 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
    689       1.1  augustss 	if (err) {
    690       1.1  augustss 		printf("%s: open rx pipe failed: %s\n",
    691       1.1  augustss 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    692       1.1  augustss 		return (EIO);
    693       1.1  augustss 	}
    694       1.1  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
    695       1.1  augustss 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
    696       1.1  augustss 	if (err) {
    697       1.1  augustss 		printf("%s: open tx pipe failed: %s\n",
    698       1.1  augustss 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    699       1.1  augustss 		return (EIO);
    700       1.1  augustss 	}
    701       1.1  augustss 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
    702       1.1  augustss 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
    703      1.19  augustss 	    &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
    704       1.1  augustss 	    UPL_INTR_INTERVAL);
    705       1.1  augustss 	if (err) {
    706       1.1  augustss 		printf("%s: open intr pipe failed: %s\n",
    707       1.1  augustss 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    708       1.1  augustss 		return (EIO);
    709       1.1  augustss 	}
    710       1.1  augustss 
    711       1.1  augustss 	return (0);
    712       1.1  augustss }
    713       1.1  augustss 
    714       1.1  augustss Static void
    715      1.26  christos upl_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
    716      1.25  christos     usbd_status status)
    717       1.1  augustss {
    718       1.1  augustss 	struct upl_softc	*sc = priv;
    719       1.1  augustss 	struct ifnet		*ifp = &sc->sc_if;
    720       1.1  augustss 	uByte			stat;
    721       1.1  augustss 
    722      1.18  augustss 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
    723       1.1  augustss 
    724       1.1  augustss 	if (sc->sc_dying)
    725       1.1  augustss 		return;
    726       1.1  augustss 
    727       1.1  augustss 	if (!(ifp->if_flags & IFF_RUNNING))
    728       1.1  augustss 		return;
    729       1.1  augustss 
    730       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    731       1.1  augustss 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    732       1.1  augustss 			return;
    733       1.1  augustss 		}
    734       1.1  augustss 		sc->sc_intr_errs++;
    735       1.1  augustss 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
    736       1.1  augustss 			printf("%s: %u usb errors on intr: %s\n",
    737       1.1  augustss 			    USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
    738       1.1  augustss 			    usbd_errstr(status));
    739       1.1  augustss 			sc->sc_intr_errs = 0;
    740       1.1  augustss 		}
    741       1.1  augustss 		if (status == USBD_STALLED)
    742      1.23  augustss 			usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
    743       1.1  augustss 		return;
    744       1.1  augustss 	}
    745       1.1  augustss 
    746       1.1  augustss 	stat = sc->sc_ibuf;
    747       1.1  augustss 
    748       1.1  augustss 	if (stat == 0)
    749       1.1  augustss 		return;
    750       1.1  augustss 
    751       1.1  augustss 	DPRINTFN(10,("%s: %s: stat=0x%02x\n", USBDEVNAME(sc->sc_dev),
    752      1.18  augustss 		     __func__, stat));
    753       1.1  augustss 
    754       1.1  augustss }
    755       1.1  augustss 
    756       1.1  augustss Static int
    757       1.4  augustss upl_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
    758       1.1  augustss {
    759       1.1  augustss 	struct upl_softc	*sc = ifp->if_softc;
    760  1.27.4.2     itohy #if 0
    761       1.1  augustss 	struct ifaddr 		*ifa = (struct ifaddr *)data;
    762       1.1  augustss 	struct ifreq		*ifr = (struct ifreq *)data;
    763  1.27.4.2     itohy #endif
    764       1.1  augustss 	int			s, error = 0;
    765       1.1  augustss 
    766       1.1  augustss 	if (sc->sc_dying)
    767       1.1  augustss 		return (EIO);
    768       1.1  augustss 
    769       1.1  augustss 	DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
    770      1.18  augustss 		    USBDEVNAME(sc->sc_dev), __func__, command));
    771       1.1  augustss 
    772      1.14   thorpej 	s = splnet();
    773       1.1  augustss 
    774       1.1  augustss 	switch(command) {
    775  1.27.4.2     itohy #if 0
    776       1.1  augustss 	case SIOCSIFADDR:
    777       1.1  augustss 		ifp->if_flags |= IFF_UP;
    778  1.27.4.2     itohy 		upl_init(ifp);
    779       1.1  augustss 
    780       1.1  augustss 		switch (ifa->ifa_addr->sa_family) {
    781       1.1  augustss #ifdef INET
    782       1.1  augustss 		case AF_INET:
    783       1.1  augustss 			break;
    784       1.1  augustss #endif /* INET */
    785       1.1  augustss 		}
    786       1.1  augustss 		break;
    787       1.1  augustss 
    788       1.1  augustss 	case SIOCSIFMTU:
    789       1.1  augustss 		if (ifr->ifr_mtu > UPL_BUFSZ)
    790       1.1  augustss 			error = EINVAL;
    791       1.1  augustss 		else
    792       1.1  augustss 			ifp->if_mtu = ifr->ifr_mtu;
    793       1.1  augustss 		break;
    794       1.1  augustss 
    795       1.1  augustss 	case SIOCSIFFLAGS:
    796       1.1  augustss 		if (ifp->if_flags & IFF_UP) {
    797       1.1  augustss 			if (!(ifp->if_flags & IFF_RUNNING))
    798  1.27.4.2     itohy 				upl_init(ifp);
    799       1.1  augustss 		} else {
    800       1.1  augustss 			if (ifp->if_flags & IFF_RUNNING)
    801  1.27.4.2     itohy 				upl_stop(ifp, 1);
    802       1.1  augustss 		}
    803       1.1  augustss 		error = 0;
    804       1.1  augustss 		break;
    805  1.27.4.2     itohy #endif
    806       1.1  augustss 	default:
    807  1.27.4.2     itohy 		error = ether_ioctl(ifp, command, data);
    808  1.27.4.2     itohy 		if (error == ENETRESET) {
    809  1.27.4.2     itohy 			/*
    810  1.27.4.2     itohy 			 * Multicast list has changed; set the hardware
    811  1.27.4.2     itohy 			 * filter accordingly.
    812  1.27.4.2     itohy 			 */
    813  1.27.4.2     itohy #if 0	/* XXX not yet */
    814  1.27.4.2     itohy 			if (ifp->if_flags & IFF_RUNNING)
    815  1.27.4.2     itohy 				upl_setmulti(sc);
    816  1.27.4.2     itohy #endif
    817  1.27.4.2     itohy 			error = 0;
    818  1.27.4.2     itohy 		}
    819       1.1  augustss 		break;
    820       1.1  augustss 	}
    821       1.1  augustss 
    822       1.1  augustss 	splx(s);
    823       1.1  augustss 
    824       1.1  augustss 	return (error);
    825       1.1  augustss }
    826       1.1  augustss 
    827       1.1  augustss Static void
    828       1.4  augustss upl_watchdog(struct ifnet *ifp)
    829       1.1  augustss {
    830       1.1  augustss 	struct upl_softc	*sc = ifp->if_softc;
    831       1.1  augustss 
    832      1.18  augustss 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
    833       1.1  augustss 
    834       1.1  augustss 	if (sc->sc_dying)
    835       1.1  augustss 		return;
    836       1.1  augustss 
    837       1.1  augustss 	ifp->if_oerrors++;
    838       1.1  augustss 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
    839       1.1  augustss 
    840  1.27.4.2     itohy 	upl_stop(ifp, 1);
    841  1.27.4.2     itohy 	upl_init(ifp);
    842       1.1  augustss 
    843      1.17    itojun 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    844       1.1  augustss 		upl_start(ifp);
    845       1.1  augustss }
    846       1.1  augustss 
    847       1.1  augustss /*
    848       1.1  augustss  * Stop the adapter and free any mbufs allocated to the
    849       1.1  augustss  * RX and TX lists.
    850       1.1  augustss  */
    851       1.1  augustss Static void
    852  1.27.4.2     itohy upl_stop(struct ifnet *ifp, int disable)
    853       1.1  augustss {
    854  1.27.4.2     itohy 	struct upl_softc	*sc = ifp->if_softc;
    855       1.1  augustss 	usbd_status		err;
    856       1.1  augustss 
    857      1.18  augustss 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
    858       1.1  augustss 
    859       1.1  augustss 	ifp = &sc->sc_if;
    860       1.1  augustss 	ifp->if_timer = 0;
    861       1.1  augustss 
    862       1.1  augustss 	/* Stop transfers. */
    863       1.1  augustss 	if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
    864       1.1  augustss 		err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
    865       1.1  augustss 		if (err) {
    866       1.1  augustss 			printf("%s: abort rx pipe failed: %s\n",
    867       1.1  augustss 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    868       1.1  augustss 		}
    869       1.1  augustss 	}
    870       1.1  augustss 
    871       1.1  augustss 	if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
    872       1.1  augustss 		err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
    873       1.1  augustss 		if (err) {
    874       1.1  augustss 			printf("%s: abort tx pipe failed: %s\n",
    875       1.1  augustss 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    876       1.1  augustss 		}
    877       1.1  augustss 	}
    878       1.1  augustss 
    879       1.1  augustss 	if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
    880       1.1  augustss 		err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
    881       1.1  augustss 		if (err) {
    882       1.1  augustss 			printf("%s: abort intr pipe failed: %s\n",
    883       1.1  augustss 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    884       1.1  augustss 		}
    885  1.27.4.2     itohy 	}
    886  1.27.4.2     itohy 
    887  1.27.4.2     itohy 	/* Free RX/TX list resources. */
    888  1.27.4.2     itohy 	usb_ether_rx_list_free(sc->sc_cdata.upl_rx_chain, UPL_RX_LIST_CNT);
    889  1.27.4.2     itohy 	usb_ether_tx_list_free(sc->sc_cdata.upl_tx_chain, UPL_TX_LIST_CNT);
    890  1.27.4.2     itohy 
    891  1.27.4.2     itohy 	/* Close pipes. */
    892  1.27.4.2     itohy 	if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
    893  1.27.4.2     itohy 		err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
    894       1.1  augustss 		if (err) {
    895  1.27.4.2     itohy 			printf("%s: close rx pipe failed: %s\n",
    896  1.27.4.2     itohy 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    897       1.1  augustss 		}
    898  1.27.4.2     itohy 		sc->sc_ep[UPL_ENDPT_RX] = NULL;
    899       1.1  augustss 	}
    900       1.1  augustss 
    901  1.27.4.2     itohy 	if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
    902  1.27.4.2     itohy 		err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
    903  1.27.4.2     itohy 		if (err) {
    904  1.27.4.2     itohy 			printf("%s: close tx pipe failed: %s\n",
    905  1.27.4.2     itohy 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    906       1.1  augustss 		}
    907  1.27.4.2     itohy 		sc->sc_ep[UPL_ENDPT_TX] = NULL;
    908       1.1  augustss 	}
    909       1.1  augustss 
    910  1.27.4.2     itohy 	if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
    911  1.27.4.2     itohy 		err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
    912  1.27.4.2     itohy 		if (err) {
    913  1.27.4.2     itohy 			printf("%s: close intr pipe failed: %s\n",
    914  1.27.4.2     itohy 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    915       1.1  augustss 		}
    916  1.27.4.2     itohy 		sc->sc_ep[UPL_ENDPT_INTR] = NULL;
    917       1.1  augustss 	}
    918       1.1  augustss 
    919       1.1  augustss 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    920       1.1  augustss }
    921       1.1  augustss 
    922       1.1  augustss Static int
    923      1.27    dyoung upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    924      1.26  christos     struct rtentry *rt0)
    925       1.1  augustss {
    926      1.17    itojun 	int s, len, error;
    927      1.17    itojun 	ALTQ_DECL(struct altq_pktattr pktattr;)
    928       1.1  augustss 
    929       1.1  augustss 	DPRINTFN(10,("%s: %s: enter\n",
    930       1.1  augustss 		     USBDEVNAME(((struct upl_softc *)ifp->if_softc)->sc_dev),
    931      1.18  augustss 		     __func__));
    932       1.1  augustss 
    933      1.17    itojun 	/*
    934      1.17    itojun 	 * if the queueing discipline needs packet classification,
    935      1.17    itojun 	 * do it now.
    936      1.17    itojun 	 */
    937      1.17    itojun 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    938      1.17    itojun 
    939      1.17    itojun 	len = m->m_pkthdr.len;
    940      1.14   thorpej 	s = splnet();
    941       1.1  augustss 	/*
    942       1.1  augustss 	 * Queue message on interface, and start output if interface
    943       1.1  augustss 	 * not yet active.
    944       1.1  augustss 	 */
    945      1.17    itojun 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    946      1.17    itojun 	if (error) {
    947      1.17    itojun 		/* mbuf is already freed */
    948       1.1  augustss 		splx(s);
    949      1.17    itojun 		return (error);
    950       1.1  augustss 	}
    951      1.17    itojun 	ifp->if_obytes += len;
    952       1.1  augustss 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    953       1.1  augustss 		(*ifp->if_start)(ifp);
    954       1.1  augustss 	splx(s);
    955       1.1  augustss 
    956       1.1  augustss 	return (0);
    957       1.1  augustss }
    958       1.1  augustss 
    959       1.1  augustss Static void
    960       1.4  augustss upl_input(struct ifnet *ifp, struct mbuf *m)
    961       1.1  augustss {
    962      1.21  christos #ifdef INET
    963       1.1  augustss 	struct ifqueue *inq;
    964       1.1  augustss 	int s;
    965       1.1  augustss 
    966       1.1  augustss 	/* XXX Assume all traffic is IP */
    967       1.1  augustss 
    968       1.1  augustss 	schednetisr(NETISR_IP);
    969       1.1  augustss 	inq = &ipintrq;
    970       1.1  augustss 
    971      1.14   thorpej 	s = splnet();
    972       1.1  augustss 	if (IF_QFULL(inq)) {
    973       1.1  augustss 		IF_DROP(inq);
    974       1.1  augustss 		splx(s);
    975       1.7  augustss #if 0
    976       1.7  augustss 		if (sc->sc_flags & SC_DEBUG)
    977       1.7  augustss 			printf("%s: input queue full\n", ifp->if_xname);
    978       1.7  augustss #endif
    979       1.1  augustss 		ifp->if_iqdrops++;
    980       1.1  augustss 		return;
    981       1.1  augustss 	}
    982       1.1  augustss 	IF_ENQUEUE(inq, m);
    983       1.1  augustss 	splx(s);
    984      1.21  christos #endif
    985       1.1  augustss 	ifp->if_ipackets++;
    986       1.1  augustss 	ifp->if_ibytes += m->m_len;
    987       1.1  augustss }
    988