mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] iio: hid-sensors: fix fractional part for unit exponents -7 and -8
@ 2026-11-04 16:53 Srinivas Pandruvada
  2026-11-05 19:26 ` Семён Марьясин
  0 siblings, 1 reply; 3+ messages in thread
From: Srinivas Pandruvada @ 2026-11-04 16:53 UTC (permalink / raw)
  To: jikos, jic23, bentiss
  Cc: linux-input, linux-iio, linux-kernel, simeon,
	Srinivas Pandruvada, stable

split_micro_fraction() scales the remainder to micro units by
multiplying it by 10^(6 - exp). The unit exponent of a HID field goes
down to -8, so exp can be 7 or 8 here. Then the argument of int_pow()
is negative, which the unsigned parameter turns into a huge power of
ten: 0 modulo 2^64. As a result, the fractional part of a value read
from the hysteresis attributes is always reported as 0.

Divide by 10^(exp - 6) in these two cases. This matches the reverse
conversion in convert_to_vtf_format().

Fixes: 73c6768b710a ("iio: hid-sensors: Common attribute and trigger")
Cc: stable@vger.kernel.org
Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
v2:
- Fix __udivdi3 link error on 32-bit by using a 32-bit divisor
- Address Andy's comment:
Keep no % divisor in a temporary variable next to no / divisor

 drivers/iio/common/hid-sensors/hid-sensor-attributes.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index 9e7c26e37880..d44091faf122 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -96,9 +96,14 @@ static void simple_div(int dividend, int divisor, int *whole, int *micro_frac)
 static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
 {
 	int divisor = int_pow(10, exp);
+	unsigned int rem;
 
 	*val1 = no / divisor;
-	*val2 = no % divisor * int_pow(10, 6 - exp);
+	rem = no % divisor;
+	if (exp > 6)
+		*val2 = rem / (unsigned int)int_pow(10, exp - 6);
+	else
+		*val2 = rem * int_pow(10, 6 - exp);
 }
 
 /*
-- 
2.56.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iio: hid-sensors: fix fractional part for unit exponents -7 and -8
  2026-11-04 16:53 [PATCH v2] iio: hid-sensors: fix fractional part for unit exponents -7 and -8 Srinivas Pandruvada
@ 2026-11-05 19:26 ` Семён Марьясин
  2026-11-05 23:56   ` Srinivas Pandruvada
  0 siblings, 1 reply; 3+ messages in thread
From: Семён Марьясин @ 2026-11-05 19:26 UTC (permalink / raw)
  To: Srinivas Pandruvada
  Cc: jikos, jic23, bentiss, linux-input, linux-iio, linux-kernel, stable

Tested-by: Semyon Maryasin <simeon@maryasin.name>

Here is what I observe with this patch:
1. Enable sensor and run xxd /dev/HID-SENSOR-2000e1.3.auto
2. In the second terminal, do rmmod intel_ishtp_hid
3. Observed result: xxd stops with "xxd: No such device"
4. At the same time, in kernel logs I see the following:

ноя 05 22:19:16 dook kernel: hid-sensor-hub 001F:8087:0AC2.0005:
timeout waiting for response from ISHTP device
ноя 05 22:19:19 dook kernel: hid-sensor-hub 001F:8087:0AC2.0005:
timeout waiting for response from ISHTP device
ноя 05 22:19:22 dook kernel: hid-sensor-hub 001F:8087:0AC2.0006:
timeout waiting for response from ISHTP device
ноя 05 22:19:25 dook kernel: hid-sensor-hub 001F:8087:0AC2.0006:
timeout waiting for response from ISHTP device

No system crash, so I suppose this is okay?

After subsequent modprobe the sensor initializes and works properly.
No kernel crash with this patch.

ср, 4 нояб. 2026 г. в 19:53, Srinivas Pandruvada
<srinivas.pandruvada@linux.intel.com>:
>
> split_micro_fraction() scales the remainder to micro units by
> multiplying it by 10^(6 - exp). The unit exponent of a HID field goes
> down to -8, so exp can be 7 or 8 here. Then the argument of int_pow()
> is negative, which the unsigned parameter turns into a huge power of
> ten: 0 modulo 2^64. As a result, the fractional part of a value read
> from the hysteresis attributes is always reported as 0.
>
> Divide by 10^(exp - 6) in these two cases. This matches the reverse
> conversion in convert_to_vtf_format().
>
> Fixes: 73c6768b710a ("iio: hid-sensors: Common attribute and trigger")
> Cc: stable@vger.kernel.org
> Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
> ---
> v2:
> - Fix __udivdi3 link error on 32-bit by using a 32-bit divisor
> - Address Andy's comment:
> Keep no % divisor in a temporary variable next to no / divisor
>
>  drivers/iio/common/hid-sensors/hid-sensor-attributes.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> index 9e7c26e37880..d44091faf122 100644
> --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> @@ -96,9 +96,14 @@ static void simple_div(int dividend, int divisor, int *whole, int *micro_frac)
>  static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
>  {
>         int divisor = int_pow(10, exp);
> +       unsigned int rem;
>
>         *val1 = no / divisor;
> -       *val2 = no % divisor * int_pow(10, 6 - exp);
> +       rem = no % divisor;
> +       if (exp > 6)
> +               *val2 = rem / (unsigned int)int_pow(10, exp - 6);
> +       else
> +               *val2 = rem * int_pow(10, 6 - exp);
>  }
>
>  /*
> --
> 2.56.0
>
>


-- 
С уважением,
Семён Марьясин

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iio: hid-sensors: fix fractional part for unit exponents -7 and -8
  2026-11-05 19:26 ` Семён Марьясин
