Skip to contentSkip to conversation

iio: add Open Sensor Fusion IIO driver

PATCH Superseded Jinseob Kim 8 participants 18 messages Jul 7 – Jul 12

Subsystems (3)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v7 cover iio: add Open Sensor Fusion IIO driver

This series adds a Linux IIO driver for Open Sensor Fusion devices exposed over a UART/serdev host interface. The driver parses OSF frames and creates IIO devices at runtime from capability reports. Expected IIO devices are accel, gyro, magnetometer, and temperature when those capabilities are reported.

The binding, supported protocol subset, runtime discovery model, and driver-facing ABI remain review-relevant, but this revision drops the RFC tag because there are no specific open questions being asked in the cover letter.

v7 was prepared after auditing earlier review comments and either addressing them in code/docs or answering them on-list.

Previous versions:

v1:
https://lore.kernel.org/r/[email protected]

v2:
https://lore.kernel.org/r/[email protected]

v3:
https://lore.kernel.org/r/[email protected]

v4:
https://lore.kernel.org/r/[email protected]

v5:
https://lore.kernel.org/r/[email protected]

v6:
https://lore.kernel.org/r/[email protected]

Changes in v7:
- Dropped the RFC tag; no specific open questions remain in the cover letter.
- Added previous-version lore links.
- Removed the confusing compatible/OSF GREEN/OSF0 explanatory sentence from the binding.
- Removed "initial" wording from the IIO documentation.
- Reused the common OSF_FRAME_MAGIC definition in the stream parser.
- Fixed OSF_FRAME_MAGIC comment wording.
- Removed dead overflow checks for u16 protocol counts.
- Removed the unused partial_frames statistic.
- Fixed remaining loop-local variable issues.
- Reworked the stream/core contract so CRC-valid/authenticated frames are consumed as full frames even when ignored or rejected at application level.
- Preserved one-byte resync only for unauthenticated/framing failures.
- Preserved IIO buffer synchronization and zeroed scan storage.
- Removed the duplicate IIO dependency from the OSF Kconfig entry.

Validation:
- git diff --check: pass.
- checkpatch --strict: pass, 0 errors, 0 warnings, 0 checks.
- dt_binding_check: pass with dtschema 2026.4.
- GCC W=1 module build: pass, no OSF compiler warnings.
- clang W=1 module build with LLVM=1 external build: pass, no OSF compiler warnings.
- local stream parser semantic tests: pass, including CRC-valid bad protocol_major and nonzero reserved frames with embedded OSF frames.
- static IIO scan layout/padding checks: pass.

Jinseob Kim (5):
dt-bindings: iio: add Open Sensor Fusion device
Documentation: iio: add Open Sensor Fusion driver overview
iio: osf: add protocol decoding
iio: osf: add authenticated stream parser
iio: osf: add UART IIO driver

 .../bindings/iio/opensensorfusion,osf.yaml    |  52 +++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 Documentation/iio/index.rst                   |   1 +
 Documentation/iio/open-sensor-fusion.rst      |  72 ++++
 MAINTAINERS                                   |  14 +
 drivers/iio/Kconfig                           |   1 +
 drivers/iio/Makefile                          |   1 +
 drivers/iio/opensensorfusion/Kconfig          |  15 +
 drivers/iio/opensensorfusion/Makefile         |   6 +
 drivers/iio/opensensorfusion/osf_core.c       | 320 ++++++++++++++++++
 drivers/iio/opensensorfusion/osf_core.h       |  70 ++++
 drivers/iio/opensensorfusion/osf_iio.c        | 308 +++++++++++++++++
 drivers/iio/opensensorfusion/osf_iio.h        |  22 ++
 drivers/iio/opensensorfusion/osf_protocol.c   | 242 +++++++++++++
 drivers/iio/opensensorfusion/osf_protocol.h   | 100 ++++++
 drivers/iio/opensensorfusion/osf_serdev.c     | 114 +++++++
 drivers/iio/opensensorfusion/osf_stream.c     | 189 +++++++++++
 drivers/iio/opensensorfusion/osf_stream.h     |  30 ++
 18 files changed, 1559 insertions(+)

base-commit: ab5fce87a778cb780a05984a2ca448f2b41aafbf

Signature · 1 line
2.43.0
#1 lore raw

PATCH v7 1/5 dt-bindings: iio: add Open Sensor Fusion device

Add a binding for the generic Open Sensor Fusion host interface.

Open Sensor Fusion devices report capabilities and samples over an OSF protocol stream. Sensor channels are discovered at runtime from capability reports instead of being described individually in Device Tree.

The protocol version is discovered at runtime from the OSF frame header. OSF GREEN is a product identity, and OSF0 is a wire-format magic value, so neither is used as the Linux compatible string.

---
 .../bindings/iio/opensensorfusion,osf.yaml    | 52 +++++++++++++++++++
 .../devicetree/bindings/vendor-prefixes.yaml  |  2 +
 MAINTAINERS                                   |  6 +++
 3 files changed, 60 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +52 -0
