diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2018-08-31 12:55:38 -0700 |
---|---|---|
committer | Martin Roth <martinroth@chromium.org> | 2018-09-06 18:40:32 +0000 |
commit | 6ac45d447955d2870af63739ed7b9994038d390f (patch) | |
tree | 040ff927a8812b109d3f1042580d16ab99618320 | |
parent | 9173b1eb0cc11e71051521e34c397d3653950419 (diff) | |
download | chrome-ec-6ac45d447955d2870af63739ed7b9994038d390f.tar.gz |
driver: accel: Prevent ODR from crashing the EC
For accelerometers which are interrupt driven, setting ODR too high may
exhaust the EC and trigger the watchdog timer.
Use config variable to verify the ODR requested is not too big.
Return an error when setting unsupported parameters.
BUG=b:112672627
BRANCH=nocturne,eve
TEST=Without this change, ectool motionsense odr 0 500000 crashes the EC
Now, it returns EC result 3 (INVALID_PARAM)
Change-Id: I64a4e522dcad450d619a7fc48a1330479f1cf81f
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1200068
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
(cherry picked from commit f932679e653a4b62fbe1fb28fba8a20d7a7e38c2)
Reviewed-on: https://chromium-review.googlesource.com/1211363
Reviewed-by: Martin Roth <martinroth@chromium.org>
Commit-Queue: Martin Roth <martinroth@chromium.org>
Tested-by: Martin Roth <martinroth@chromium.org>
-rw-r--r-- | driver/accel_lis2dh.c | 12 | ||||
-rw-r--r-- | driver/accelgyro_bmi160.c | 37 | ||||
-rw-r--r-- | driver/accelgyro_lsm6dsm.c | 4 |
3 files changed, 21 insertions, 32 deletions
diff --git a/driver/accel_lis2dh.c b/driver/accel_lis2dh.c index a8e8827acd..df0b7be7de 100644 --- a/driver/accel_lis2dh.c +++ b/driver/accel_lis2dh.c @@ -96,14 +96,10 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd) normalized_rate = LIS2DH_REG_TO_NORMALIZE(reg_val); } - /* Adjust rounded value */ - if (reg_val > LIS2DH_ODR_400HZ_VAL) { - reg_val = LIS2DH_ODR_400HZ_VAL; - normalized_rate = LIS2DH_ODR_MAX_VAL; - } else if (reg_val < LIS2DH_ODR_1HZ_VAL) { - reg_val = LIS2DH_ODR_1HZ_VAL; - normalized_rate = LIS2DH_ODR_MIN_VAL; - } + if (normalized_rate > MIN(LIS2DH_ODR_MAX_VAL, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ) || + normalized_rate < LIS2DH_ODR_MIN_VAL) + return EC_RES_INVALID_PARAM; /* * Lock accel resource to prevent another task from attempting diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c index 2d03b06bd1..084304af54 100644 --- a/driver/accelgyro_bmi160.c +++ b/driver/accelgyro_bmi160.c @@ -389,41 +389,32 @@ static int set_data_rate(const struct motion_sensor_t *s, normalized_rate = BMI160_REG_TO_ODR(reg_val); if (rnd && (normalized_rate < rate)) { reg_val++; - normalized_rate *= 2; + normalized_rate = BMI160_REG_TO_ODR(reg_val); } switch (s->type) { case MOTIONSENSE_TYPE_ACCEL: - if (reg_val > BMI160_ODR_1600HZ) { - reg_val = BMI160_ODR_1600HZ; - normalized_rate = 1600000; - } else if (reg_val < BMI160_ODR_0_78HZ) { - reg_val = BMI160_ODR_0_78HZ; - normalized_rate = 780; - } + if (normalized_rate > MIN(BMI160_ACCEL_MAX_FREQ, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ) || + normalized_rate < BMI160_ACCEL_MIN_FREQ) + return EC_RES_INVALID_PARAM; break; case MOTIONSENSE_TYPE_GYRO: - if (reg_val > BMI160_ODR_3200HZ) { - reg_val = BMI160_ODR_3200HZ; - normalized_rate = 3200000; - } else if (reg_val < BMI160_ODR_25HZ) { - reg_val = BMI160_ODR_25HZ; - normalized_rate = 25000; - } + if (normalized_rate > MIN(BMI160_GYRO_MAX_FREQ, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ) || + normalized_rate < BMI160_GYRO_MIN_FREQ) + return EC_RES_INVALID_PARAM; break; +#ifdef CONFIG_MAG_BMI160_BMM150 case MOTIONSENSE_TYPE_MAG: /* We use the regular preset we can go about 100Hz */ - if (reg_val > BMI160_ODR_100HZ) { - reg_val = BMI160_ODR_100HZ; - normalized_rate = 100000; - } else if (reg_val < BMI160_ODR_0_78HZ) { - reg_val = BMI160_ODR_0_78HZ; - normalized_rate = 780; - } + if (reg_val > BMI160_ODR_100HZ || reg_val < BMI160_ODR_0_78HZ) + return EC_RES_INVALID_PARAM; break; +#endif default: - return -1; + return EC_RES_INVALID_PARAM; } /* diff --git a/driver/accelgyro_lsm6dsm.c b/driver/accelgyro_lsm6dsm.c index 29c21618a7..52eaec8586 100644 --- a/driver/accelgyro_lsm6dsm.c +++ b/driver/accelgyro_lsm6dsm.c @@ -446,7 +446,9 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd) reg_val++; normalized_rate *= 2; } - if (normalized_rate == 0) + if (normalized_rate < LSM6DSM_ODR_MIN_VAL || + normalized_rate > MIN(LSM6DSM_ODR_MAX_VAL, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ)) return EC_RES_INVALID_PARAM; } else { reg_val = 0; |