[PATCH RFC v4 3/6] iio: osf: add protocol decoding

From: Jinseob Kim

Date: Sun Jun 07 2026 - 19:46:15 EST


Use a FourCC-style wire magic comparison.

Document signed little-endian sample handling.

Signed-off-by: Jinseob Kim <kimjinseob88@xxxxxxxxx>
---
drivers/iio/opensensorfusion/osf_protocol.c | 4 +++-
drivers/iio/opensensorfusion/osf_protocol.h | 4 +++-
2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/opensensorfusion/osf_protocol.c b/drivers/iio/opensensorfusion/osf_protocol.c
index ed91d3dd5..5bee545f3 100644
--- a/drivers/iio/opensensorfusion/osf_protocol.c
+++ b/drivers/iio/opensensorfusion/osf_protocol.c
@@ -11,6 +11,7 @@

#define OSF_CRC32_INIT GENMASK(31, 0)
#define OSF_CRC32_XOROUT GENMASK(31, 0)
+#define OSF_FRAME_MAGIC 0x3046534f /* "OSF0" little-endian */

static bool osf_sensor_type_valid(u16 sensor_type)
{
@@ -38,7 +39,7 @@ int osf_protocol_decode_frame(const u8 *buf, size_t len,
if (len < OSF_FRAME_MIN_LEN)
return -EMSGSIZE;

- if (buf[0] != 'O' || buf[1] != 'S' || buf[2] != 'F' || buf[3] != '0')
+ if (get_unaligned_le32(buf) != OSF_FRAME_MAGIC)
return -EPROTO;

major = buf[4];
@@ -136,6 +137,7 @@ int osf_protocol_sensor_sample_value(const struct osf_sensor_sample *sample,
if (index >= sample->channel_count)
return -ERANGE;

+ /* Samples are little-endian two's-complement signed values. */
*value = (s32)get_unaligned_le32(sample->samples + index * sizeof(s32));

return 0;
diff --git a/drivers/iio/opensensorfusion/osf_protocol.h b/drivers/iio/opensensorfusion/osf_protocol.h
index 4b6fb131a..c62c2c254 100644
--- a/drivers/iio/opensensorfusion/osf_protocol.h
+++ b/drivers/iio/opensensorfusion/osf_protocol.h
@@ -2,6 +2,7 @@
#ifndef _OSF_PROTOCOL_H
#define _OSF_PROTOCOL_H

+#include <linux/bits.h>
#include <linux/types.h>

#define OSF_PROTOCOL_MAJOR 0
@@ -14,7 +15,7 @@
#define OSF_DEVICE_STATUS_LEN 20
#define OSF_CAP_REPORT_BASE_LEN 4
#define OSF_CAP_SENSOR_ENTRY_LEN 20
-#define OSF_CAPABILITY_FLAGS_MASK 0x00000003U
+#define OSF_CAPABILITY_FLAGS_MASK GENMASK(1, 0)

enum osf_message_type {
OSF_MSG_SENSOR_SAMPLE = 0x0001,
@@ -44,6 +45,7 @@ struct osf_frame {
u64 sequence;
u64 timestamp_us;
u32 flags;
+ /* payload points into the caller-owned frame buffer. */
const u8 *payload;
u32 crc;
};
--
2.43.0