new file mode 100644
index 000000000..399839082
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/opensensorfusion,osf.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Open Sensor Fusion Sensor Aggregation Hub
+
+maintainers:
+  - Jinseob Kim <[email protected]>
+
+description: |
+  This binding documents the generic Open Sensor Fusion host interface. Open
+  Sensor Fusion is a sensor aggregation hub. The hub exposes an OSF protocol
+  data stream over its host interface and reports capabilities and samples for
+  multiple sensor classes. The actual sensor channels are discovered at runtime
+  from OSF capability reports instead of describing them in Device Tree. The
+  protocol version is discovered at runtime.
+
+  Public project documentation is available at:
+
+    https://github.com/opensensorfusion
+
+  OSF0, protocol_major, and protocol_minor are wire-protocol details
+  exchanged in OSF frames.
+
+allOf:
+  - $ref: /schemas/serial/serial-peripheral-props.yaml#
+
+properties:
+  compatible:
+    const: opensensorfusion,osf
+
+  vcc-supply:
+    description:
+      Regulator supplying power to the Open Sensor Fusion device.
+
+required:
+  - compatible
+  - vcc-supply
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    serial {
+        sensor {
+            compatible = "opensensorfusion,osf";
+            vcc-supply = <&vcc_sensor>;
+        };
+    };
+...
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 28784d66a..88172d4a4 100644
@@ -1237,6 +1237,8 @@ patternProperties:
     description: OpenPandora GmbH
   "^openrisc,.*":
     description: OpenRISC.io
+  "^opensensorfusion,.*":
+    description: Open Sensor Fusion
   "^openwrt,.*":
     description: OpenWrt
   "^option,.*":
