[PATCH v6 3/7] platform/chrome: cros_ec_sensorhub: Fix TS filter history shift index
From: Satyam Gupta
Date: Thu Oct 29 2026 - 05:33:10 EST
From: Scott Collyer <scollyer@xxxxxxxxxx>
cros_ec_sensor_ring_ts_filter_update() shifts the history arrays by one
entry before storing the new IRQ timestamp pair at index 0, and then
increments history_len. The shift loop starts at history_len - 1, so the
oldest valid entry is overwritten instead of moved, and the entry that
becomes valid when history_len is incremented holds stale data. For
example, after the second update, x_history[1] does not hold the first
IRQ timestamp.
Start the loop at history_len, bounded by
CROS_EC_SENSORHUB_TS_HISTORY_SIZE - 1 so that the oldest entry is
dropped once the arrays are full.
Fixes: 93fe48a58590 ("platform/chrome: cros_ec_sensorhub: Add median filter")
Signed-off-by: Scott Collyer <scollyer@xxxxxxxxxx>
Signed-off-by: Satyam Gupta <sgsatyam@xxxxxxxxxx>
---
drivers/platform/chrome/cros_ec_sensorhub_ring.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index 076a65a..c5331d9 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -277,7 +277,7 @@ cros_ec_sensor_ring_ts_filter_update(struct cros_ec_sensors_ts_filter_state
s64 m; /* stored as *M_PRECISION */
s64 *m_history_copy = state->temp_buf;
s64 *error = state->temp_buf;
- int i;
+ int i, start;
/* we trust b the most, that'll be our independent variable */
x = b;
@@ -303,7 +303,9 @@ cros_ec_sensor_ring_ts_filter_update(struct cros_ec_sensors_ts_filter_state
state->history_len = 0;
/* Move everything over, also update offset to all absolute coords .*/
- for (i = state->history_len - 1; i >= 1; i--) {
+ start = min_t(int, state->history_len,
+ CROS_EC_SENSORHUB_TS_HISTORY_SIZE - 1);
+ for (i = start; i >= 1; i--) {
state->x_history[i] = state->x_history[i - 1] + dx;
state->y_history[i] = state->y_history[i - 1] + dy;
--
2.56.0.307.g5fe7bd680c-goog