[PATCH 1/2] input: ad714x - fix divide by 0 error in wheel cal_abs_pos

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



V2: added comment, removed "BUG_ON" check and fixed >80 col line

As discussed with Michael Hennerich <michael.hennerich@xxxxxxxxxx>,
another divide by 0 happens when the user is lifting the finger
when all the sensors are back below ambient but we are in case
"ACTIVE".

Signed-off-by: Jean-François Dagenais <dagenaisj@xxxxxxxxxxxx>
Signed-off-by: Jean-François Dagenais <jeff.dagenais@xxxxxxxxx>
---
 drivers/input/misc/ad714x.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/input/misc/ad714x.c b/drivers/input/misc/ad714x.c
index ca42c7d..2cf26e5 100644
--- a/drivers/input/misc/ad714x.c
+++ b/drivers/input/misc/ad714x.c
@@ -418,10 +418,15 @@ static void ad714x_wheel_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
 		sw->highest_stage);
 }
 
-static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
+/**
+ * Calculates the sensor value for each stages of the given wheel idx.
+ * Returns true if a sensor detects being touched, false otherwise.
+ */
+static bool ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
 {
 	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
 	int i;
+	bool touched = false;
 
 	ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,
 			&ad714x->adc_reg[hw->start_stage],
@@ -430,12 +435,15 @@ static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
 	for (i = hw->start_stage; i <= hw->end_stage; i++) {
 		ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
 				&ad714x->amb_reg[i], 1);
-		if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
+		if (ad714x->adc_reg[i] > ad714x->amb_reg[i]) {
 			ad714x->sensor_val[i] =
 				ad714x->adc_reg[i] - ad714x->amb_reg[i];
+			touched = true;
+		}
 		else
 			ad714x->sensor_val[i] = 0;
 	}
+	return touched;
 }
 
 /*
@@ -544,8 +552,8 @@ static void ad714x_wheel_state_machine(struct ad714x_chip *ad714x, int idx)
 
 	case ACTIVE:
 		if (c_state == mask) {
-			if (h_state) {
-				ad714x_wheel_cal_sensor_val(ad714x, idx);
+			if (h_state &&
+			    ad714x_wheel_cal_sensor_val(ad714x, idx)) {
 				ad714x_wheel_cal_highest_stage(ad714x, idx);
 				ad714x_wheel_cal_abs_pos(ad714x, idx);
 				ad714x_wheel_cal_flt_pos(ad714x, idx);
-- 
1.7.9

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Media Devel]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Linux Wireless Networking]     [Linux Omap]

  Powered by Linux