MAINTAINERS +6 -0
index c2c6d7927..e4df9d8dc 100644
@@ -20011,6 +20011,12 @@ F:	Documentation/devicetree/
 F:	arch/*/boot/dts/
 F:	include/dt-bindings/
 
+OPEN SENSOR FUSION
+M:	Jinseob Kim <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
+K:	opensensorfusion
+
 OPENCOMPUTE PTP CLOCK DRIVER
 M:	Vadim Fedorenko <[email protected]>
 L:	[email protected]
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v7 1/5] dt-bindings: iio: add Open Sensor Fusion device

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The Device Tree binding intentionally omits the specific hardware product identity, restricting…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The Device Tree binding intentionally omits the specific hardware product identity, restricting the compatible property to a generic protocol-level string. --

--- Patch [1]: [PATCH v7 1/5] dt-bindings: iio: add Open Sensor Fusion device --- commit 427a1ffc9a3a61dc0d7c28ad4dfc48f800f94f69
Author: Jinseob Kim <[email protected]>

dt-bindings: iio: add Open Sensor Fusion device

This commit introduces a binding for the generic Open Sensor Fusion host interface. It specifies that sensor channels and protocol versions are discovered at runtime, and sets the compatible string to a generic protocol identifier.

Quoted 5 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +0 -0
new file mode 100644
index 0000000000000..3998390828cc0

[ ... ]

Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 32

Quoted 6 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +6 -0
+allOf:
+  - $ref: /schemas/serial/serial-peripheral-props.yaml#
+
+properties:
+  compatible:
+    const: opensensorfusion,osf

[Severity: Medium]
Will omitting the specific hardware identity prevent the kernel from applying hardware-specific quirks in the future?

The commit message notes that OSF GREEN is a product identity and intentionally omitted from the compatible string. However, device trees are expected to describe specific hardware, not just the generic protocol it speaks.

If the specific hardware requires workarounds in the future, such as power sequencing adjustments, delays before the protocol responds, or errata handling, the kernel will have no way to apply them because the device tree only identifies it as a generic device.

Could the binding be updated to allow specific hardware compatible strings alongside the generic fallback, rather than restricting it to a single const value?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v7 1/5
#3 lore raw

Re: [PATCH v7 1/5] dt-bindings: iio: add Open Sensor Fusion device

In reply to Jinseob Kim
Quoted 7 lines · Jinseob Kim

Add a binding for the generic Open Sensor Fusion host interface.

Open Sensor Fusion devices report capabilities and samples over an OSF protocol stream. Sensor channels are discovered at runtime from capability reports instead of being described individually in Device Tree.

Quoted 5 lines

The protocol version is discovered at runtime from the OSF frame header. OSF GREEN is a product identity, and OSF0 is a wire-format magic value, so neither is used as the Linux compatible string.

pw-bot: not-applicable

#4 lore raw

PATCH v7 2/5 Documentation: iio: add Open Sensor Fusion driver overview

In reply to Jinseob Kim

Document the Linux IIO mapping for Open Sensor Fusion devices.

The overview explains that sensor channels are discovered at runtime from mandatory capability reports. It also documents that OSF0 is a wire-format detail and that protocol_major and protocol_minor carry protocol compatibility information.

---
 Documentation/iio/index.rst              |  1 +
 Documentation/iio/open-sensor-fusion.rst | 72 ++++++++++++++++++++++++
 MAINTAINERS                              |  1 +
 3 files changed, 74 insertions(+)
 create mode 100644 Documentation/iio/open-sensor-fusion.rst
Documentation/iio/index.rst +1 -0
index ba3e609c6..2713ec5e0 100644
@@ -38,4 +38,5 @@ Industrial I/O Kernel Drivers
    adxl345
    bno055
    ep93xx_adc
+   open-sensor-fusion
    opt4060
Documentation/iio/open-sensor-fusion.rst new file +72 -0

72 lines not on this page · raw message

MAINTAINERS +1 -0
index e4df9d8dc..17c80b55b 100644
@@ -20015,6 +20015,7 @@ OPEN SENSOR FUSION
 M:	Jinseob Kim <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
+F:	Documentation/iio/open-sensor-fusion.rst
 K:	opensensorfusion
 
 OPENCOMPUTE PTP CLOCK DRIVER
Signature · 1 line
2.43.0
reviewer Tested-by on v7 2/5
#5 lore raw

Re: [PATCH v7 2/5] Documentation: iio: add Open Sensor Fusion driver overview

Quoted 8 lines · Jinseob Kim

Document the Linux IIO mapping for Open Sensor Fusion devices.

The overview explains that sensor channels are discovered at runtime from mandatory capability reports. It also documents that OSF0 is a wire-format detail and that protocol_major and protocol_minor carry protocol compatibility information.

Thanks.

Quoted 107 lines
---
 Documentation/iio/index.rst              |  1 +
 Documentation/iio/open-sensor-fusion.rst | 72 ++++++++++++++++++++++++
 MAINTAINERS                              |  1 +
 3 files changed, 74 insertions(+)
 create mode 100644 Documentation/iio/open-sensor-fusion.rst
Documentation/iio/index.rst +1 -0
index ba3e609c6..2713ec5e0 100644
@@ -38,4 +38,5 @@ Industrial I/O Kernel Drivers
    adxl345
    bno055
    ep93xx_adc
+   open-sensor-fusion
    opt4060
Documentation/iio/open-sensor-fusion.rst new file +72 -0
new file mode 100644
index 000000000..c28886f3b
@@ -0,0 +1,72 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Open Sensor Fusion
+==================
+
+Open Sensor Fusion is a sensor aggregation hub interface. The Linux IIO driver
+receives OSF protocol frames from an attached device and registers matching IIO
+devices for the sensor classes supported by the driver. The actual sensor
+channels are discovered at runtime from mandatory OSF capability reports.
+
+This document is a driver-facing overview for the Linux IIO mapping. The full
+wire protocol, firmware behavior, and hardware model details belong in the Open
+Sensor Fusion project documentation.
+
+Device Model
+------------
+
+An OSF device sends binary frames from the device to the host. Devices using the
+``opensensorfusion,osf`` compatible are expected to provide
+``CAPABILITY_REPORT`` messages so the host can discover which sensor streams are
+available. Device Tree describes the attached OSF sensor aggregation hub; it does
+not enumerate the individual sensors discovered at runtime.
+
+The currently supported Linux subset exposes:
+
+* accelerometer samples as ``IIO_ACCEL`` X/Y/Z channels,
+* gyroscope samples as ``IIO_ANGL_VEL`` X/Y/Z channels,
+* magnetometer samples as ``IIO_MAGN`` X/Y/Z channels, and
+* temperature samples as ``IIO_TEMP``.
+
+Protocol Scope
+---------------
+
+The driver supports OSF protocol major version 0 for the IIO receive path.
+The current wire magic is ``OSF0``; that string is a wire-format detail and is
+not the Linux driver identity. Device Tree keeps the generic
+``opensensorfusion,osf`` compatible rather than naming a product such as OSF
+GREEN or a wire magic value.
+
+Protocol versioning is carried by the ``protocol_major`` and ``protocol_minor``
+fields at fixed offsets in the OSF frame header. The driver currently
+supports ``protocol_major`` 0. ``protocol_minor`` changes within major version
+0 are intended to remain backward-compatible within the fixed header layout.
+Incompatible wire-format changes require a new ``protocol_major``. A future
+device that cannot expose compatible version discovery through that fixed
+header layout would need a different Device Tree compatible.
+
+The Linux driver handles device-to-host frames for:
+
+* ``SENSOR_SAMPLE`` buffered and direct-mode sample data,
+* ``CAPABILITY_REPORT`` based IIO device registration, and
+* ``DEVICE_STATUS`` cache updates.
+
+Vendor-private message types are ignored. Command transport, calibration
+control ABI, fusion output ABI, and runtime capability removal are outside the
+Linux IIO receive path.
+
+Timestamps
+----------
+
+OSF frames include a device-side ``timestamp_us`` field. Buffered IIO samples use
+an IIO timestamp captured on the host when samples are pushed to IIO buffers.
+The driver does not correlate the device timestamp with the host IIO
+clock.
+
+Compatibility Notes
+-------------------
+
+The project protocol documentation should define the compatibility rules for
+reserved fields, optional flags, and trailing extension data. Until those rules
+are finalized, the Linux decoder keeps conservative bounds checks around the
+currently supported message layouts.
MAINTAINERS +1 -0
index e4df9d8dc..17c80b55b 100644
@@ -20015,6 +20015,7 @@ OPEN SENSOR FUSION
 M:	Jinseob Kim <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
+F:	Documentation/iio/open-sensor-fusion.rst
 K:	opensensorfusion
 
 OPENCOMPUTE PTP CLOCK DRIVER
Signature · 1 line
~Randy
#6 lore raw

PATCH v7 3/5 iio: osf: add protocol decoding

In reply to Jinseob Kim

Add helpers for decoding Open Sensor Fusion frame headers and supported message payloads.

The decoder validates the OSF0 wire magic, protocol major version, header length, payload bounds, reserved fields and CRC before exposing decoded frame contents to the rest of the driver.

Use explicit little-endian wire storage sizes and designated initializers for decoded output structures.

---
 MAINTAINERS                                 |   1 +
 drivers/iio/opensensorfusion/osf_protocol.c | 242 ++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_protocol.h | 100 ++++++++
 3 files changed, 343 insertions(+)
 create mode 100644 drivers/iio/opensensorfusion/osf_protocol.c
 create mode 100644 drivers/iio/opensensorfusion/osf_protocol.h
MAINTAINERS +1 -0
index 17c80b55b..32d3e7674 100644
@@ -20016,6 +20016,7 @@ M:	Jinseob Kim <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
 F:	Documentation/iio/open-sensor-fusion.rst
+F:	drivers/iio/opensensorfusion/osf_protocol.*
 K:	opensensorfusion
 
 OPENCOMPUTE PTP CLOCK DRIVER
drivers/iio/opensensorfusion/osf_protocol.c new file +242 -0

242 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_protocol.h new file +100 -0

100 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v7 3/5] iio: osf: add protocol decoding

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The protocol major version and header reserved fields are not validated, contradicting the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The protocol major version and header reserved fields are not validated, contradicting the commit message. --

--- Patch [3]: [PATCH v7 3/5] iio: osf: add protocol decoding --- commit 4b68faaa805d7ad7aeb0c1d32700ea5abc7cccf2
Author: Jinseob Kim <[email protected]>

iio: osf: add protocol decoding

This commit adds helpers for decoding Open Sensor Fusion frame headers and supported message payloads.

Quoted 5 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +0 -0
new file mode 100644
index 0000000000000..b5749525e4c43

[ ... ]

Quoted 7 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +7 -0
+int osf_protocol_decode_frame(const u8 *buf, size_t len,
+			      struct osf_frame *frame, size_t *frame_len)
+{
+	u32 expected_crc;
+	u32 actual_crc;
+	u32 payload_len;
+	size_t total_len;

[ ... ]

drivers/iio/opensensorfusion/osf_protocol.c line 63

Quoted 11 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +11 -0
+	if (actual_crc != expected_crc)
+		return -EBADMSG;
+
+	frame->protocol_major = buf[4];
+	frame->protocol_minor = buf[5];
+	frame->message_type = get_unaligned_le16(buf + 8);
+	frame->payload_len = payload_len;
+	frame->sequence = get_unaligned_le64(buf + 14);
+	frame->timestamp_us = get_unaligned_le64(buf + 22);
+	frame->flags = get_unaligned_le32(buf + 30);
+	frame->reserved = get_unaligned_le32(buf + 34);

[Severity: Medium]
The commit message states: "The decoder validates the OSF0 wire magic, protocol major version, header length, payload bounds, reserved fields and CRC..."

Are the protocol major version and reserved fields actually being validated here in osf_protocol_decode_frame()?

It looks like buf[4] is being assigned directly to frame->protocol_major without checking it against OSF_PROTOCOL_MAJOR, and the reserved field is similarly assigned without verifying if it is zero. Could this lead to compatibility issues if a future protocol major version reuses the same magic number but alters field semantics?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#8 lore raw

PATCH v7 4/5 iio: osf: add authenticated stream parser

In reply to Jinseob Kim

Add a UART byte-stream parser for Open Sensor Fusion frames.

The parser searches for the OSF0 wire magic, keeps partial frames buffered, checks header length and payload bounds, and passes complete candidate frames to the core decoder.

Rejected candidate frames drop only the current head byte before resynchronizing, so a corrupted unauthenticated payload length cannot make the parser skip later valid frames.

---
 MAINTAINERS                               |   1 +
 drivers/iio/opensensorfusion/osf_stream.c | 189 ++++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_stream.h |  30 ++++
 3 files changed, 220 insertions(+)
 create mode 100644 drivers/iio/opensensorfusion/osf_stream.c
 create mode 100644 drivers/iio/opensensorfusion/osf_stream.h
MAINTAINERS +1 -0
index 32d3e7674..3d4199d9e 100644
@@ -20017,6 +20017,7 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
 F:	Documentation/iio/open-sensor-fusion.rst
 F:	drivers/iio/opensensorfusion/osf_protocol.*
+F:	drivers/iio/opensensorfusion/osf_stream.*
 K:	opensensorfusion
 
 OPENCOMPUTE PTP CLOCK DRIVER
drivers/iio/opensensorfusion/osf_stream.c new file +189 -0

189 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_stream.h new file +30 -0
new file mode 100644
index 000000000..c1761b6e6
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _OSF_STREAM_H
+#define _OSF_STREAM_H
+
+#include <linux/types.h>
+
+#define OSF_STREAM_MAX_FRAME_LEN	4096
+
+struct osf_device;
+
+struct osf_stream_stats {
+	u64 valid_frames;
+	u64 bad_magic_resyncs;
+	u64 bad_crc_frames;
+	u64 dropped_bytes;
+};
+
+struct osf_stream {
+	struct osf_device *osf;
+	u8 buf[OSF_STREAM_MAX_FRAME_LEN];
+	size_t len;
+	struct osf_stream_stats stats;
+};
+
+void osf_stream_init(struct osf_stream *stream, struct osf_device *osf);
+void osf_stream_reset(struct osf_stream *stream);
+int osf_stream_receive_bytes(struct osf_stream *stream, const u8 *buf,
+			     size_t len);
+
+#endif
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH v7 4/5] iio: osf: add authenticated stream parser

Quoted 9 lines · Jinseob Kim

Add a UART byte-stream parser for Open Sensor Fusion frames.

The parser searches for the OSF0 wire magic, keeps partial frames buffered, checks header length and payload bounds, and passes complete candidate frames to the core decoder.

Rejected candidate frames drop only the current head byte before resynchronizing, so a corrupted unauthenticated payload length cannot make the parser skip later valid frames.

...

drivers/iio/opensensorfusion/osf_stream.c line 39

Quoted 8 lines
drivers/iio/opensensorfusion/osf_stream.c new file +8 -0
+static bool osf_stream_frame_prefix_match(const u8 *buf, size_t len)
+{
+	for (size_t i = 0; i < len; i++) {
+		if (buf[i] != (u8)(OSF_FRAME_MAGIC >> (i * 8)))
+			return false;
+	}
+
+	return true;

Why so complicated? le32_to_cpup() + just integer comparison should work, no? Alternatively get_unaligned_le32() if the buffer is unaligned.

Quoted 1 line

+}

...

drivers/iio/opensensorfusion/osf_stream.c line 50

Quoted 9 lines
drivers/iio/opensensorfusion/osf_stream.c new file +9 -0
+static size_t osf_stream_discard_to_magic(struct osf_stream *stream)
+{
+	size_t old_len = stream->len;
+	size_t match_len;
+
+	for (size_t i = 0; i < stream->len; i++) {
+		match_len = stream->len - i;
+		if (match_len > OSF_STREAM_MAGIC_LEN)
+			match_len = OSF_STREAM_MAGIC_LEN;

Seems like reinvention of min() from minmax.h.

Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +10 -0
+		if (osf_stream_frame_prefix_match(stream->buf + i, match_len)) {
+			if (i)
+				osf_stream_discard(stream, i);
+			return i;
+		}
+	}
+
+	stream->len = 0;
+	return old_len;
+}

...

drivers/iio/opensensorfusion/osf_stream.c line 143

Quoted 13 lines
drivers/iio/opensensorfusion/osf_stream.c new file +13 -0
+void osf_stream_init(struct osf_stream *stream, struct osf_device *osf)
+{
+	if (!stream)
+		return;
+
+	stream->osf = osf;
+	stream->len = 0;
+	memset(&stream->stats, 0, sizeof(stream->stats));
+}
+
+void osf_stream_reset(struct osf_stream *stream)
+{
+	if (stream) {

I believe I have commented on this already. Please, go and double check all comments.

drivers/iio/opensensorfusion/osf_stream.c line 139

Quoted 3 lines
drivers/iio/opensensorfusion/osf_stream.c new file +3 -0
+		stream->len = 0;
+		memset(&stream->stats, 0, sizeof(stream->stats));
+	}

My comment was to use the same pattern as in _init() above.

Quoted 1 line

+}

...

drivers/iio/opensensorfusion/osf_stream.c line 150

Quoted 2 lines
drivers/iio/opensensorfusion/osf_stream.c new file +2 -0
+int osf_stream_receive_bytes(struct osf_stream *stream, const u8 *buf,
+			     size_t len)

Not sure, but likely also commented on this, id est to wrap on logical split:

int osf_stream_receive_bytes(struct osf_stream *stream,
const u8 *buf, size_t len)

OR simply do in a single line (it's only 82 characters with increased readability):

int osf_stream_receive_bytes(struct osf_stream *stream, const u8 *buf, size_t len)

Quoted 4 lines
drivers/iio/opensensorfusion/osf_stream.c new file +4 -0
+{
+	size_t copy_len;
+	size_t space;
+	int first_err = 0;

drivers/iio/opensensorfusion/osf_stream.c line 164

Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +10 -0
+	int ret;
+
+	if (!stream || !stream->osf || (!buf && len))
+		return -EINVAL;
+
+	if (!len) {
+		ret = osf_stream_process(stream);
+		if (ret && !first_err)
+			first_err = ret;
+		return first_err;

Why all this dances? first_err is 0, we all know this.

Quoted 11 lines
drivers/iio/opensensorfusion/osf_stream.c new file +11 -0
+	}
+
+	while (len) {
+		space = OSF_STREAM_MAX_FRAME_LEN - stream->len;
+		if (!space) {
+			stream->stats.dropped_bytes++;
+			osf_stream_discard(stream, 1);
+			if (!first_err)
+				first_err = -EMSGSIZE;
+			continue;
+		}

drivers/iio/opensensorfusion/osf_stream.c line 185

Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +10 -0
+
+		copy_len = len < space ? len : space;
+		memcpy(stream->buf + stream->len, buf, copy_len);
+		stream->len += copy_len;
+		buf += copy_len;
+		len -= copy_len;
+
+		ret = osf_stream_process(stream);
+		if (ret && !first_err)
+			first_err = ret;

This error checking and handling is unusual. It requires a good comment explaining what's going on.

Quoted 4 lines
drivers/iio/opensensorfusion/osf_stream.c new file +4 -0
+	}
+
+	return first_err;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v7 4/5] iio: osf: add authenticated stream parser

Quoted 2 lines

Why so complicated? le32_to_cpup() + just integer comparison should work, no? Alternatively get_unaligned_le32() if the buffer is unaligned.

I have reworked this in my local next revision. Complete four-byte candidates are now compared using get_unaligned_le32() and OSF_FRAME_MAGIC.

A small separate check remains only for preserving a trailing one-, two-, or three-byte "OSF0" prefix when the magic is split across receive callbacks. I added a comment explaining that case.

Quoted 1 line

Seems like reinvention of min() from minmax.h.

Agreed. The open-coded minimum calculations are now replaced with min().

Quoted 3 lines

I believe I have commented on this already. Please, go and double check all comments.

My comment was to use the same pattern as in _init() above.

osf_stream_reset() now uses the same early-return pattern as osf_stream_init(). I also re-audited the earlier comments, including the common magic definition, loop-local variables, redundant casts, GENMASK(), Kconfig wording, and partial-frame statistics.

Quoted 1 line

Why all this dances? first_err is 0, we all know this.

The zero-length path now directly returns osf_stream_process(stream).

Quoted 2 lines

This error checking and handling is unusual. It requires a good comment explaining what's going on.

The parser continues processing the current received chunk after a recoverable framing error so that later valid frames in the same chunk are not delayed until another callback. Only the first error is retained for diagnostic logging, while the serdev callback still reports the complete byte count as consumed.

I added a source comment documenting this behavior and verified that CRC-valid frames ignored by the core are consumed in full without resynchronizing into their payload.

thanks,

jinseob

#11 lore raw

Re: [PATCH v7 4/5] iio: osf: add authenticated stream parser

On Sat, Jul 11, 2026 at 12:24:12AM +0900, Kim Jinseob wrote:

What happened to your email? Regular MUAs add the line to see the quoting legend, and in your email it's gone?

...

Quoted 3 lines
Quoted 1 line

Seems like reinvention of min() from minmax.h.

Agreed. The open-coded minimum calculations are now replaced with min().

You too eagerly removed the context... But okay, it seems you agreed on the suggested changes.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

PATCH v7 5/5 iio: osf: add UART IIO driver

In reply to Jinseob Kim

Add the Open Sensor Fusion serdev transport, driver core, and IIO registration path as one complete driver patch.

The driver enables the required vcc regulator, receives OSF frames over UART, registers IIO devices from capability reports, supports direct raw reads from the latest sample cache, and pushes buffered samples into software kfifo buffers.

Use final Kconfig and Makefile contents from the start, claim IIO buffer mode while pushing samples, and use zeroed scan storage with explicit timestamp alignment so the driver does not depend on IIO core bounce-buffer padding behavior.

---
 MAINTAINERS                               |   5 +
 drivers/iio/Kconfig                       |   1 +
 drivers/iio/Makefile                      |   1 +
 drivers/iio/opensensorfusion/Kconfig      |  15 +
 drivers/iio/opensensorfusion/Makefile     |   6 +
 drivers/iio/opensensorfusion/osf_core.c   | 320 ++++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_core.h   |  70 +++++
 drivers/iio/opensensorfusion/osf_iio.c    | 308 +++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_iio.h    |  22 ++
 drivers/iio/opensensorfusion/osf_serdev.c | 114 ++++++++
 10 files changed, 862 insertions(+)
 create mode 100644 drivers/iio/opensensorfusion/Kconfig
 create mode 100644 drivers/iio/opensensorfusion/Makefile
 create mode 100644 drivers/iio/opensensorfusion/osf_core.c
 create mode 100644 drivers/iio/opensensorfusion/osf_core.h
 create mode 100644 drivers/iio/opensensorfusion/osf_iio.c
 create mode 100644 drivers/iio/opensensorfusion/osf_iio.h
 create mode 100644 drivers/iio/opensensorfusion/osf_serdev.c
MAINTAINERS +5 -0
index 3d4199d9e..6ce18ad92 100644
@@ -20016,7 +20016,12 @@ M:	Jinseob Kim <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
 F:	Documentation/iio/open-sensor-fusion.rst
+F:	drivers/iio/opensensorfusion/Kconfig
+F:	drivers/iio/opensensorfusion/Makefile
+F:	drivers/iio/opensensorfusion/osf_core.*
+F:	drivers/iio/opensensorfusion/osf_iio.*
 F:	drivers/iio/opensensorfusion/osf_protocol.*
+F:	drivers/iio/opensensorfusion/osf_serdev.c
 F:	drivers/iio/opensensorfusion/osf_stream.*
 K:	opensensorfusion
 
drivers/iio/Kconfig +1 -0
index 661127aed..939f6c546 100644
@@ -101,6 +101,7 @@ source "drivers/iio/light/Kconfig"
 source "drivers/iio/magnetometer/Kconfig"
 source "drivers/iio/multiplexer/Kconfig"
 source "drivers/iio/orientation/Kconfig"
+source "drivers/iio/opensensorfusion/Kconfig"
 source "drivers/iio/test/Kconfig"
 if IIO_TRIGGER
    source "drivers/iio/trigger/Kconfig"
drivers/iio/Makefile +1 -0
index cb80ef837..d864fe17b 100644
@@ -37,6 +37,7 @@ obj-y += light/
 obj-y += magnetometer/
 obj-y += multiplexer/
 obj-y += orientation/
+obj-y += opensensorfusion/
 obj-y += position/
 obj-y += potentiometer/
 obj-y += potentiostat/
drivers/iio/opensensorfusion/Kconfig new file +15 -0
new file mode 100644
index 000000000..f955a1f29
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config OPEN_SENSOR_FUSION
+	tristate "Open Sensor Fusion UART IIO driver"
+	depends on SERIAL_DEV_BUS
+	select CRC32
+	select IIO_BUFFER
+	select IIO_KFIFO_BUF
+	help
+	  Build the Open Sensor Fusion UART IIO driver.
+
+	  The driver receives OSF protocol frames over a serdev UART and
+	  registers IIO devices for supported capability entries. It exposes
+	  accelerometer, gyroscope, magnetometer, and temperature samples
+	  through IIO direct reads and software buffers.
drivers/iio/opensensorfusion/Makefile new file +6 -0
new file mode 100644
index 000000000..b4e03b80c
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_OPEN_SENSOR_FUSION) += open-sensor-fusion.o
+
+open-sensor-fusion-y := osf_core.o osf_iio.o osf_protocol.o osf_serdev.o \
+			 osf_stream.o
drivers/iio/opensensorfusion/osf_core.c new file +320 -0

320 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_core.h new file +70 -0

70 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_iio.c new file +308 -0

308 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_iio.h new file +22 -0
new file mode 100644
index 000000000..d0745167f
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _OSF_IIO_H
+#define _OSF_IIO_H
+
+#include <linux/types.h>
+
+#include "osf_protocol.h"
+
+struct device;
+struct iio_dev;
+struct osf_device;
+
+int osf_iio_register_sensor(struct device *dev,
+			    const struct osf_capability_entry *entry,
+			    struct osf_device *osf, struct iio_dev **indio_dev);
+void osf_iio_unregister_sensor(struct iio_dev *indio_dev);
+int osf_iio_push_sample(struct iio_dev *indio_dev, const s32 *values,
+			u16 channel_count);
+bool osf_iio_sensor_supported(u16 sensor_type, u16 channel_count);
+const char *osf_iio_sensor_name(u16 sensor_type);
+
+#endif
drivers/iio/opensensorfusion/osf_serdev.c new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH v7 5/5] iio: osf: add UART IIO driver

Hello,

Quoted 1 line · Jinseob Kim

+#include <linux/mod_devicetable.h>

Please don't add new users for this header file. Only use those <linux/device-id/*.h> that you actually need (if any).

Thanks
Uwe

#14 lore raw

Re: [PATCH v7 5/5] iio: osf: add UART IIO driver

In reply to Jinseob Kim

Hello,

Quoted 1 line · Jinseob Kim

+#include <linux/mod_devicetable.h>

Please don't add new users for this header file. Only use those <linux/device-id/*.h> that you actually need (if any).

Thanks
Uwe

#15 lore raw

Re: [PATCH v7 5/5] iio: osf: add UART IIO driver

In reply to Jinseob Kim
Quoted 39 lines · On Tue, 7 Jul 2026 10:45:25 +0900 Jinseob Kim <[email protected]> wrote:

Add the Open Sensor Fusion serdev transport, driver core, and IIO registration path as one complete driver patch.

The driver enables the required vcc regulator, receives OSF frames over UART, registers IIO devices from capability reports, supports direct raw reads from the latest sample cache, and pushes buffered samples into software kfifo buffers.

Use final Kconfig and Makefile contents from the start, claim IIO buffer mode while pushing samples, and use zeroed scan storage with explicit timestamp alignment so the driver does not depend on IIO core bounce-buffer padding behavior.

---
 MAINTAINERS                               |   5 +
 drivers/iio/Kconfig                       |   1 +
 drivers/iio/Makefile                      |   1 +
 drivers/iio/opensensorfusion/Kconfig      |  15 +
 drivers/iio/opensensorfusion/Makefile     |   6 +
 drivers/iio/opensensorfusion/osf_core.c   | 320 ++++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_core.h   |  70 +++++
 drivers/iio/opensensorfusion/osf_iio.c    | 308 +++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_iio.h    |  22 ++
 drivers/iio/opensensorfusion/osf_serdev.c | 114 ++++++++
 10 files changed, 862 insertions(+)
 create mode 100644 drivers/iio/opensensorfusion/Kconfig
 create mode 100644 drivers/iio/opensensorfusion/Makefile
 create mode 100644 drivers/iio/opensensorfusion/osf_core.c
 create mode 100644 drivers/iio/opensensorfusion/osf_core.h
 create mode 100644 drivers/iio/opensensorfusion/osf_iio.c
 create mode 100644 drivers/iio/opensensorfusion/osf_iio.h
 create mode 100644 drivers/iio/opensensorfusion/osf_serdev.c
MAINTAINERS +0 -0
index 3d4199d9e..6ce18ad92 100644

MAINTAINERS M: Jinseob Kim <[email protected]> · line 20025

Quoted 10 lines
MAINTAINERS +5 -0
@@ -20016,7 +20016,12 @@ M:	Jinseob Kim <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
 F:	Documentation/iio/open-sensor-fusion.rst
+F:	drivers/iio/opensensorfusion/Kconfig
+F:	drivers/iio/opensensorfusion/Makefile
+F:	drivers/iio/opensensorfusion/osf_core.*
+F:	drivers/iio/opensensorfusion/osf_iio.*
 F:	drivers/iio/opensensorfusion/osf_protocol.*
+F:	drivers/iio/opensensorfusion/osf_serdev.c
 F:	drivers/iio/opensensorfusion/osf_stream.*

Looking at the list of F: entries, wouldn't it be better to just
do F: drivers/iio/opensensorfusion/?

Signature · 3 lines
Kind regards

CJD
maintainer
#16 lore raw

Re: [PATCH v7 5/5] iio: osf: add UART IIO driver

In reply to Jinseob Kim
Quoted 8 lines · On Tue, 7 Jul 2026 10:45:25 +0900 Jinseob Kim <[email protected]> wrote:

Add the Open Sensor Fusion serdev transport, driver core, and IIO registration path as one complete driver patch.

The driver enables the required vcc regulator, receives OSF frames over UART, registers IIO devices from capability reports, supports direct raw reads from the latest sample cache, and pushes buffered samples into software kfifo buffers.

Quoted 6 lines

Use final Kconfig and Makefile contents from the start, claim IIO buffer mode while pushing samples, and use zeroed scan storage with explicit timestamp alignment so the driver does not depend on IIO core bounce-buffer padding behavior.

A few things inline.

drivers/iio/opensensorfusion/osf_core.c line 238

Quoted 8 lines
drivers/iio/opensensorfusion/osf_core.c new file +2 -0
new file mode 100644
index 000000000..02d55a201
 
+
+int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len)

This is only defined in this patch but is used in patch 4. Make sure your code builds after each patch so as to avoid breaking code bisection.

Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10 -0
+{
+	struct osf_frame frame;
+	size_t frame_len;
+	int ret;
+
+	ret = osf_protocol_decode_frame(buf, len, &frame, &frame_len);
+	if (ret)
+		return ret;
+
+	if (frame_len != len)

...

Quoted 5 lines
drivers/iio/opensensorfusion/osf_iio.c new file +0 -0
new file mode 100644
index 000000000..91afcf3b8

drivers/iio/opensensorfusion/osf_iio.c line 48

Quoted 9 lines
drivers/iio/opensensorfusion/osf_iio.c new file +8 -0
 
+
+#define OSF_SCAN_TYPE_S32						\
+	{								\
+		.sign = 's',						\
+		.realbits = 32,					\
+		.storagebits = 32,					\
+		.endianness = IIO_CPU,					\
+	}

This feels like going too far to deduplicate just this. Put a copy inline in each of the other macros where it is used and drop this one.

Quoted 15 lines
drivers/iio/opensensorfusion/osf_iio.c new file +13 -0
+
+#define OSF_MOD_CHAN(_type, _mod, _idx)				\
+	{								\
+		.type = (_type),					\
+		.modified = 1,					\
+		.channel2 = (_mod),					\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
+		.scan_index = (_idx),					\
+		.scan_type = OSF_SCAN_TYPE_S32,			\
+	}
 
 
+int osf_iio_push_sample(struct iio_dev *indio_dev, const s32 *values,
+			u16 channel_count)

drivers/iio/opensensorfusion/osf_iio.c

Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +10 -0
+{
+	struct osf_iio_state *state = iio_priv(indio_dev);
+	s64 timestamp;
+	int ret;
+
+	if (channel_count != state->spec->channel_count)
+		return -EPROTO;
+
+	if (!iio_device_try_claim_buffer_mode(indio_dev))
+		return 0;

Looking at this again, why do we care about holding the device in buffered mode for this? Races should be safe without that big hammer. A simple check on iio_buffer_enabled() should ensure data is only pushed when it is enabled, or just after it is disabled (which should always be safe).

drivers/iio/opensensorfusion/osf_iio.c line 287

Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +10 -0
+
+	timestamp = iio_get_time_ns(indio_dev);
+
+	switch (channel_count) {
+	case 1: {
+		struct osf_iio_scan_1axis scan = { };
+
+		scan.value = values[0];
+		ret = iio_push_to_buffers_with_ts(indio_dev, &scan,
+						  sizeof(scan), timestamp);

With change above direct returns here

drivers/iio/opensensorfusion/osf_iio.c line 296

Quoted 9 lines
drivers/iio/opensensorfusion/osf_iio.c new file +9 -0
+		break;
+	}
+	case 3: {
+		struct osf_iio_scan_3axis scan = { };
+
+		scan.values[0] = values[0];
+		scan.values[1] = values[1];
+		scan.values[2] = values[2];
+		ret = iio_push_to_buffers_with_ts(indio_dev, &scan,

here

drivers/iio/opensensorfusion/osf_iio.c line 301

Quoted 5 lines
drivers/iio/opensensorfusion/osf_iio.c new file +5 -0
+						  sizeof(scan), timestamp);
+		break;
+	}
+	default:
+		ret = -EPROTO;

and here should be fine as no need to release it.

Quoted 7 lines
drivers/iio/opensensorfusion/osf_iio.c new file +7 -0
+		break;
+	}
+
+	iio_device_release_buffer_mode(indio_dev);
+
+	return ret;
+}
#17 lore raw

Re: [PATCH v7 5/5] iio: osf: add UART IIO driver

Quoted 2 lines

This is only defined in this patch but is used in patch 4. Make sure your code builds after each patch so as to avoid breaking code bisection.

I changed patch 4 in my local next revision so that the stream parser no longer depends on osf_core.h or osf_core_receive_frame().

The stream parser now accepts a typed frame handler and an opaque context. Patch 5 provides the wrapper that calls osf_core_receive_frame().

I compiled the sources introduced by patches 3 and 4 separately using temporary uncommitted Kbuild harnesses, and built and linked the complete module after patch 5.

Quoted 2 lines

This feels like going too far to deduplicate just this. Put a copy inline in each of the other macros where it is used and drop this one.

I removed OSF_SCAN_TYPE_S32 and placed the scan_type initializer directly in both channel macros.

Quoted 5 lines

Looking at this again, why do we care about holding the device in buffered mode for this? Races should be safe without that big hammer. A simple check on iio_buffer_enabled() should ensure data is only pushed when it is enabled, or just after it is disabled (which should always be safe).

I replaced the buffer-mode claim and release with an iio_buffer_enabled() check.

The one-axis and three-axis cases now return directly from iio_push_to_buffers_with_ts(), and the default case directly returns -EPROTO. The zero-initialized scan structures and aligned timestamps are unchanged.

Thanks,

Jinseob