Skip to content

Commit 1ad8f7f

Browse files
Ivan Zaentsevgregkh
authored andcommitted
w1: w1_therm: Fix conversion result for negative temperatures
commit 2f6055c upstream. DS18B20 device driver returns an incorrect value for negative temperatures due to a missing sign-extension in w1_DS18B20_convert_temp(). Fix by using s16 temperature value when converting to int. Fixes: 9ace0b4 (w1: w1_therm: Add support for GXCAS GX20MH01 device.) Cc: stable <stable@vger.kernel.org> Reported-by: Paweł Marciniak <sunwire@gmail.com> Signed-off-by: Ivan Zaentsev <ivan.zaentsev@wirenboard.ru> Link: https://lore.kernel.org/r/20210121093021.224764-1-ivan.zaentsev@wirenboard.ru Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ed5d02f commit 1ad8f7f

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

‎drivers/w1/slaves/w1_therm.c‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -667,28 +667,24 @@ static inline int w1_DS18B20_get_resolution(struct w1_slave *sl)
667667
*/
668668
static inline int w1_DS18B20_convert_temp(u8 rom[9])
669669
{
670-
int t;
671-
u32 bv;
670+
u16 bv;
671+
s16 t;
672+
673+
/* Signed 16-bit value to unsigned, cpu order */
674+
bv = le16_to_cpup((__le16 *)rom);
672675

673676
/* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */
674677
if (rom[4] & 0x80) {
675-
/* Signed 16-bit value to unsigned, cpu order */
676-
bv = le16_to_cpup((__le16 *)rom);
677-
678678
/* Insert two temperature bits from config register */
679679
/* Avoid arithmetic shift of signed value */
680680
bv = (bv << 2) | (rom[4] & 3);
681-
682-
t = (int) sign_extend32(bv, 17); /* Degrees, lowest bit is 2^-6 */
683-
return (t*1000)/64; /* Millidegrees */
681+
t = (s16) bv; /* Degrees, lowest bit is 2^-6 */
682+
return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */
684683
}
685-
686-
t = (int)le16_to_cpup((__le16 *)rom);
687-
return t*1000/16;
684+
t = (s16)bv; /* Degrees, lowest bit is 2^-4 */
685+
return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */
688686
}
689687

690-
691-
692688
/**
693689
* w1_DS18S20_convert_temp() - temperature computation for DS18S20
694690
* @rom: data read from device RAM (8 data bytes + 1 CRC byte)

0 commit comments

Comments
 (0)