Skip to content

Commit 812d6cf

Browse files
committed
Accelerometer: Check CrosEcReadMemU8 return values
CrosEcReadMemU8 returns 0 on IOCTL failure but we never check this. If the memmap read fails, the buffers stay uninitialized/unchanged, which makes the busy bit logic fail and the sensor report the wrong data. Add an EC_READ_U8 helper macro to checks for failure and jump to Exit with STATUS_IO_DEVICE_ERROR, so that broken EC communication is properly reported instead of producing invalid readings. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent fc95b8f commit 812d6cf

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

FrameworkSensors/AccelerometerClient.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,15 @@ AccelerometerDevice::GetData(
539539
#define BUSY_WAIT_SLEEP_INTERVAL 5
540540
#define BUSY_WAIT_SLEEP_MS 25
541541

542+
// Helper: read a byte from EC memory map, goto Exit on failure.
543+
#define EC_READ_U8(offset, dest) do { \
544+
if (CrosEcReadMemU8(Handle, (offset), (dest)) == 0) { \
545+
TraceError("%!FUNC! CrosEcReadMemU8 failed at offset 0x%02x", (offset)); \
546+
Status = STATUS_IO_DEVICE_ERROR; \
547+
goto Exit; \
548+
} \
549+
} while (0)
550+
542551
UINT8 lid_angle_bytes[2] = {0};
543552
UINT16 lid_angle = 0;
544553
UINT SensorOffset = 6 * m_LidSensorIndex + EC_MEMMAP_ACC_DATA + 2;
@@ -557,7 +566,7 @@ AccelerometerDevice::GetData(
557566
// Poll ACC_STATUS until the EC is not busy
558567
//
559568
int busy_attempts = 0;
560-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
569+
EC_READ_U8(EC_MEMMAP_ACC_STATUS, &acc_status);
561570

562571
while (acc_status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
563572
if (busy_attempts++ >= MAX_BUSY_WAIT_ATTEMPTS) {
@@ -569,7 +578,7 @@ AccelerometerDevice::GetData(
569578
if (busy_attempts % BUSY_WAIT_SLEEP_INTERVAL == 0)
570579
Sleep(BUSY_WAIT_SLEEP_MS);
571580

572-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
581+
EC_READ_U8(EC_MEMMAP_ACC_STATUS, &acc_status);
573582
}
574583

575584
TraceInformation("Status: (%02x), Present: %d, Busy: %d\n",
@@ -585,22 +594,24 @@ AccelerometerDevice::GetData(
585594
//
586595
// Read all sensor data (unsafe - EC could update mid-read)
587596
//
588-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 0, &lid_angle_bytes[0]);
589-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 1, &lid_angle_bytes[1]);
597+
EC_READ_U8(EC_MEMMAP_ACC_DATA + 0, &lid_angle_bytes[0]);
598+
EC_READ_U8(EC_MEMMAP_ACC_DATA + 1, &lid_angle_bytes[1]);
590599

591-
CrosEcReadMemU8(Handle, SensorOffset + 0, &Sensor1[0]);
592-
CrosEcReadMemU8(Handle, SensorOffset + 1, &Sensor1[1]);
593-
CrosEcReadMemU8(Handle, SensorOffset + 2, &Sensor1[2]);
594-
CrosEcReadMemU8(Handle, SensorOffset + 3, &Sensor1[3]);
595-
CrosEcReadMemU8(Handle, SensorOffset + 4, &Sensor1[4]);
596-
CrosEcReadMemU8(Handle, SensorOffset + 5, &Sensor1[5]);
600+
EC_READ_U8(SensorOffset + 0, &Sensor1[0]);
601+
EC_READ_U8(SensorOffset + 1, &Sensor1[1]);
602+
EC_READ_U8(SensorOffset + 2, &Sensor1[2]);
603+
EC_READ_U8(SensorOffset + 3, &Sensor1[3]);
604+
EC_READ_U8(SensorOffset + 4, &Sensor1[4]);
605+
EC_READ_U8(SensorOffset + 5, &Sensor1[5]);
597606

598607
//
599608
// Re-read ACC_STATUS to verify data consistency
600609
//
601-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &status);
610+
EC_READ_U8(EC_MEMMAP_ACC_STATUS, &status);
602611
}
603612

613+
#undef EC_READ_U8
614+
604615
//
605616
// Data is now consistent - process it
606617
//

0 commit comments

Comments
 (0)