Devicetree
 help / color / mirror / Atom feed
From: me@herrie.org
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: Herman van Hazendonk <github.com@herrie.org>,
	jic23@kernel.org, linusw@kernel.org, denis.ciocca@st.com,
	robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	sanjayembeddedse@gmail.com, maudspierings@gocontroll.com,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/3] iio: common: st_sensors: honour channel endianness in read_axis_data
Date: Mon, 15 Jun 2026 14:41:14 +0200	[thread overview]
Message-ID: <58115ff8fad3f4762b7c626ce1eab1f5@herrie.org> (raw)
In-Reply-To: <aiMLuIlRPHXuj3cH@ashevche-desk.local>

On 2026-06-05 19:47, Andy Shevchenko wrote:
> On Fri, Jun 05, 2026 at 12:08:41PM +0200, Herman van Hazendonk wrote:
>> st_sensors_read_axis_data() unconditionally decoded multi-byte
>> results with get_unaligned_le16() / get_unaligned_le24() regardless
>> of the channel's declared scan_type.endianness.
>> 
>> For every ST sensor that has used this helper since it was introduced
>> this happened to be fine because the ST IMU/accel/gyro/pressure
>> families publish their data registers as little-endian and the
>> channel specs in those drivers declare IIO_LE accordingly.
>> 
>> The LSM303DLH magnetometer however publishes its X/Y/Z output as a
>> pair of big-endian bytes (the H register sits at the lower address,
>> 0x03/0x05/0x07, and the L register immediately after), and its
>> channel specs in st_magn_core.c correctly declare IIO_BE -- but
>> read_axis_data() ignored that and decoded as little-endian, swapping
>> the high and low bytes of every magnetometer sample.
>> 
>> The bug is most visible on a stationary chip: in earth's field the
>> true X reading is small and the high byte sits at 0x00, so swapping
>> the bytes pins sysfs X at exactly the low byte's pattern (e.g. 0x00F0
>> = 240). Y and Z still appear "to vary" because their magnitudes are
>> larger and the noise in the low byte produces big swings in the
>> swapped high byte:
>> 
>>   before (chip flat, sysfs in_magn_*_raw):
>>       X=240 (stuck), Y= 12032..23296, Z=-16128..-9728
>> 
>>   after (direct i2c-dev big-endian decode, same chip same 
>> orientation):
>>       X≈-4096, Y≈210, Z≈80     (sensible values reflecting earth's
>>                                 ambient field at low gauss range)
>> 
>> Fix read_axis_data() to dispatch on ch->scan_type.endianness and
>> call get_unaligned_be16() / get_unaligned_be24() when the channel
>> declares IIO_BE. Existing IIO_LE consumers (st_accel, st_gyro,
>> st_pressure, st_lsm6dsx and others) are unaffected because their
>> channel specs already declare IIO_LE and the LE path is unchanged.
> 
> ...
> 
>> -	if (byte_for_channel == 1)
>> +	if (byte_for_channel == 1) {
>>  		*data = (s8)*outdata;
> 
> Maybe for the consistency's sake use sign_extend32() everywhere?
> 
>> +	} else if (byte_for_channel == 2) {
>> +		if (ch->scan_type.endianness == IIO_BE)
>> +			*data = (s16)get_unaligned_be16(outdata);
>> +		else
>> +			*data = (s16)get_unaligned_le16(outdata);
>> +	} else if (byte_for_channel == 3) {
>> +		if (ch->scan_type.endianness == IIO_BE)
>> +			*data = (s32)sign_extend32(get_unaligned_be24(outdata),
>> +						   23);
>> +		else
>> +			*data = (s32)sign_extend32(get_unaligned_le24(outdata),
>> +						   23);
> 
> Why do you need casting here? sign_extend32() should return signed 
> type.
> With this being addressed, you can make them one-liners.
> 
>> +	}
> 
> 	u32 tmp;
> 	...
> 	if (byte_for_channel == 1) {
> 		// this way is done to show the below variant
> 		tmp = *outdata;
> 		*data = sign_extend32(tmp, 7);
> 	} else if (byte_for_channel == 2) {
> 		if (ch->scan_type.endianness == IIO_BE)
> 			tmp = get_unaligned_be16(outdata);
> 		else
> 			tmp = get_unaligned_le16(outdata);
> 		*data = sign_extend32(tmp, 15);
> 	} else if (byte_for_channel == 3) {
> 		if (ch->scan_type.endianness == IIO_BE)
> 			tmp = get_unaligned_be24(outdata);
> 		else
> 			tmp = get_unaligned_le24(outdata);
> 		*data = sign_extend32(tmp, 23);
> 	}
> 
> Or even
> 
> 	if (byte_for_channel == 1) {
> 		tmp = *outdata;
> 	} else if (byte_for_channel == 2) {
> 		if (ch->scan_type.endianness == IIO_BE)
> 			tmp = get_unaligned_be16(outdata);
> 		else
> 			tmp = get_unaligned_le16(outdata);
> 	} else if (byte_for_channel == 3) {
> 		if (ch->scan_type.endianness == IIO_BE)
> 			tmp = get_unaligned_be24(outdata);
> 		else
> 			tmp = get_unaligned_le24(outdata);
> 	} else {
> 		...error...
> 	}
> 	*data = sign_extend32(tmp, BYTES_TO_BITS(byte_for_channel) - 1);
Good points, both applied as you suggested in v2. Single u32 tmp 
captures
the per-width decode, then one sign_extend32(tmp, 
BYTES_TO_BITS(byte_for_channel)
  - 1) at the bottom handles all three widths. Casts are gone. I also 
turned
the previously-silent 0/≥4 fall-through into an explicit return 
-EINVAL so
a malformed channel spec is caught instead of leaving *data uninitialis

Thanks,
Herman

  reply	other threads:[~2026-06-15 12:49 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-05 10:08 [PATCH 0/3] iio: lsm303dlh-magn: endianness + boot-time fullscale selection Herman van Hazendonk
2026-06-05 10:08 ` [PATCH 1/3] iio: common: st_sensors: honour channel endianness in read_axis_data Herman van Hazendonk
2026-06-05 17:47   ` Andy Shevchenko
2026-06-15 12:41     ` me [this message]
2026-06-14 18:39   ` Jonathan Cameron
2026-06-15 11:12     ` me
2026-06-05 10:08 ` [PATCH 2/3] dt-bindings: iio: st,st-sensors: add st,fullscale-mg Herman van Hazendonk
2026-06-05 16:07   ` Conor Dooley
2026-06-07 22:54   ` Linus Walleij
2026-06-14 18:44   ` Jonathan Cameron
2026-06-15 11:16     ` me
2026-06-21 12:41       ` Jonathan Cameron
2026-06-05 10:08 ` [PATCH 3/3] iio: magnetometer: st_magn: honour st,fullscale-mg DT property Herman van Hazendonk
2026-06-05 17:52   ` Andy Shevchenko
2026-06-15 12:40     ` me

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=58115ff8fad3f4762b7c626ce1eab1f5@herrie.org \
    --to=me@herrie.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=denis.ciocca@st.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=github.com@herrie.org \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maudspierings@gocontroll.com \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=sanjayembeddedse@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox