From 20d730b322fde1a5785b493a99a3b702cef2f793 Mon Sep 17 00:00:00 2001 From: Luigi Semenzato Date: Thu, 7 Jun 2012 10:30:53 -0700 Subject: [PATCH] EC: snow: keyboard: fix incorrect index calculation with empty fifo When kb_fifo_start is 0, the index calculation yields -1 because C got the mod (%) operation arguably wrong. By adding KB_FIFO_DEPTH to the index before computing the mod with respect to KB_FIFO_DEPTH, we avoid the negative case and (obviously) produce the same result in all other cases. BUG=chrome-os-partner:10247 TEST=saw incorrect state from kernel log before fix: could not repro after. Signed-off-by: Luigi Semenzato Change-Id: I3a30c229dc9f762dd45203e842128811a24cf53f Reviewed-on: https://gerrit.chromium.org/gerrit/24730 Tested-by: Luigi Semenzato Reviewed-by: Vincent Palatin Commit-Ready: Luigi Semenzato --- chip/stm32/keyboard_scan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chip/stm32/keyboard_scan.c b/chip/stm32/keyboard_scan.c index ea93f45cc0..40a0498476 100644 --- a/chip/stm32/keyboard_scan.c +++ b/chip/stm32/keyboard_scan.c @@ -138,8 +138,8 @@ static int kb_fifo_remove(uint8_t *buffp) { if (!kb_fifo_entries) { /* no entry remaining in FIFO : return last known state */ - memcpy(buffp, kb_fifo[(kb_fifo_start - 1) % KB_FIFO_DEPTH], - KB_OUTPUTS); + int last = (kb_fifo_start + KB_FIFO_DEPTH - 1) % KB_FIFO_DEPTH; + memcpy(buffp, kb_fifo[last], KB_OUTPUTS); /* * Bail out without changing any FIFO indices and let the