[PATCH] iio: imu: bno055: add explicit scan buf layout

David Lechner posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
drivers/iio/imu/bno055/bno055.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
[PATCH] iio: imu: bno055: add explicit scan buf layout
Posted by David Lechner 1 month, 2 weeks ago
Move the scan buf.chans array into a union along with a struct that
gives the layout of the buffer with all channels enabled.

Although not technically required in this case, if there had been a
different number of items before the quaternion, there could have been
a subtle bug with the special alignment needed for the quaternion
channel data and the array would have been too small.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
This depends on [1] that introduces the IIO_DECLARE_REPEATED_ELEMENT()
macro. I didn't include it in the same series because the other series
contains a fix and should be backported while this one ins't a fix.

[1]: https://lore.kernel.org/linux-iio/20260214-iio-fix-repeat-alignment-v1-1-47f01288c803@baylibre.com/
---
 drivers/iio/imu/bno055/bno055.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/imu/bno055/bno055.c b/drivers/iio/imu/bno055/bno055.c
index 303bc308f80a..47afda01229f 100644
--- a/drivers/iio/imu/bno055/bno055.c
+++ b/drivers/iio/imu/bno055/bno055.c
@@ -211,7 +211,25 @@ struct bno055_priv {
 	struct gpio_desc *reset_gpio;
 	bool sw_reset;
 	struct {
-		__le16 chans[BNO055_SCAN_CH_COUNT];
+		union {
+			__le16 chans[BNO055_SCAN_CH_COUNT];
+			/*
+			 * Struct is to ensure proper size in case any padding
+			 * is needed. Technically not needed in this case, but
+			 * better to be explicit about the requirements.
+			 */
+			struct {
+				__le16 acc[3];
+				__le16 magn[3];
+				__le16 gyr[3];
+				__le16 yaw;
+				__le16 pitch;
+				__le16 roll;
+				IIO_DECLARE_REPEATED_ELEMENT(__le16, quaternion, 4);
+				__le16 lia[3];
+				__le16 gravity[3];
+			};
+		};
 		aligned_s64 timestamp;
 	} buf;
 	struct dentry *debugfs;

---
base-commit: 0713b26190addfa3a774b386c8658952ef9f7faf
change-id: 20260214-iio-imu-bno055-repeated-element-1c1552ea74be
prerequisite-change-id: 20260214-iio-fix-repeat-alignment-575b2c009e25:v1
prerequisite-patch-id: f7d2b3c87791f50de610900f5680d1b9b503702a
prerequisite-patch-id: 28aed7da5bbcd16f83256bbe2d4c209fc26888bc

Best regards,
-- 
David Lechner <dlechner@baylibre.com>
Re: [PATCH] iio: imu: bno055: add explicit scan buf layout
Posted by Andy Shevchenko 1 month, 2 weeks ago
On Sat, Feb 14, 2026 at 04:10:38PM -0600, David Lechner wrote:
> Move the scan buf.chans array into a union along with a struct that
> gives the layout of the buffer with all channels enabled.
> 
> Although not technically required in this case, if there had been a
> different number of items before the quaternion, there could have been
> a subtle bug with the special alignment needed for the quaternion
> channel data and the array would have been too small.

...

> +			struct {
> +				__le16 acc[3];
> +				__le16 magn[3];
> +				__le16 gyr[3];
> +				__le16 yaw;
> +				__le16 pitch;
> +				__le16 roll;

> +				IIO_DECLARE_REPEATED_ELEMENT(__le16, quaternion, 4);

Right, and like I suggested:

				IIO_DECLARE_QUATERNION(__le16, quaternion);

> +				__le16 lia[3];
> +				__le16 gravity[3];
> +			};

-- 
With Best Regards,
Andy Shevchenko