+ /* Clear the buffer early, to simplify the receive code path. */
+ memset(buf, 0, count + 1);
+
+ /*
+ * This calculation is aiming for backwards compatibility with
+ * an earlier implementation. An initial timeout is used which
+ * depends on the expected response byte count, and a maximum
+ * iteration count is used for read attempts.
+ *
+ * TODO Consider an absolute initial timeout instead, to reduce
+ * accumulated rounding errors for serial timeout results. The
+ * iteration with a short period is still required for variable
+ * length responses, because otherwise the serial communication
+ * layer would spend the total amount of time waiting for the
+ * remaining bytes, while the device probe code path by design
+ * passes a larger acceptable count than the typical and legal
+ * response would occupy.
+ *
+ * After initial receive data was seen, a shorter timeout is
+ * used which corresponds to a few bytes at wire speed. Idle
+ * periods without receive data longer than this threshold are
+ * taken as the end of the response. This is not compatible to
+ * the previous implementation, but was found to work as well.
+ * And severely reduces the time spent scanning for devices.
+ */
+ timeout_first = serial_timeout(serial, count);
+ retries_first = 100;
+ timeout_later = serial_timeout(serial, 3);
+ retries_later = 1;
+
+ sr_spew("want %zu bytes, timeout/retry: init %d/%zu, later %d/%zu.",
+ count, timeout_first, retries_first,
+ timeout_later, retries_later);
+
+ /*
+ * Run a sequence of read attempts. Try with the larger timeout
+ * and a high retry count until the first receive data became
+ * available. Then continue with a short timeout and small retry
+ * count.
+ *
+ * Failed read is fatal, immediately terminates the read sequence.
+ * A timeout in the initial phase just keeps repeating. A timeout
+ * after receive data was seen regularly terminates the sequence.
+ * Successful reads of non-empty responses keep extending the
+ * read sequence until no more receive data is available.
+ */