Author: ian
Date: Sun Feb  5 15:45:31 2017
New Revision: 313287
URL: https://svnweb.freebsd.org/changeset/base/313287

Log:
  Add tsw_busy support to usb_serial (ucom).
  
  The tty layer uses tsw_busy to poll for busy/idle status of the transmitter
  hardware during close() and tcdrain(). The ucom layer defines ULSR_TXRDY and
  ULSR_TSRE bits for the line status register; when both are set, the
  transmitter is idle. Not all chip drivers maintain those bits in the sc_lsr
  field, and if the bits never get set the transmitter will always appear
  busy, causing hangs in tcdrain().
  
  These changes add a new sc_flag bit, UCOM_FLAG_LSRTXIDLE. When this flag is
  set, ucom_busy() uses the lsr bits to return busy vs. idle state, otherwise
  it always returns idle (which is effectively what happened before this
  change because tsw_busy wasn't implemented).
  
  For the uftdi chip driver, these changes stop masking out the tx idle bits
  when processing the status register (because now they're useful), and it
  calls ucom_use_lsr_txbits() to indicate the bits are maintained by the
  driver and can be used by ucom_busy().
  
  Differential Revision:        https://reviews.freebsd.org/D9183

Modified:
  head/sys/dev/usb/serial/uftdi.c
  head/sys/dev/usb/serial/usb_serial.c
  head/sys/dev/usb/serial/usb_serial.h

Modified: head/sys/dev/usb/serial/uftdi.c
==============================================================================
--- head/sys/dev/usb/serial/uftdi.c     Sun Feb  5 14:25:31 2017        
(r313286)
+++ head/sys/dev/usb/serial/uftdi.c     Sun Feb  5 15:45:31 2017        
(r313287)
@@ -1123,6 +1123,9 @@ uftdi_attach(device_t dev)
            FTDI_SIO_SET_DATA_PARITY_NONE |
            FTDI_SIO_SET_DATA_BITS(8));
 
+       /* Indicate tx bits in sc_lsr can be used to determine busy vs idle. */
+       ucom_use_lsr_txbits(&sc->sc_ucom);
+
        error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
            &uftdi_callback, &sc->sc_mtx);
        if (error) {
@@ -1279,16 +1282,20 @@ uftdi_read_callback(struct usb_xfer *xfe
                offset = 0;
                /*
                 * Extract packet headers and payload bytes from the buffer.
-                * Feed payload bytes to ucom/tty layer; OR-accumulate header
-                * status bits which are transient and could toggle with each
-                * packet. After processing all packets in the buffer, process
-                * the accumulated transient MSR and LSR values along with the
+                * Feed payload bytes to ucom/tty layer; OR-accumulate the
+                * receiver-related header status bits which are transient and
+                * could toggle with each packet, but for transmitter-related
+                * bits keep only the ones from the last packet.
+                *
+                * After processing all packets in the buffer, process the
+                * accumulated transient MSR and LSR values along with the
                 * non-transient bits from the last packet header.
                 */
                while (buflen >= UFTDI_IHDRSIZE) {
                        usbd_copy_out(pc, offset, buf, UFTDI_IHDRSIZE);
                        offset += UFTDI_IHDRSIZE;
                        buflen -= UFTDI_IHDRSIZE;
+                       lsr &= ~(ULSR_TXRDY | ULSR_TSRE);
                        lsr |= FTDI_GET_LSR(buf);
                        if (FTDI_GET_MSR(buf) & FTDI_SIO_RI_MASK)
                                msr |= SER_RI;
@@ -1311,8 +1318,7 @@ uftdi_read_callback(struct usb_xfer *xfe
                if (ftdi_msr & FTDI_SIO_RLSD_MASK)
                        msr |= SER_DCD;
 
-               if ((sc->sc_msr != msr) ||
-                   ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
+               if (sc->sc_msr != msr || sc->sc_lsr != lsr) {
                        DPRINTF("status change msr=0x%02x (0x%02x) "
                            "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
                            lsr, sc->sc_lsr);

Modified: head/sys/dev/usb/serial/usb_serial.c
==============================================================================
--- head/sys/dev/usb/serial/usb_serial.c        Sun Feb  5 14:25:31 2017        
(r313286)
+++ head/sys/dev/usb/serial/usb_serial.c        Sun Feb  5 15:45:31 2017        
(r313287)
@@ -161,6 +161,7 @@ static tsw_param_t ucom_param;
 static tsw_outwakeup_t ucom_outwakeup;
 static tsw_inwakeup_t ucom_inwakeup;
 static tsw_free_t ucom_free;
+static tsw_busy_t ucom_busy;
 
 static struct ttydevsw ucom_class = {
        .tsw_flags = TF_INITLOCK | TF_CALLOUT,
@@ -172,6 +173,7 @@ static struct ttydevsw ucom_class = {
        .tsw_param = ucom_param,
        .tsw_modem = ucom_modem,
        .tsw_free = ucom_free,
+       .tsw_busy = ucom_busy,
 };
 
 MODULE_DEPEND(ucom, usb, 1, 1, 1);
@@ -1294,6 +1296,27 @@ ucom_outwakeup(struct tty *tp)
        ucom_start_transfers(sc);
 }
 
+static bool
+ucom_busy(struct tty *tp)
+{
+       struct ucom_softc *sc = tty_softc(tp);
+       const uint8_t txidle = ULSR_TXRDY | ULSR_TSRE;
+
+       UCOM_MTX_ASSERT(sc, MA_OWNED);
+
+       DPRINTFN(3, "sc = %p lsr 0x%02x\n", sc, sc->sc_lsr);
+
+       /*
+        * If the driver maintains the txidle bits in LSR, we can use them to
+        * determine whether the transmitter is busy or idle.  Otherwise we have
+        * to assume it is idle to avoid hanging forever on tcdrain(3).
+        */
+       if (sc->sc_flag & UCOM_FLAG_LSRTXIDLE)
+               return ((sc->sc_lsr & txidle) != txidle);
+       else
+               return (false);
+}
+
 /*------------------------------------------------------------------------*
  *     ucom_get_data
  *

Modified: head/sys/dev/usb/serial/usb_serial.h
==============================================================================
--- head/sys/dev/usb/serial/usb_serial.h        Sun Feb  5 14:25:31 2017        
(r313286)
+++ head/sys/dev/usb/serial/usb_serial.h        Sun Feb  5 15:45:31 2017        
(r313287)
@@ -180,6 +180,7 @@ struct ucom_softc {
 #define        UCOM_FLAG_WAIT_REFS   0x0100    /* set if we must wait for refs 
*/
 #define        UCOM_FLAG_FREE_UNIT   0x0200    /* set if we must free the unit 
*/
 #define        UCOM_FLAG_INWAKEUP    0x0400    /* set if we are in the 
tsw_inwakeup callback */
+#define        UCOM_FLAG_LSRTXIDLE   0x0800    /* set if sc_lsr bits 
ULSR_TSRE+TXRDY work */
        uint8_t sc_lsr;
        uint8_t sc_msr;
        uint8_t sc_mcr;
@@ -218,4 +219,12 @@ void       ucom_drain(struct ucom_super_softc 
 void   ucom_drain_all(void *);
 void   ucom_ref(struct ucom_super_softc *);
 int    ucom_unref(struct ucom_super_softc *);
+
+static inline void
+ucom_use_lsr_txbits(struct ucom_softc *sc)
+{
+
+       sc->sc_flag |= UCOM_FLAG_LSRTXIDLE;
+}
+
 #endif                                 /* _USB_SERIAL_H_ */
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "[email protected]"

Reply via email to