LCOV - code coverage report
Current view: top level - dev/usb - if_upl.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 388 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: if_upl.c,v 1.74 2017/10/29 21:54:22 mpi Exp $ */
       2             : /*      $NetBSD: if_upl.c,v 1.19 2002/07/11 21:14:26 augustss Exp $     */
       3             : /*
       4             :  * Copyright (c) 2000 The NetBSD Foundation, Inc.
       5             :  * All rights reserved.
       6             :  *
       7             :  * This code is derived from software contributed to The NetBSD Foundation
       8             :  * by Lennart Augustsson (lennart@augustsson.net) at
       9             :  * Carlstedt Research & Technology.
      10             :  *
      11             :  * Redistribution and use in source and binary forms, with or without
      12             :  * modification, are permitted provided that the following conditions
      13             :  * are met:
      14             :  * 1. Redistributions of source code must retain the above copyright
      15             :  *    notice, this list of conditions and the following disclaimer.
      16             :  * 2. Redistributions in binary form must reproduce the above copyright
      17             :  *    notice, this list of conditions and the following disclaimer in the
      18             :  *    documentation and/or other materials provided with the distribution.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
      21             :  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
      22             :  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
      23             :  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
      24             :  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      25             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      26             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      27             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      28             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      29             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      30             :  * POSSIBILITY OF SUCH DAMAGE.
      31             :  */
      32             : 
      33             : /*
      34             :  * Prolific PL2301/PL2302 driver
      35             :  */
      36             : 
      37             : #include "bpfilter.h"
      38             : 
      39             : #include <sys/param.h>
      40             : #include <sys/systm.h>
      41             : #include <sys/timeout.h>
      42             : #include <sys/sockio.h>
      43             : #include <sys/mbuf.h>
      44             : #include <sys/kernel.h>
      45             : #include <sys/socket.h>
      46             : 
      47             : #include <sys/device.h>
      48             : 
      49             : #include <net/if.h>
      50             : #include <net/if_types.h>
      51             : 
      52             : #if NBPFILTER > 0
      53             : #include <net/bpf.h>
      54             : #endif
      55             : 
      56             : #include <netinet/in.h>
      57             : #include <netinet/if_ether.h>
      58             : 
      59             : #include <dev/usb/usb.h>
      60             : #include <dev/usb/usbdi.h>
      61             : #include <dev/usb/usbdi_util.h>
      62             : #include <dev/usb/usbdevs.h>
      63             : 
      64             : /*
      65             :  * 7  6  5  4  3  2  1  0
      66             :  * tx rx 1  0
      67             :  * 1110 0000 rxdata
      68             :  * 1010 0000 idle
      69             :  * 0010 0000 tx over
      70             :  * 0110      tx over + rxd
      71             :  */
      72             : 
      73             : #define UPL_RXDATA              0x40
      74             : #define UPL_TXOK                0x80
      75             : 
      76             : #define UPL_INTR_PKTLEN         1
      77             : 
      78             : #define UPL_CONFIG_NO           1
      79             : #define UPL_IFACE_IDX           0
      80             : 
      81             : /***/
      82             : 
      83             : #define UPL_INTR_INTERVAL       20
      84             : 
      85             : #define UPL_BUFSZ               1024
      86             : 
      87             : #define UPL_RX_FRAMES           1
      88             : #define UPL_TX_FRAMES           1
      89             : 
      90             : #define UPL_RX_LIST_CNT         1
      91             : #define UPL_TX_LIST_CNT         1
      92             : 
      93             : #define UPL_ENDPT_RX            0x0
      94             : #define UPL_ENDPT_TX            0x1
      95             : #define UPL_ENDPT_INTR          0x2
      96             : #define UPL_ENDPT_MAX           0x3
      97             : 
      98             : struct upl_softc;
      99             : 
     100             : struct upl_chain {
     101             :         struct upl_softc        *upl_sc;
     102             :         struct usbd_xfer        *upl_xfer;
     103             :         char                    *upl_buf;
     104             :         struct mbuf             *upl_mbuf;
     105             :         int                     upl_idx;
     106             : };
     107             : 
     108             : struct upl_cdata {
     109             :         struct upl_chain        upl_tx_chain[UPL_TX_LIST_CNT];
     110             :         struct upl_chain        upl_rx_chain[UPL_RX_LIST_CNT];
     111             :         int                     upl_tx_prod;
     112             :         int                     upl_tx_cons;
     113             :         int                     upl_tx_cnt;
     114             :         int                     upl_rx_prod;
     115             : };
     116             : 
     117             : struct upl_softc {
     118             :         struct device           sc_dev;
     119             : 
     120             :         struct ifnet            sc_if;
     121             :         struct timeout          sc_stat_ch;
     122             : 
     123             :         struct usbd_device      *sc_udev;
     124             :         struct usbd_interface   *sc_iface;
     125             :         int                     sc_ed[UPL_ENDPT_MAX];
     126             :         struct usbd_pipe        *sc_ep[UPL_ENDPT_MAX];
     127             :         struct upl_cdata        sc_cdata;
     128             : 
     129             :         uByte                   sc_ibuf;
     130             : 
     131             :         u_int                   sc_rx_errs;
     132             :         struct timeval          sc_rx_notice;
     133             :         u_int                   sc_intr_errs;
     134             : };
     135             : 
     136             : #ifdef UPL_DEBUG
     137             : #define DPRINTF(x)      do { if (upldebug) printf x; } while (0)
     138             : #define DPRINTFN(n,x)   do { if (upldebug >= (n)) printf x; } while (0)
     139             : int     upldebug = 0;
     140             : #else
     141             : #define DPRINTF(x)
     142             : #define DPRINTFN(n,x)
     143             : #endif
     144             : 
     145             : /*
     146             :  * Various supported device vendors/products.
     147             :  */
     148             : struct usb_devno upl_devs[] = {
     149             :         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 },
     150             :         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 }
     151             : };
     152             : 
     153             : int upl_match(struct device *, void *, void *);
     154             : void upl_attach(struct device *, struct device *, void *);
     155             : int upl_detach(struct device *, int);
     156             : 
     157             : struct cfdriver upl_cd = {
     158             :         NULL, "upl", DV_IFNET
     159             : };
     160             : 
     161             : const struct cfattach upl_ca = {
     162             :         sizeof(struct upl_softc), upl_match, upl_attach, upl_detach
     163             : };
     164             : 
     165             : int upl_openpipes(struct upl_softc *);
     166             : int upl_tx_list_init(struct upl_softc *);
     167             : int upl_rx_list_init(struct upl_softc *);
     168             : int upl_newbuf(struct upl_softc *, struct upl_chain *, struct mbuf *);
     169             : int upl_send(struct upl_softc *, struct mbuf *, int);
     170             : void upl_intr(struct usbd_xfer *, void *, usbd_status);
     171             : void upl_rxeof(struct usbd_xfer *, void *, usbd_status);
     172             : void upl_txeof(struct usbd_xfer *, void *, usbd_status);
     173             : void upl_start(struct ifnet *);
     174             : int upl_ioctl(struct ifnet *, u_long, caddr_t);
     175             : void upl_init(void *);
     176             : void upl_stop(struct upl_softc *);
     177             : void upl_watchdog(struct ifnet *);
     178             : 
     179             : int upl_output(struct ifnet *, struct mbuf *, struct sockaddr *,
     180             :                       struct rtentry *);
     181             : 
     182             : /*
     183             :  * Probe for a Prolific chip.
     184             :  */
     185             : int
     186           0 : upl_match(struct device *parent, void *match, void *aux)
     187             : {
     188           0 :         struct usb_attach_arg           *uaa = aux;
     189             : 
     190           0 :         if (uaa->iface == NULL || uaa->configno != UPL_CONFIG_NO)
     191           0 :                 return (UMATCH_NONE);
     192             : 
     193           0 :         return (usb_lookup(upl_devs, uaa->vendor, uaa->product) != NULL ?
     194             :             UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
     195           0 : }
     196             : 
     197             : void
     198           0 : upl_attach(struct device *parent, struct device *self, void *aux)
     199             : {
     200           0 :         struct upl_softc        *sc = (struct upl_softc *)self;
     201           0 :         struct usb_attach_arg   *uaa = aux;
     202             :         int                     s;
     203           0 :         struct usbd_device      *dev = uaa->device;
     204           0 :         struct usbd_interface   *iface;
     205             :         usbd_status             err;
     206             :         struct ifnet            *ifp;
     207             :         usb_interface_descriptor_t      *id;
     208             :         usb_endpoint_descriptor_t       *ed;
     209             :         int                     i;
     210             : 
     211             :         DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev));
     212             : 
     213           0 :         sc->sc_udev = dev;
     214             : 
     215           0 :         err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface);
     216           0 :         if (err) {
     217           0 :                 printf("%s: getting interface handle failed\n",
     218           0 :                     sc->sc_dev.dv_xname);
     219           0 :                 return;
     220             :         }
     221             : 
     222           0 :         sc->sc_iface = iface;
     223           0 :         id = usbd_get_interface_descriptor(iface);
     224             : 
     225             :         /* Find endpoints. */
     226           0 :         for (i = 0; i < id->bNumEndpoints; i++) {
     227           0 :                 ed = usbd_interface2endpoint_descriptor(iface, i);
     228           0 :                 if (ed == NULL) {
     229           0 :                         printf("%s: couldn't get ep %d\n",
     230           0 :                             sc->sc_dev.dv_xname, i);
     231           0 :                         return;
     232             :                 }
     233           0 :                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
     234           0 :                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
     235           0 :                         sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress;
     236           0 :                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
     237           0 :                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
     238           0 :                         sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress;
     239           0 :                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
     240           0 :                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
     241           0 :                         sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress;
     242           0 :                 }
     243             :         }
     244             : 
     245           0 :         if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 ||
     246           0 :             sc->sc_ed[UPL_ENDPT_INTR] == 0) {
     247           0 :                 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
     248           0 :                 return;
     249             :         }
     250             : 
     251           0 :         s = splnet();
     252             : 
     253             :         /* Initialize interface info.*/
     254           0 :         ifp = &sc->sc_if;
     255           0 :         ifp->if_softc = sc;
     256           0 :         ifp->if_mtu = UPL_BUFSZ;
     257           0 :         ifp->if_hardmtu = UPL_BUFSZ;
     258           0 :         ifp->if_flags = IFF_POINTOPOINT | IFF_SIMPLEX;
     259           0 :         ifp->if_ioctl = upl_ioctl;
     260           0 :         ifp->if_start = upl_start;
     261           0 :         ifp->if_watchdog = upl_watchdog;
     262           0 :         strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
     263             : 
     264           0 :         ifp->if_type = IFT_OTHER;
     265           0 :         ifp->if_addrlen = 0;
     266           0 :         ifp->if_hdrlen = 0;
     267           0 :         ifp->if_output = upl_output;
     268           0 :         ifp->if_baudrate = IF_Mbps(12);
     269             : 
     270             :         /* Attach the interface. */
     271           0 :         if_attach(ifp);
     272           0 :         if_alloc_sadl(ifp);
     273             : 
     274           0 :         splx(s);
     275           0 : }
     276             : 
     277             : int
     278           0 : upl_detach(struct device *self, int flags)
     279             : {
     280           0 :         struct upl_softc        *sc = (struct upl_softc *)self;
     281           0 :         struct ifnet            *ifp = &sc->sc_if;
     282             :         int                     s;
     283             : 
     284             :         DPRINTFN(2,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__));
     285             : 
     286           0 :         s = splusb();
     287             : 
     288           0 :         if (ifp->if_flags & IFF_RUNNING)
     289           0 :                 upl_stop(sc);
     290             : 
     291           0 :         if (ifp->if_softc != NULL)
     292           0 :                 if_detach(ifp);
     293             : 
     294             : #ifdef DIAGNOSTIC
     295           0 :         if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
     296           0 :             sc->sc_ep[UPL_ENDPT_RX] != NULL ||
     297           0 :             sc->sc_ep[UPL_ENDPT_INTR] != NULL)
     298           0 :                 printf("%s: detach has active endpoints\n",
     299           0 :                        sc->sc_dev.dv_xname);
     300             : #endif
     301             : 
     302           0 :         splx(s);
     303             : 
     304           0 :         return (0);
     305             : }
     306             : 
     307             : /*
     308             :  * Initialize an RX descriptor and attach an MBUF cluster.
     309             :  */
     310             : int
     311           0 : upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m)
     312             : {
     313             :         struct mbuf             *m_new = NULL;
     314             : 
     315             :         DPRINTFN(8,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__));
     316             : 
     317           0 :         if (m == NULL) {
     318           0 :                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
     319           0 :                 if (m_new == NULL) {
     320           0 :                         printf("%s: no memory for rx list "
     321           0 :                             "-- packet dropped!\n", sc->sc_dev.dv_xname);
     322           0 :                         return (ENOBUFS);
     323             :                 }
     324             : 
     325           0 :                 MCLGET(m_new, M_DONTWAIT);
     326           0 :                 if (!(m_new->m_flags & M_EXT)) {
     327           0 :                         printf("%s: no memory for rx list "
     328           0 :                             "-- packet dropped!\n", sc->sc_dev.dv_xname);
     329           0 :                         m_freem(m_new);
     330           0 :                         return (ENOBUFS);
     331             :                 }
     332           0 :                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
     333           0 :         } else {
     334             :                 m_new = m;
     335           0 :                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
     336           0 :                 m_new->m_data = m_new->m_ext.ext_buf;
     337             :         }
     338             : 
     339           0 :         c->upl_mbuf = m_new;
     340             : 
     341           0 :         return (0);
     342           0 : }
     343             : 
     344             : int
     345           0 : upl_rx_list_init(struct upl_softc *sc)
     346             : {
     347             :         struct upl_cdata        *cd;
     348             :         struct upl_chain        *c;
     349             :         int                     i;
     350             : 
     351             :         DPRINTFN(5,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__));
     352             : 
     353           0 :         cd = &sc->sc_cdata;
     354           0 :         for (i = 0; i < UPL_RX_LIST_CNT; i++) {
     355           0 :                 c = &cd->upl_rx_chain[i];
     356           0 :                 c->upl_sc = sc;
     357           0 :                 c->upl_idx = i;
     358           0 :                 if (upl_newbuf(sc, c, NULL) == ENOBUFS)
     359           0 :                         return (ENOBUFS);
     360           0 :                 if (c->upl_xfer == NULL) {
     361           0 :                         c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
     362           0 :                         if (c->upl_xfer == NULL)
     363           0 :                                 return (ENOBUFS);
     364           0 :                         c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
     365           0 :                         if (c->upl_buf == NULL) {
     366           0 :                                 usbd_free_xfer(c->upl_xfer);
     367           0 :                                 return (ENOBUFS);
     368             :                         }
     369             :                 }
     370             :         }
     371             : 
     372           0 :         return (0);
     373           0 : }
     374             : 
     375             : int
     376           0 : upl_tx_list_init(struct upl_softc *sc)
     377             : {
     378             :         struct upl_cdata        *cd;
     379             :         struct upl_chain        *c;
     380             :         int                     i;
     381             : 
     382             :         DPRINTFN(5,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__));
     383             : 
     384           0 :         cd = &sc->sc_cdata;
     385           0 :         for (i = 0; i < UPL_TX_LIST_CNT; i++) {
     386           0 :                 c = &cd->upl_tx_chain[i];
     387           0 :                 c->upl_sc = sc;
     388           0 :                 c->upl_idx = i;
     389           0 :                 c->upl_mbuf = NULL;
     390           0 :                 if (c->upl_xfer == NULL) {
     391           0 :                         c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
     392           0 :                         if (c->upl_xfer == NULL)
     393           0 :                                 return (ENOBUFS);
     394           0 :                         c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
     395           0 :                         if (c->upl_buf == NULL) {
     396           0 :                                 usbd_free_xfer(c->upl_xfer);
     397           0 :                                 return (ENOBUFS);
     398             :                         }
     399             :                 }
     400             :         }
     401             : 
     402           0 :         return (0);
     403           0 : }
     404             : 
     405             : /*
     406             :  * A frame has been uploaded: pass the resulting mbuf chain up to
     407             :  * the higher level protocols.
     408             :  */
     409             : void
     410           0 : upl_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
     411             : {
     412           0 :         struct upl_chain        *c = priv;
     413           0 :         struct upl_softc        *sc = c->upl_sc;
     414           0 :         struct ifnet            *ifp = &sc->sc_if;
     415           0 :         struct mbuf_list        ml = MBUF_LIST_INITIALIZER();
     416             :         struct mbuf             *m;
     417           0 :         int                     total_len = 0;
     418             :         int                     s;
     419             : 
     420           0 :         if (usbd_is_dying(sc->sc_udev))
     421           0 :                 return;
     422             : 
     423           0 :         if (!(ifp->if_flags & IFF_RUNNING))
     424           0 :                 return;
     425             : 
     426           0 :         if (status != USBD_NORMAL_COMPLETION) {
     427           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
     428           0 :                         return;
     429           0 :                 sc->sc_rx_errs++;
     430           0 :                 if (usbd_ratecheck(&sc->sc_rx_notice)) {
     431           0 :                         printf("%s: %u usb errors on rx: %s\n",
     432           0 :                             sc->sc_dev.dv_xname, sc->sc_rx_errs,
     433           0 :                             usbd_errstr(status));
     434           0 :                         sc->sc_rx_errs = 0;
     435           0 :                 }
     436           0 :                 if (status == USBD_STALLED)
     437           0 :                         usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
     438             :                 goto done;
     439             :         }
     440             : 
     441           0 :         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
     442             : 
     443             :         DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
     444             :                     sc->sc_dev.dv_xname, __func__, status, total_len));
     445             : 
     446           0 :         m = c->upl_mbuf;
     447           0 :         memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len);
     448             : 
     449           0 :         m->m_pkthdr.len = m->m_len = total_len;
     450           0 :         ml_enqueue(&ml, m);
     451             : 
     452           0 :         if (upl_newbuf(sc, c, NULL) == ENOBUFS) {
     453           0 :                 ifp->if_ierrors++;
     454           0 :                 goto done;
     455             :         }
     456             : 
     457           0 :         s = splnet();
     458           0 :         if_input(ifp, &ml);
     459           0 :         splx(s);
     460             :  done:
     461             : #if 1
     462             :         /* Setup new transfer. */
     463           0 :         usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
     464           0 :             c, c->upl_buf, UPL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
     465             :             USBD_NO_TIMEOUT, upl_rxeof);
     466           0 :         usbd_transfer(c->upl_xfer);
     467             : 
     468             :         DPRINTFN(10,("%s: %s: start rx\n", sc->sc_dev.dv_xname,
     469             :                     __func__));
     470             : #endif
     471           0 : }
     472             : 
     473             : /*
     474             :  * A frame was downloaded to the chip. It's safe for us to clean up
     475             :  * the list buffers.
     476             :  */
     477             : void
     478           0 : upl_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
     479             : {
     480           0 :         struct upl_chain        *c = priv;
     481           0 :         struct upl_softc        *sc = c->upl_sc;
     482           0 :         struct ifnet            *ifp = &sc->sc_if;
     483             :         int                     s;
     484             : 
     485           0 :         if (usbd_is_dying(sc->sc_udev))
     486           0 :                 return;
     487             : 
     488           0 :         s = splnet();
     489             : 
     490             :         DPRINTFN(10,("%s: %s: enter status=%d\n", sc->sc_dev.dv_xname,
     491             :                     __func__, status));
     492             : 
     493           0 :         ifp->if_timer = 0;
     494           0 :         ifq_clr_oactive(&ifp->if_snd);
     495             : 
     496           0 :         if (status != USBD_NORMAL_COMPLETION) {
     497           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
     498           0 :                         splx(s);
     499           0 :                         return;
     500             :                 }
     501           0 :                 ifp->if_oerrors++;
     502           0 :                 printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname,
     503           0 :                     usbd_errstr(status));
     504           0 :                 if (status == USBD_STALLED)
     505           0 :                         usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]);
     506           0 :                 splx(s);
     507           0 :                 return;
     508             :         }
     509             : 
     510           0 :         m_freem(c->upl_mbuf);
     511           0 :         c->upl_mbuf = NULL;
     512             : 
     513           0 :         if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
     514           0 :                 upl_start(ifp);
     515             : 
     516           0 :         splx(s);
     517           0 : }
     518             : 
     519             : int
     520           0 : upl_send(struct upl_softc *sc, struct mbuf *m, int idx)
     521             : {
     522             :         int                     total_len;
     523             :         struct upl_chain        *c;
     524             :         usbd_status             err;
     525             : 
     526           0 :         c = &sc->sc_cdata.upl_tx_chain[idx];
     527             : 
     528             :         /*
     529             :          * Copy the mbuf data into a contiguous buffer, leaving two
     530             :          * bytes at the beginning to hold the frame length.
     531             :          */
     532           0 :         m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf);
     533           0 :         c->upl_mbuf = m;
     534             : 
     535           0 :         total_len = m->m_pkthdr.len;
     536             : 
     537             :         DPRINTFN(10,("%s: %s: total_len=%d\n",
     538             :                      sc->sc_dev.dv_xname, __func__, total_len));
     539             : 
     540           0 :         usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_TX],
     541           0 :             c, c->upl_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
     542             :             upl_txeof);
     543             : 
     544             :         /* Transmit */
     545           0 :         err = usbd_transfer(c->upl_xfer);
     546           0 :         if (err != USBD_IN_PROGRESS) {
     547           0 :                 printf("%s: upl_send error=%s\n", sc->sc_dev.dv_xname,
     548           0 :                        usbd_errstr(err));
     549           0 :                 upl_stop(sc);
     550           0 :                 return (EIO);
     551             :         }
     552             : 
     553           0 :         sc->sc_cdata.upl_tx_cnt++;
     554             : 
     555           0 :         return (0);
     556           0 : }
     557             : 
     558             : void
     559           0 : upl_start(struct ifnet *ifp)
     560             : {
     561           0 :         struct upl_softc        *sc = ifp->if_softc;
     562             :         struct mbuf             *m_head = NULL;
     563             : 
     564           0 :         if (usbd_is_dying(sc->sc_udev))
     565           0 :                 return;
     566             : 
     567             :         DPRINTFN(10,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__));
     568             : 
     569           0 :         if (ifq_is_oactive(&ifp->if_snd))
     570           0 :                 return;
     571             : 
     572           0 :         m_head = ifq_deq_begin(&ifp->if_snd);
     573           0 :         if (m_head == NULL)
     574           0 :                 return;
     575             : 
     576           0 :         if (upl_send(sc, m_head, 0)) {
     577           0 :                 ifq_deq_rollback(&ifp->if_snd, m_head);
     578           0 :                 ifq_set_oactive(&ifp->if_snd);
     579           0 :                 return;
     580             :         }
     581             : 
     582           0 :         ifq_deq_commit(&ifp->if_snd, m_head);
     583             : 
     584             : #if NBPFILTER > 0
     585             :         /*
     586             :          * If there's a BPF listener, bounce a copy of this frame
     587             :          * to him.
     588             :          */
     589           0 :         if (ifp->if_bpf)
     590           0 :                 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
     591             : #endif
     592             : 
     593           0 :         ifq_set_oactive(&ifp->if_snd);
     594             : 
     595             :         /*
     596             :          * Set a timeout in case the chip goes out to lunch.
     597             :          */
     598           0 :         ifp->if_timer = 5;
     599           0 : }
     600             : 
     601             : void
     602           0 : upl_init(void *xsc)
     603             : {
     604           0 :         struct upl_softc        *sc = xsc;
     605           0 :         struct ifnet            *ifp = &sc->sc_if;
     606             :         int                     s;
     607             : 
     608           0 :         if (usbd_is_dying(sc->sc_udev))
     609           0 :                 return;
     610             : 
     611             :         DPRINTFN(10,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__));
     612             : 
     613           0 :         s = splnet();
     614             : 
     615             :         /* Init TX ring. */
     616           0 :         if (upl_tx_list_init(sc) == ENOBUFS) {
     617           0 :                 printf("%s: tx list init failed\n", sc->sc_dev.dv_xname);
     618           0 :                 splx(s);
     619           0 :                 return;
     620             :         }
     621             : 
     622             :         /* Init RX ring. */
     623           0 :         if (upl_rx_list_init(sc) == ENOBUFS) {
     624           0 :                 printf("%s: rx list init failed\n", sc->sc_dev.dv_xname);
     625           0 :                 splx(s);
     626           0 :                 return;
     627             :         }
     628             : 
     629           0 :         if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
     630           0 :                 if (upl_openpipes(sc)) {
     631           0 :                         splx(s);
     632           0 :                         return;
     633             :                 }
     634             :         }
     635             : 
     636           0 :         ifp->if_flags |= IFF_RUNNING;
     637           0 :         ifq_clr_oactive(&ifp->if_snd);
     638             : 
     639           0 :         splx(s);
     640           0 : }
     641             : 
     642             : int
     643           0 : upl_openpipes(struct upl_softc *sc)
     644             : {
     645             :         struct upl_chain        *c;
     646             :         usbd_status             err;
     647             :         int                     i;
     648             : 
     649             :         /* Open RX and TX pipes. */
     650           0 :         err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
     651           0 :             USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
     652           0 :         if (err) {
     653           0 :                 printf("%s: open rx pipe failed: %s\n",
     654           0 :                     sc->sc_dev.dv_xname, usbd_errstr(err));
     655           0 :                 return (EIO);
     656             :         }
     657           0 :         err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
     658           0 :             USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
     659           0 :         if (err) {
     660           0 :                 printf("%s: open tx pipe failed: %s\n",
     661           0 :                     sc->sc_dev.dv_xname, usbd_errstr(err));
     662           0 :                 return (EIO);
     663             :         }
     664           0 :         err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
     665           0 :             USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
     666           0 :             &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
     667             :             UPL_INTR_INTERVAL);
     668           0 :         if (err) {
     669           0 :                 printf("%s: open intr pipe failed: %s\n",
     670           0 :                     sc->sc_dev.dv_xname, usbd_errstr(err));
     671           0 :                 return (EIO);
     672             :         }
     673             : 
     674             : 
     675             : #if 1
     676             :         /* Start up the receive pipe. */
     677           0 :         for (i = 0; i < UPL_RX_LIST_CNT; i++) {
     678           0 :                 c = &sc->sc_cdata.upl_rx_chain[i];
     679           0 :                 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
     680           0 :                     c, c->upl_buf, UPL_BUFSZ,
     681             :                     USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
     682             :                     upl_rxeof);
     683           0 :                 usbd_transfer(c->upl_xfer);
     684             :         }
     685             : #endif
     686             : 
     687           0 :         return (0);
     688           0 : }
     689             : 
     690             : void
     691           0 : upl_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
     692             : {
     693           0 :         struct upl_softc        *sc = priv;
     694           0 :         struct ifnet            *ifp = &sc->sc_if;
     695             :         uByte                   stat;
     696             : 
     697             :         DPRINTFN(15,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__));
     698             : 
     699           0 :         if (usbd_is_dying(sc->sc_udev))
     700           0 :                 return;
     701             : 
     702           0 :         if (!(ifp->if_flags & IFF_RUNNING))
     703           0 :                 return;
     704             : 
     705           0 :         if (status != USBD_NORMAL_COMPLETION) {
     706           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
     707           0 :                         return;
     708             :                 }
     709           0 :                 sc->sc_intr_errs++;
     710           0 :                 if (usbd_ratecheck(&sc->sc_rx_notice)) {
     711           0 :                         printf("%s: %u usb errors on intr: %s\n",
     712           0 :                             sc->sc_dev.dv_xname, sc->sc_rx_errs,
     713           0 :                             usbd_errstr(status));
     714           0 :                         sc->sc_intr_errs = 0;
     715           0 :                 }
     716           0 :                 if (status == USBD_STALLED)
     717           0 :                         usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
     718           0 :                 return;
     719             :         }
     720             : 
     721           0 :         stat = sc->sc_ibuf;
     722             : 
     723           0 :         if (stat == 0)
     724           0 :                 return;
     725             : 
     726             :         DPRINTFN(10,("%s: %s: stat=0x%02x\n", sc->sc_dev.dv_xname,
     727             :                      __func__, stat));
     728             : 
     729           0 : }
     730             : 
     731             : int
     732           0 : upl_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
     733             : {
     734           0 :         struct upl_softc        *sc = ifp->if_softc;
     735           0 :         struct ifreq            *ifr = (struct ifreq *)data;
     736             :         int                     s, error = 0;
     737             : 
     738           0 :         if (usbd_is_dying(sc->sc_udev))
     739           0 :                 return (EIO);
     740             : 
     741             :         DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
     742             :                     sc->sc_dev.dv_xname, __func__, command));
     743             : 
     744           0 :         s = splnet();
     745             : 
     746           0 :         switch(command) {
     747             :         case SIOCSIFADDR:
     748           0 :                 ifp->if_flags |= IFF_UP;
     749           0 :                 if (!(ifp->if_flags & IFF_RUNNING))
     750           0 :                         upl_init(sc);
     751             :                 break;
     752             : 
     753             :         case SIOCSIFFLAGS:
     754           0 :                 if (ifp->if_flags & IFF_UP) {
     755           0 :                         if (ifp->if_flags & IFF_RUNNING)
     756           0 :                                 error = ENETRESET;
     757             :                         else
     758           0 :                                 upl_init(sc);
     759             :                 } else {
     760           0 :                         if (ifp->if_flags & IFF_RUNNING)
     761           0 :                                 upl_stop(sc);
     762             :                 }
     763             :                 break;
     764             : 
     765             :         case SIOCSIFMTU:
     766           0 :                 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu)
     767           0 :                         error = EINVAL;
     768             :                 else
     769           0 :                         ifp->if_mtu = ifr->ifr_mtu;
     770             :                 break;
     771             : 
     772             :         default:
     773             :                 error = ENOTTY;
     774           0 :         }
     775             : 
     776           0 :         if (error == ENETRESET)
     777           0 :                 error = 0;
     778             : 
     779           0 :         splx(s);
     780           0 :         return (error);
     781           0 : }
     782             : 
     783             : void
     784           0 : upl_watchdog(struct ifnet *ifp)
     785             : {
     786           0 :         struct upl_softc        *sc = ifp->if_softc;
     787             : 
     788             :         DPRINTFN(5,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__));
     789             : 
     790           0 :         if (usbd_is_dying(sc->sc_udev))
     791           0 :                 return;
     792             : 
     793           0 :         ifp->if_oerrors++;
     794           0 :         printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
     795             : 
     796           0 :         upl_stop(sc);
     797           0 :         upl_init(sc);
     798             : 
     799           0 :         if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
     800           0 :                 upl_start(ifp);
     801           0 : }
     802             : 
     803             : /*
     804             :  * Stop the adapter and free any mbufs allocated to the
     805             :  * RX and TX lists.
     806             :  */
     807             : void
     808           0 : upl_stop(struct upl_softc *sc)
     809             : {
     810             :         usbd_status             err;
     811             :         struct ifnet            *ifp;
     812             :         int                     i;
     813             : 
     814             :         DPRINTFN(10,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__));
     815             : 
     816           0 :         ifp = &sc->sc_if;
     817           0 :         ifp->if_timer = 0;
     818           0 :         ifp->if_flags &= ~IFF_RUNNING;
     819           0 :         ifq_clr_oactive(&ifp->if_snd);
     820             : 
     821             :         /* Stop transfers. */
     822           0 :         if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
     823           0 :                 usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
     824           0 :                 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
     825           0 :                 if (err) {
     826           0 :                         printf("%s: close rx pipe failed: %s\n",
     827           0 :                         sc->sc_dev.dv_xname, usbd_errstr(err));
     828           0 :                 }
     829           0 :                 sc->sc_ep[UPL_ENDPT_RX] = NULL;
     830           0 :         }
     831             : 
     832           0 :         if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
     833           0 :                 usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
     834           0 :                 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
     835           0 :                 if (err) {
     836           0 :                         printf("%s: close tx pipe failed: %s\n",
     837           0 :                             sc->sc_dev.dv_xname, usbd_errstr(err));
     838           0 :                 }
     839           0 :                 sc->sc_ep[UPL_ENDPT_TX] = NULL;
     840           0 :         }
     841             : 
     842           0 :         if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
     843           0 :                 usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
     844           0 :                 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
     845           0 :                 if (err) {
     846           0 :                         printf("%s: close intr pipe failed: %s\n",
     847           0 :                             sc->sc_dev.dv_xname, usbd_errstr(err));
     848           0 :                 }
     849           0 :                 sc->sc_ep[UPL_ENDPT_INTR] = NULL;
     850           0 :         }
     851             : 
     852             :         /* Free RX resources. */
     853           0 :         for (i = 0; i < UPL_RX_LIST_CNT; i++) {
     854           0 :                 if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) {
     855           0 :                         m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf);
     856           0 :                         sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL;
     857           0 :                 }
     858           0 :                 if (sc->sc_cdata.upl_rx_chain[i].upl_xfer != NULL) {
     859           0 :                         usbd_free_xfer(sc->sc_cdata.upl_rx_chain[i].upl_xfer);
     860           0 :                         sc->sc_cdata.upl_rx_chain[i].upl_xfer = NULL;
     861           0 :                 }
     862             :         }
     863             : 
     864             :         /* Free TX resources. */
     865           0 :         for (i = 0; i < UPL_TX_LIST_CNT; i++) {
     866           0 :                 if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) {
     867           0 :                         m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf);
     868           0 :                         sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL;
     869           0 :                 }
     870           0 :                 if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) {
     871           0 :                         usbd_free_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer);
     872           0 :                         sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL;
     873           0 :                 }
     874             :         }
     875           0 : }
     876             : 
     877             : int
     878           0 : upl_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
     879             :            struct rtentry *rt0)
     880             : {
     881           0 :         return (if_enqueue(ifp, m));
     882             : }

Generated by: LCOV version 1.13