Skip to content

Commit f1ee548

Browse files
Jakko3akpm00
authored andcommitted
iio: magnetometer: yas530: use signed integer type for clamp limits
In the function yas537_measure() there is a clamp_val() with limits of -BIT(13) and BIT(13) - 1. The input clamp value h[] is of type s32. The BIT() is of type unsigned long integer due to its define in include/vdso/bits.h. The lower limit -BIT(13) is recognized as -8192 but expressed as an unsigned long integer. The size of an unsigned long integer differs between 32-bit and 64-bit architectures. Converting this to type s32 may lead to undesired behavior. Additionally, in the calculation lines h[0], h[1] and h[2] the unsigned long integer divisor BIT(13) causes an unsigned division, shifting the left-hand side of the equation back and forth, possibly ending up in large positive values instead of negative values on 32-bit architectures. To solve those two issues, declare a signed integer with a value of BIT(13). There is another omission in the clamp line: clamp_val() returns a value and it's going nowhere here. Self-assign it to h[i] to make use of the clamp macro. Finally, replace clamp_val() macro by clamp() because after changing the limits from type unsigned long integer to signed integer it's fine that way. Link: https://lkml.kernel.org/r/11609b2243c295d65ab4d47e78c239d61ad6be75.1732914810.git.jahau@rocketmail.com Fixes: 65f79b5 ("iio: magnetometer: yas530: Add YAS537 variant") Signed-off-by: Jakob Hauser <[email protected]> Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Reviewed-by: David Laight <[email protected]> Acked-by: Jonathan Cameron <[email protected]> Cc: Lars-Peter Clausen <[email protected]> Cc: Linus Walleij <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 5f1b64e commit f1ee548

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

drivers/iio/magnetometer/yamaha-yas530.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y
372372
u8 data[8];
373373
u16 xy1y2[3];
374374
s32 h[3], s[3];
375+
int half_range = BIT(13);
375376
int i, ret;
376377

377378
mutex_lock(&yas5xx->lock);
@@ -406,13 +407,13 @@ static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y
406407
/* The second version of YAS537 needs to include calibration coefficients */
407408
if (yas5xx->version == YAS537_VERSION_1) {
408409
for (i = 0; i < 3; i++)
409-
s[i] = xy1y2[i] - BIT(13);
410-
h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / BIT(13);
411-
h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / BIT(13);
412-
h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / BIT(13);
410+
s[i] = xy1y2[i] - half_range;
411+
h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / half_range;
412+
h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / half_range;
413+
h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / half_range;
413414
for (i = 0; i < 3; i++) {
414-
clamp_val(h[i], -BIT(13), BIT(13) - 1);
415-
xy1y2[i] = h[i] + BIT(13);
415+
h[i] = clamp(h[i], -half_range, half_range - 1);
416+
xy1y2[i] = h[i] + half_range;
416417
}
417418
}
418419

0 commit comments

Comments
 (0)