@ 2026-11-05 23:56   ` Srinivas Pandruvada
  0 siblings, 0 replies; 3+ messages in thread
From: Srinivas Pandruvada @ 2026-11-05 23:56 UTC (permalink / raw)
  To: Семён
	Марьясин
  Cc: jikos, jic23, bentiss, linux-input, linux-iio, linux-kernel, stable

On Thu, 2026-11-05 at 22:26 +0300, Семён Марьясин wrote:
> Tested-by: Semyon Maryasin <simeon@maryasin.name>
> 
> Here is what I observe with this patch:
> 1. Enable sensor and run xxd /dev/HID-SENSOR-2000e1.3.auto
> 2. In the second terminal, do rmmod intel_ishtp_hid
> 3. Observed result: xxd stops with "xxd: No such device"
> 4. At the same time, in kernel logs I see the following:
> 
> ноя 05 22:19:16 dook kernel: hid-sensor-hub 001F:8087:0AC2.0005:
> timeout waiting for response from ISHTP device
> ноя 05 22:19:19 dook kernel: hid-sensor-hub 001F:8087:0AC2.0005:
> timeout waiting for response from ISHTP device
> ноя 05 22:19:22 dook kernel: hid-sensor-hub 001F:8087:0AC2.0006:
> timeout waiting for response from ISHTP device
> ноя 05 22:19:25 dook kernel: hid-sensor-hub 001F:8087:0AC2.0006:
> timeout waiting for response from ISHTP device
> 
> No system crash, so I suppose this is okay?
> 
That is fine.

Thanks,
Srinivas


> After subsequent modprobe the sensor initializes and works properly.
> No kernel crash with this patch.
> 
> ср, 4 нояб. 2026 г. в 19:53, Srinivas Pandruvada
> <srinivas.pandruvada@linux.intel.com>:
> > 
> > split_micro_fraction() scales the remainder to micro units by
> > multiplying it by 10^(6 - exp). The unit exponent of a HID field
> > goes
> > down to -8, so exp can be 7 or 8 here. Then the argument of
> > int_pow()
> > is negative, which the unsigned parameter turns into a huge power
> > of
> > ten: 0 modulo 2^64. As a result, the fractional part of a value
> > read
> > from the hysteresis attributes is always reported as 0.
> > 
> > Divide by 10^(exp - 6) in these two cases. This matches the reverse
> > conversion in convert_to_vtf_format().
> > 
> > Fixes: 73c6768b710a ("iio: hid-sensors: Common attribute and
> > trigger")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Srinivas Pandruvada
> > <srinivas.pandruvada@linux.intel.com>
> > ---
> > v2:
> > - Fix __udivdi3 link error on 32-bit by using a 32-bit divisor
> > - Address Andy's comment:
> > Keep no % divisor in a temporary variable next to no / divisor
> > 
> >  drivers/iio/common/hid-sensors/hid-sensor-attributes.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > index 9e7c26e37880..d44091faf122 100644
> > --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > @@ -96,9 +96,14 @@ static void simple_div(int dividend, int
> > divisor, int *whole, int *micro_frac)
> >  static void split_micro_fraction(unsigned int no, int exp, int
> > *val1, int *val2)
> >  {
> >         int divisor = int_pow(10, exp);
> > +       unsigned int rem;
> > 
> >         *val1 = no / divisor;
> > -       *val2 = no % divisor * int_pow(10, 6 - exp);
> > +       rem = no % divisor;
> > +       if (exp > 6)
> > +               *val2 = rem / (unsigned int)int_pow(10, exp - 6);
> > +       else
> > +               *val2 = rem * int_pow(10, 6 - exp);
> >  }
> > 
> >  /*
> > --
> > 2.56.0
> > 
> > 
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-05 23:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 16:53 [PATCH v2] iio: hid-sensors: fix fractional part for unit exponents -7 and -8 Srinivas Pandruvada
2026-11-05 19:26 ` Семён Марьясин
2026-11-05 23:56   ` Srinivas Pandruvada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®