| From 24e421bdfa4a557e6ccd3f58efb146f409aa7152 Mon Sep 17 00:00:00 2001 |
| From: Tony Lindgren <tony@atomide.com> |
| Date: Sun, 7 Apr 2019 11:12:48 -0700 |
| Subject: power: supply: cpcap-battery: Fix division by zero |
| |
| [ Upstream commit dbe7208c6c4aec083571f2ec742870a0d0edbea3 ] |
| |
| If called fast enough so samples do not increment, we can get |
| division by zero in kernel: |
| |
| __div0 |
| cpcap_battery_cc_raw_div |
| cpcap_battery_get_property |
| power_supply_get_property.part.1 |
| power_supply_get_property |
| power_supply_show_property |
| power_supply_uevent |
| |
| Fixes: 874b2adbed12 ("power: supply: cpcap-battery: Add a battery driver") |
| Signed-off-by: Tony Lindgren <tony@atomide.com> |
| Acked-by: Pavel Machek <pavel@ucw.cz> |
| Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com> |
| Signed-off-by: Sasha Levin <sashal@kernel.org> |
| --- |
| drivers/power/supply/cpcap-battery.c | 3 +++ |
| 1 file changed, 3 insertions(+) |
| |
| diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c |
| index 98ba07869c3b0..3bae02380bb22 100644 |
| --- a/drivers/power/supply/cpcap-battery.c |
| +++ b/drivers/power/supply/cpcap-battery.c |
| @@ -221,6 +221,9 @@ static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata, |
| int avg_current; |
| u32 cc_lsb; |
| |
| + if (!divider) |
| + return 0; |
| + |
| sample &= 0xffffff; /* 24-bits, unsigned */ |
| offset &= 0x7ff; /* 10-bits, signed */ |
| |
| -- |
| 2.20.1 |
| |