bmi160: do not overrun the amount of data read from the BMI FIFO
bmi160: do not overrun the amount of data read from the BMI FIFO

This patch fixes a race condition in the BMI160 driver w.r.t. reading
from the FIFO.

The previous code was reading the FIFO length, adding 1 byte to it and
then reading that much data into a static buffer. This works well if no
new samples are added to the FIFO between reading the length and reading
the payload. But, if the BMI pushes a new sample in the FIFO, the read will
not return an empty frame header, but instead fill in the first byte of the
new sample, which is - however - incomplete and will be retransmitted.

However, because the buffer we process is static, if things align just
right, it is possible for that header to be parsed as a valid sample,
since we do not clear the buffer and we assume the entire 64 bytes of
it are valid and processable.

Fix this issue by maintaining a pointer to the end of the read-in FIFO
buffer and using that - instead of the static bp + sizeof - to calculate
how much data we can actually process from the FIFO.

BUG=b:120508077
BRANCH=poppy,octopus
TEST=Starting from CL:1367013 and CL:1370689, adding an
     `msleep(2)` between reading last_interrupt_timestamp
     and calling load_fifo(), observe that the kernel sees
     more sensor events than should be present given the
     sample rate selected. Observe that some of those have
     repeated payload values.

     With this change, the number of samples seen at the kernel
     side is compatible with the chosen sample rate and no samples
     have incorrectly repeating payloads. It is possible to also
     make this more obvious by setting the buffer to contain an
     incorrect value before reading from the FIFO, and observing
     that no samples contain that incorrect value.

Change-Id: I57124756d51b8acf04630020c1ffb934f471735f
Signed-off-by: Enrico Granata <f861e98f10aee98dc7dce70e80887ecdfdeb65b1@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1372027
Reviewed-by: Gwendal Grignou <190c5a5edfcf62e0246559bf31fa7548674de3ad@chromium.org>
Reviewed-by: Alexandru M Stan <1784cbba6d7675b1eba55eaefe41c7672ad9da3b@chromium.org>

coreboot/chrome-ec,coreboot/chrome-ec,coreboot/chrome-ec,coreboot/chrome-ec,coreboot/chrome-ec,coreboot/chrome-ec