Skip to content

Commit 0af5137

Browse files
dlechBrian Maly
authored andcommitted
hwmon: (tmp513) Fix division of negative numbers
[ Upstream commit e2c68ce ] Fix several issues with division of negative numbers in the tmp513 driver. The docs on the DIV_ROUND_CLOSEST macro explain that dividing a negative value by an unsigned type is undefined behavior. The driver was doing this in several places, i.e. data->shunt_uohms has type of u32. The actual "undefined" behavior is that it converts both values to unsigned before doing the division, for example: int ret = DIV_ROUND_CLOSEST(-100, 3U); results in ret == 1431655732 instead of -33. Furthermore the MILLI macro has a type of unsigned long. Multiplying a signed long by an unsigned long results in an unsigned long. So, we need to cast both MILLI and data data->shunt_uohms to long when using the DIV_ROUND_CLOSEST macro. Fixes: f07f9d2 ("hwmon: (tmp513) Use SI constants from units.h") Fixes: 59dfa75 ("hwmon: Add driver for Texas Instruments TMP512/513 sensor chips.") Signed-off-by: David Lechner <[email protected]> Link: https://lore.kernel.org/r/20250114-fix-si-prefix-macro-sign-bugs-v1-1-696fd8d10f00@baylibre.com [groeck: Drop some continuation lines] Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit 2f176c0ec9f52cb4b47330cb6958690de08c36ea) FOF: 0225 Signed-off-by: Brian Maly <[email protected]>
1 parent 6cb858e commit 0af5137

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/hwmon/tmp513.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
203203
*val = sign_extend32(regval,
204204
reg == TMP51X_SHUNT_CURRENT_RESULT ?
205205
16 - tmp51x_get_pga_shift(data) : 15);
206-
*val = DIV_ROUND_CLOSEST(*val * 10 * MILLI, data->shunt_uohms);
206+
*val = DIV_ROUND_CLOSEST(*val * 10 * (long)MILLI, (long)data->shunt_uohms);
207+
207208
break;
208209
case TMP51X_BUS_VOLTAGE_RESULT:
209210
case TMP51X_BUS_VOLTAGE_H_LIMIT:
@@ -219,7 +220,7 @@ static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
219220
case TMP51X_BUS_CURRENT_RESULT:
220221
// Current = (ShuntVoltage * CalibrationRegister) / 4096
221222
*val = sign_extend32(regval, 15) * (long)data->curr_lsb_ua;
222-
*val = DIV_ROUND_CLOSEST(*val, MILLI);
223+
*val = DIV_ROUND_CLOSEST(*val, (long)MILLI);
223224
break;
224225
case TMP51X_LOCAL_TEMP_RESULT:
225226
case TMP51X_REMOTE_TEMP_RESULT_1:
@@ -259,7 +260,7 @@ static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
259260
* The user enter current value and we convert it to
260261
* voltage. 1lsb = 10uV
261262
*/
262-
val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10 * MILLI);
263+
val = DIV_ROUND_CLOSEST(val * (long)data->shunt_uohms, 10 * (long)MILLI);
263264
max_val = U16_MAX >> tmp51x_get_pga_shift(data);
264265
regval = clamp_val(val, -max_val, max_val);
265266
break;

0 commit comments

Comments
 (0)