ftdiuart: Decrease latency timer to improve responsiveness

POSIX PTY slaves usually expect a read() to compelete instantly and
return any data that had been buffered as long as there is at least one
byte pending. Unfortunately, the FTDI chip has a different idea of
reading: it only completes a USB Bulk IN when it can either serve the
whole request at once or after a "latency timer" of by default 16ms has
passed. This doesn't really play nice with bidirectional protocols like
remote GDB which send a lot of little chunks back and forth while always
waiting for an acknowledgement.

This patch decreases the latency timer for UARTs to the minimum of 1ms,
which is well within the range of other limiting delays in the whole
Servo serial stack. Note that this will increase the Servo USB traffic
when idle by a factor of 16, which shouldn't be an issue (and if it ever
turns out to be one we should find smarter ways to ratelimit it).

BUG=chrome-os-partner:18390
TEST=Saw efficiency of my custom ping-pong test code grow by a factor of
~15, and putchar-based printfs over remote GDB feel a bit faster as well.

Change-Id: Ife85b7ca60a04e8f5fece87792ecf15884aac86a
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/195312
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
diff --git a/lib/ftdiuart.c b/lib/ftdiuart.c
index 05b52e2..96e5143 100644
--- a/lib/ftdiuart.c
+++ b/lib/ftdiuart.c
@@ -177,6 +177,12 @@
   if (fuart_stty_locked(fuartc, &fargs->uart_cfg))
     return FUART_ERR_OPEN;
 
+  // Minimize read latency to improve bidirectional protocol responsiveness
+  if (ftdi_set_latency_timer(fc, 1)) {
+    ERROR_FTDI("latency timer", fc);
+    return FUART_ERR_OPEN;
+  }
+
   if ((fd = posix_openpt(O_RDWR | O_NOCTTY)) == -1) {
     perror("opening pty master");
     return FUART_ERR_OPEN;