svn commit: r314539 - stable/11/sys/dev/usb/serial
Ian Lepore
ian at FreeBSD.org
Thu Mar 2 04:42:35 UTC 2017
Author: ian
Date: Thu Mar 2 04:42:33 2017
New Revision: 314539
URL: https://svnweb.freebsd.org/changeset/base/314539
Log:
MFC r313287:
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().
Modified:
stable/11/sys/dev/usb/serial/uftdi.c
stable/11/sys/dev/usb/serial/usb_serial.c
stable/11/sys/dev/usb/serial/usb_serial.h
Directory Properties:
stable/11/ (props changed)
Modified: stable/11/sys/dev/usb/serial/uftdi.c
==============================================================================
--- stable/11/sys/dev/usb/serial/uftdi.c Thu Mar 2 04:23:53 2017 (r314538)
+++ stable/11/sys/dev/usb/serial/uftdi.c Thu Mar 2 04:42:33 2017 (r314539)
@@ -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: stable/11/sys/dev/usb/serial/usb_serial.c
==============================================================================
--- stable/11/sys/dev/usb/serial/usb_serial.c Thu Mar 2 04:23:53 2017 (r314538)
+++ stable/11/sys/dev/usb/serial/usb_serial.c Thu Mar 2 04:42:33 2017 (r314539)
@@ -162,6 +162,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,
@@ -173,6 +174,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);
@@ -1295,6 +1297,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: stable/11/sys/dev/usb/serial/usb_serial.h
==============================================================================
--- stable/11/sys/dev/usb/serial/usb_serial.h Thu Mar 2 04:23:53 2017 (r314538)
+++ stable/11/sys/dev/usb/serial/usb_serial.h Thu Mar 2 04:42:33 2017 (r314539)
@@ -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_ */
More information about the svn-src-stable
mailing list