Skip to contentSkip to conversation

iio: add Open Sensor Fusion UART driver

RFC PATCH Superseded Jinseob Kim 6 participants 13 messages May 20 – May 22 1 message not in the archive

Subsystems (2)

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

cover iio: add Open Sensor Fusion UART driver

Open Sensor Fusion is a UART-attached sensor aggregation device. The current prototype sends OSF protocol v0 frames over a host UART link. This RFC adds the first Linux IIO receive path for that UART stream.

The first transport is serdev UART. USB, real sensor reads, fusion output, and production timestamp correlation are intentionally left out of this series.

The driver code is kept in one patch for the first RFC to avoid intermediate non-buildable states. The internal files are still split by protocol, stream, core, transport, and IIO code.

Current validation coverage:

- STM32F405 test firmware OSF protocol v0 UART stream at 115200 8N1.
- Raspberry Pi 4 serdev probe and receive path to osf_core_receive_frame().
- CAPABILITY_REPORT-driven IIO device registration.
- IIO read_raw for accel, gyro, magn, and temp samples.
- IIO software kfifo buffer userspace read.
- scan decode helper for buffered samples.
- dt_binding_check pass for the binding.
- checkpatch.pl --strict with ERROR 0, WARNING 0, CHECK 0.
- W=1 source compile/link phase clean in the staging kernel tree.
- modpost unresolved symbol warnings came from the staging tree missing Module.symvers.

Known limits:

- The sample source is synthetic stream data, not real ICM-42688-P or MMC5983MA sensor reads.
- The IIO timestamp is Linux host receive time. Device time correlation is still open.
- Runtime capability removal is not implemented.
- The staging layout uses drivers/iio/opensensorfusion/. The final directory is open for review.

Review feedback wanted on the IIO device layout, timestamp policy, binding shape, and driver directory.

Jinseob Kim (3):
dt-bindings: iio: imu: add Open Sensor Fusion UART binding
iio: osf: add Open Sensor Fusion UART IIO driver
MAINTAINERS: add Open Sensor Fusion IIO driver entry

 .../iio/imu/opensensorfusion,osf-uart.yaml    |  33 ++
 MAINTAINERS                                   |   7 +
 drivers/iio/Kconfig                           |   1 +
 drivers/iio/Makefile                          |   1 +
 drivers/iio/opensensorfusion/Kconfig          |  15 +
 drivers/iio/opensensorfusion/Makefile         |   6 +
 drivers/iio/opensensorfusion/osf_core.c       | 334 +++++++++++++++++
 drivers/iio/opensensorfusion/osf_core.h       |  81 ++++
 drivers/iio/opensensorfusion/osf_iio.c        | 268 +++++++++++++
 drivers/iio/opensensorfusion/osf_iio.h        |  21 ++
 drivers/iio/opensensorfusion/osf_protocol.c   | 234 ++++++++++++
 drivers/iio/opensensorfusion/osf_protocol.h   | 100 +++++
 drivers/iio/opensensorfusion/osf_serdev.c     | 354 ++++++++++++++++++
 drivers/iio/opensensorfusion/osf_serdev.h     |   8 +
 drivers/iio/opensensorfusion/osf_stream.c     | 212 +++++++++++
 drivers/iio/opensensorfusion/osf_stream.h     |  31 ++
 16 files changed, 1706 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml
 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_protocol.c
 create mode 100644 drivers/iio/opensensorfusion/osf_protocol.h
 create mode 100644 drivers/iio/opensensorfusion/osf_serdev.c
 create mode 100644 drivers/iio/opensensorfusion/osf_serdev.h
 create mode 100644 drivers/iio/opensensorfusion/osf_stream.c
 create mode 100644 drivers/iio/opensensorfusion/osf_stream.h
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 1/3 dt-bindings: iio: imu: add Open Sensor Fusion UART binding

Add a binding for a UART-attached Open Sensor Fusion device.

The device is exposed as a serdev child node using the opensensorfusion,osf-uart compatible string. Keep Raspberry Pi overlay and test notes outside the binding.

The binding has been checked with dt_binding_check.

---
 .../iio/imu/opensensorfusion,osf-uart.yaml    | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +33 -0
new file mode 100644
index 000000000..6f329c326
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/opensensorfusion,osf-uart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Open Sensor Fusion UART Sensor Aggregation Device
+
+maintainers:
+  - Jinseob Kim <[email protected]>
+
+description: |
+  Open Sensor Fusion is a UART-attached sensor aggregation device. The device
+  exposes an OSF protocol v0 data stream over its host UART interface and may
+  report capabilities and samples for multiple sensor classes.
+
+properties:
+  compatible:
+    const: opensensorfusion,osf-uart
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    serial {
+        sensor {
+            compatible = "opensensorfusion,osf-uart";
+        };
+    };
+...
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH RFC 1/3] dt-bindings: iio: imu: add Open Sensor Fusion UART binding

Quoted 7 lines · Jinseob Kim

Add a binding for a UART-attached Open Sensor Fusion device.

The device is exposed as a serdev child node using the opensensorfusion,osf-uart compatible string.

Keep Raspberry Pi overlay and
test notes outside the binding.

This should not be in the commit message, it's not relevant info.

Quoted 1 line

The binding has been checked with dt_binding_check.

This is a requirement, there's no reason to state it in the commit message.

Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml

Quoted 12 lines
---
 .../iio/imu/opensensorfusion,osf-uart.yaml    | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +0 -0
new file mode 100644
index 000000000..6f329c326

I find it hard to believe that this is an "imu". It sounds more like this is a bus or remoteproc than anything else.

Quoted 4 lines
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +3 -0
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---

Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml line 13

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +10 -0
+$id: http://devicetree.org/schemas/iio/imu/opensensorfusion,osf-uart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Open Sensor Fusion UART Sensor Aggregation Device
+
+maintainers:
+  - Jinseob Kim <[email protected]>
+
+description: |
+  Open Sensor Fusion is a UART-attached sensor aggregation device. The device

What actually is this device?
Googling "open sensor fusion" gives me
https://github.com/memsindustrygroup/Open-Source-Sensor-Fusion
and
https://www.nxp.com/company/about-nxp/smarter-world-blog/BL-OPEN-SOURCE-SENSOR-FUSION
but neither of these explain to me what sort of device we are looking at here. Seems as if it is not /a/ device at all, but could be one of many devices, provided that they implement the other side of the protocol?

Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml line 14

Quoted 1 line
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +1 -0
+  exposes an OSF protocol v0 data stream over its host UART interface and may

What does "v0" mean here? Is the data format not complete yet? Are versions of the protocol likely to be backwards compatible? Will the device identify what version of the protocol it implements?

Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml line 15

Quoted 1 line
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +1 -0
+  report capabilities and samples for multiple sensor classes.

Yeah, then definitely this is not an "imu".

Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml line 19

Quoted 4 lines
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +4 -0
+
+properties:
+  compatible:
+    const: opensensorfusion,osf-uart

If this is some sort of remote processor slaved to the os, it seems possible that the may be required to do turn its power or reset it somehow. Having nothing but a compatible seems incomplete to me but I have no idea what the complete set of properties would actually be.

Probably a good bit of discussion is needed here, but for now:
pw-bot: changes-requested

Thanks,
Conor.

Quoted 18 lines
Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml new file +14 -0
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    serial {
+        sensor {
+            compatible = "opensensorfusion,osf-uart";
+        };
+    };
+...
Signature · 1 line
2.43.0
#3 lore raw

RFC PATCH 2/3 iio: osf: add Open Sensor Fusion UART IIO driver

In reply to Jinseob Kim

Add the initial Open Sensor Fusion UART IIO driver.

This includes the protocol v0 parser, OSF0 stream assembler, core capability/sample cache, serdev UART transport, IIO read_raw path, and software kfifo buffer support.

The first RFC keeps the driver code in one patch to avoid intermediate non-buildable states. The internal files still separate protocol, stream, core, transport, and IIO code.

The tested path is STM32F405 test firmware OSF UART stream to Raspberry Pi serdev, then IIO read_raw and buffered userspace reads.

Not included here: real sensor reads, USB transport, fusion/AHRS/Kalman, runtime capability removal, or production timestamp correlation.

---
 drivers/iio/Kconfig                         |   1 +
 drivers/iio/Makefile                        |   1 +
 drivers/iio/opensensorfusion/Kconfig        |  15 +
 drivers/iio/opensensorfusion/Makefile       |   6 +
 drivers/iio/opensensorfusion/osf_core.c     | 334 ++++++++++++++++++
 drivers/iio/opensensorfusion/osf_core.h     |  81 +++++
 drivers/iio/opensensorfusion/osf_iio.c      | 268 +++++++++++++++
 drivers/iio/opensensorfusion/osf_iio.h      |  21 ++
 drivers/iio/opensensorfusion/osf_protocol.c | 234 +++++++++++++
 drivers/iio/opensensorfusion/osf_protocol.h | 100 ++++++
 drivers/iio/opensensorfusion/osf_serdev.c   | 354 ++++++++++++++++++++
 drivers/iio/opensensorfusion/osf_serdev.h   |   8 +
 drivers/iio/opensensorfusion/osf_stream.c   | 212 ++++++++++++
 drivers/iio/opensensorfusion/osf_stream.h   |  31 ++
 14 files changed, 1666 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_protocol.c
 create mode 100644 drivers/iio/opensensorfusion/osf_protocol.h
 create mode 100644 drivers/iio/opensensorfusion/osf_serdev.c
 create mode 100644 drivers/iio/opensensorfusion/osf_serdev.h
 create mode 100644 drivers/iio/opensensorfusion/osf_stream.c
 create mode 100644 drivers/iio/opensensorfusion/osf_stream.h
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..1db1dbe56
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config OPEN_SENSOR_FUSION
+	tristate "Open Sensor Fusion UART IIO driver"
+	depends on IIO
+	depends on SERIAL_DEV_BUS
+	select CRC32
+	select IIO_BUFFER
+	select IIO_KFIFO_BUF
+	help
+	  Build the Open Sensor Fusion UART IIO driver.
+
+	  This includes the serdev UART receive path for OSF0 protocol
+	  bring-up. It does not include real sensor reads, USB transport,
+	  or fusion/AHRS/Kalman output.
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 +334 -0

334 lines not on this page · raw message

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

81 lines not on this page · raw message

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

268 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_iio.h new file +21 -0
new file mode 100644
index 000000000..1e8d7a39b
@@ -0,0 +1,21 @@
+/* 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_sample_event;
+
+int osf_iio_register_sensor(struct device *dev,
+			    const struct osf_capability_entry *entry,
+			    void *driver_data, struct iio_dev **indio_dev);
+int osf_iio_push_sample(struct iio_dev *indio_dev,
+			const struct osf_sample_event *event);
+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_protocol.c new file +234 -0

234 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

drivers/iio/opensensorfusion/osf_serdev.c new file +354 -0

354 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_serdev.h new file +8 -0
new file mode 100644
index 000000000..278b088df
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _OSF_SERDEV_H
+#define _OSF_SERDEV_H
+
+int osf_serdev_register_driver(void);
+void osf_serdev_unregister_driver(void);
+
+#endif
drivers/iio/opensensorfusion/osf_stream.c new file +212 -0

212 lines not on this page · raw message

drivers/iio/opensensorfusion/osf_stream.h new file +31 -0
new file mode 100644
index 000000000..f7f9477fe
@@ -0,0 +1,31 @@
+/* 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 partial_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
#4 lore raw

Re: [PATCH RFC 2/3] iio: osf: add Open Sensor Fusion UART IIO driver

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] The IIO channels are declared as little-endian (IIO_LE), but the driver pushes CPU-native endian…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The IIO channels are declared as little-endian (IIO_LE), but the driver pushes CPU-native endian data to the IIO kfifo buffer.
- [High] The driver pushes unpacked channel data into the IIO buffer but fails to set available_scan_masks, causing incorrect data demuxing and timestamp overwriting when a subset of channels is active.
- [Medium] Unbounded memory leak via devm_ allocations in the error path of dynamic IIO device registration, triggered by repeated capability reports.
- [Medium] The driver hardcodes ktime_get_ns() for sample timestamps, breaking the IIO subsystem's clock selection mechanism.
--

commit 07b7754f2b44c1840fd85c5de6bb124482d998fd
Author: Jinseob Kim <[email protected]>

iio: osf: add Open Sensor Fusion UART IIO driver

Add the initial Open Sensor Fusion UART IIO driver. This includes the protocol v0 parser, OSF0 stream assembler, core capability/sample cache, serdev UART transport, IIO read_raw path, and software kfifo buffer support.

Quoted 6 lines
drivers/iio/opensensorfusion/osf_core.c new file +0 -0
new file mode 100644
index 0000000000000..fd2eccefc47d6
@@ -0,0 +1,334 @@

[ ... ]

Quoted 3 lines
drivers/iio/opensensorfusion/osf_core.c new file +3 -0
+static int osf_core_handle_sensor_sample(struct osf_device *osf,
+					 const struct osf_frame *frame)
+{

[ ... ]

drivers/iio/opensensorfusion/osf_core.c line 97

Quoted 8 lines
drivers/iio/opensensorfusion/osf_core.c new file +8 -0
+	event.sensor_type = sample.sensor_type;
+	event.sensor_index = sample.sensor_index;
+	event.channel_count = sample.channel_count;
+	event.sample_format = sample.sample_format;
+	event.scale_nano = sample.scale_nano;
+	event.sequence = frame->sequence;
+	event.timestamp_us = frame->timestamp_us;
+	event.host_timestamp_ns = ktime_get_ns();

[Severity: Medium]
Does this hardcode the timestamp clock to CLOCK_MONOTONIC?

The IIO subsystem supports user-configurable clocks via the current_timestamp_clock sysfs attribute. Should this use iio_get_time_ns() instead to honor the user's clock selection?

Quoted 2 lines
drivers/iio/opensensorfusion/osf_core.c new file +2 -0
+	for (i = 0; i < sample.channel_count; i++)
+		event.values[i] = values[i];

[ ... ]

Quoted 6 lines
drivers/iio/opensensorfusion/osf_iio.c new file +0 -0
new file mode 100644
index 0000000000000..3d2674e43fbb0
@@ -0,0 +1,268 @@

[ ... ]

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

[ ... ]

Quoted 7 lines
drivers/iio/opensensorfusion/osf_iio.c new file +7 -0
+int osf_iio_push_sample(struct iio_dev *indio_dev,
+			const struct osf_sample_event *event)
+{
+	struct osf_iio_state *state;
+	u8 scan[OSF_IIO_SCAN_BYTES] __aligned(8) = { };
+	s32 *scan_values = (s32 *)scan;
+	unsigned int i;

[ ... ]

drivers/iio/opensensorfusion/osf_iio.c line 217

Quoted 5 lines
drivers/iio/opensensorfusion/osf_iio.c new file +5 -0
+	for (i = 0; i < event->channel_count; i++)
+		scan_values[i] = event->values[i];
+
+	return iio_push_to_buffers_with_timestamp(indio_dev, scan,
+						  event->host_timestamp_ns);

[Severity: High]
Does this code push CPU-native endian data to the IIO buffer?

The channels are declared as IIO_LE, so userspace on big-endian architectures will byte-swap this data again. Should the values be converted back using cpu_to_le32(), or should the channel be declared as IIO_CPU?

Quoted 6 lines
drivers/iio/opensensorfusion/osf_iio.c new file +6 -0
+}
+
+int osf_iio_register_sensor(struct device *dev,
+			    const struct osf_capability_entry *entry,
+			    void *driver_data, struct iio_dev **indio_dev)
+{

[ ... ]

drivers/iio/opensensorfusion/osf_iio.c line 254

Quoted 5 lines
drivers/iio/opensensorfusion/osf_iio.c new file +5 -0
+	iio_dev->name = spec->name;
+	iio_dev->info = &osf_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
+	iio_dev->channels = spec->channels;
+	iio_dev->num_channels = spec->num_channels;

[Severity: High]
Does this code omit setting available_scan_masks while pushing unpacked data into the buffer?

If userspace enables only a subset of channels, the IIO core will expect the data to be tightly packed. The dynamic timestamp placement by iio_push_to_buffers_with_timestamp() could then overwrite active channel data. Providing available_scan_masks would enable the software demuxing fallback to correct the data alignment.

Quoted 2 lines
drivers/iio/opensensorfusion/osf_iio.c new file +2 -0
+
+	ret = devm_iio_kfifo_buffer_setup(dev, iio_dev, NULL);

[ ... ]

Quoted 6 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +0 -0
new file mode 100644
index 0000000000000..1827760c27f24
@@ -0,0 +1,354 @@

[ ... ]

Quoted 6 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +6 -0
+static int osf_serdev_register_iio(struct osf_serdev *osf_uart,
+				   const struct osf_capability_entry *entry)
+{
+	struct device *dev = &osf_uart->serdev->dev;
+	struct iio_dev *indio_dev;
+	int ret;

[ ... ]

drivers/iio/opensensorfusion/osf_serdev.c line 106

Quoted 3 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +3 -0
+	ret = osf_iio_register_sensor(dev, entry, &osf_uart->osf, &indio_dev);
+	if (ret)
+		return ret;

[Severity: Medium]
Does this error path leak the devm_ allocated resources if device registration fails?

Since the sensor isn't added to the iio array on failure, a new capability report with a different sequence number will trigger registration again. This could potentially lead to an unbounded memory leak until out-of-memory.

Should this explicitly free the devm_ resources on failure?

Quoted 2 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +2 -0
+
+	osf_uart->iio[osf_uart->iio_count].sensor_type = entry->sensor_type;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw

Re: [PATCH RFC 2/3] iio: osf: add Open Sensor Fusion UART IIO driver

In reply to Jinseob Kim
Quoted 10 lines · On Wed, 20 May 2026 16:28:42 +0900 Jinseob Kim <[email protected]> wrote:

Add the initial Open Sensor Fusion UART IIO driver.

This includes the protocol v0 parser, OSF0 stream assembler, core capability/sample cache, serdev UART transport, IIO read_raw path, and software kfifo buffer support.

The first RFC keeps the driver code in one patch to avoid intermediate non-buildable states. The internal files still separate protocol, stream, core, transport, and IIO code.

Quoted 6 lines

The tested path is STM32F405 test firmware OSF UART stream to Raspberry Pi serdev, then IIO read_raw and buffered userspace reads.

Not included here: real sensor reads, USB transport, fusion/AHRS/Kalman, runtime capability removal, or production timestamp correlation.

There are several things in here I don't follow. Why do we not push the data directly to the iio devices when it shows up? There seems to be a queue in between that I don't understand the purpose of - that just seems to mean async handling where synchronous should be fine.

Anyhow, as I mentioned this review is somewhat superficial given the driver needs a lot of work to get it into a more reviewable state.
In particular break it up into more sensible sized chunks, building up functionality from device discovery to actually registering IIO devices and then adding additional features on that base.

Quoted 22 lines
drivers/iio/opensensorfusion/osf_core.c new file +13 -0
new file mode 100644
index 000000000..fd2eccefc
@@ -0,0 +1,334 @@
 
+
+static int osf_core_handle_sensor_sample(struct osf_device *osf,
+					 const struct osf_frame *frame)
+{
+	osf_sample_callback_t callback;
+	void *callback_context;
+	struct osf_latest_sample *latest;
+	struct osf_sample_event event = { };
+	struct osf_sensor_sample sample;
+	s32 values[OSF_MAX_SAMPLE_CHANNELS] = { };
+	unsigned long flags;
+	unsigned int i;
+	int ret;

drivers/iio/opensensorfusion/osf_core.c line 85

Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10 -0
+
+	ret = osf_protocol_decode_sensor_sample(frame, &sample);
+	if (ret)
+		return ret;
+
+	if (sample.channel_count > OSF_MAX_SAMPLE_CHANNELS)
+		return -E2BIG;
+
+	for (i = 0; i < sample.channel_count; i++) {
+		ret = osf_protocol_sensor_sample_value(&sample, i, &values[i]);

This converts to CPU endian. That's fine but then your channels aren't little endian.

drivers/iio/opensensorfusion/osf_core.c line 97

Quoted 12 lines
drivers/iio/opensensorfusion/osf_core.c new file +12 -0
+		if (ret)
+			return ret;
+	}
+
+	event.sensor_type = sample.sensor_type;
+	event.sensor_index = sample.sensor_index;
+	event.channel_count = sample.channel_count;
+	event.sample_format = sample.sample_format;
+	event.scale_nano = sample.scale_nano;
+	event.sequence = frame->sequence;
+	event.timestamp_us = frame->timestamp_us;
+	event.host_timestamp_ns = ktime_get_ns();

For IIO stuff we have (for weird historical reasons) rather complex clock handling for timestamps. Please follow that.

Quoted 28 lines
drivers/iio/opensensorfusion/osf_core.c new file +27 -0
+	for (i = 0; i < sample.channel_count; i++)
+		event.values[i] = values[i];
+
+	spin_lock_irqsave(&osf->lock, flags);
+	latest = osf_core_find_latest_sample(osf, sample.sensor_type,
+					     sample.sensor_index, true);
+	if (!latest) {
+		spin_unlock_irqrestore(&osf->lock, flags);
+		return -ENOSPC;
+	}
+
+	osf_core_store_latest_sample(latest, &sample, values, frame);
+	osf_core_store_latest_sample(&osf->latest_sample, &sample, values,
+				     frame);
+	osf->last_sequence = frame->sequence;
+	callback = osf->sample_callback;
+	callback_context = osf->sample_callback_context;
+	spin_unlock_irqrestore(&osf->lock, flags);
+
+	if (callback)
+		callback(callback_context, &event);
+
+	return 0;
+}
 
+
+int osf_core_read_latest_sample(struct osf_device *osf, u16 sensor_type,
+				u16 sensor_index, unsigned int channel,

drivers/iio/opensensorfusion/osf_core.c line 230

Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10 -0
+				s32 *value)
+{
+	const struct osf_latest_sample *latest;
+	unsigned long flags;
+	int ret = 0;
+
+	if (!osf || !value)
+		return -EINVAL;
+
+	spin_lock_irqsave(&osf->lock, flags);

Use guard() and see if you can relax to a mutex. I haven't really worked out the flow in here yet so maybe you can't.

Quoted 32 lines
drivers/iio/opensensorfusion/osf_core.c new file +31 -0
+	latest = osf_core_find_latest_sample(osf, sensor_type,
+					     sensor_index, false);
+	if (!latest)
+		latest = &osf->latest_sample;
+
+	if (!latest->valid) {
+		ret = -ENODATA;
+		goto out_unlock;
+	}
+
+	if (latest->sensor_type != sensor_type ||
+	    latest->sensor_index != sensor_index) {
+		ret = -ENODATA;
+		goto out_unlock;
+	}
+
+	if (channel >= latest->channel_count) {
+		ret = -ENODATA;
+		goto out_unlock;
+	}
+
+	*value = latest->values[channel];
+
+out_unlock:
+	spin_unlock_irqrestore(&osf->lock, flags);
+
+	return ret;
+}
 
+
+bool osf_core_capability_sequence(struct osf_device *osf, u64 *sequence)
+{

drivers/iio/opensensorfusion/osf_core.c line 299

Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10 -0
+	unsigned long flags;
+	bool valid;
+
+	if (!osf || !sequence)
+		return false;
+
+	spin_lock_irqsave(&osf->lock, flags);
+	valid = osf->capability_cache.valid;
+	if (valid)
+		*sequence = osf->capability_cache.sequence;

use guard() above
and if (valid)

		return false;

	*sequence = ..
	return true;

That way the 'error' path is out of line and can be quickly ignored by readers.

Quoted 5 lines
drivers/iio/opensensorfusion/osf_core.c new file +5 -0
+	spin_unlock_irqrestore(&osf->lock, flags);
+
+	return valid;
+}
+

drivers/iio/opensensorfusion/osf_core.c line 314

Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10 -0
+void osf_core_set_sample_callback(struct osf_device *osf,
+				  osf_sample_callback_t callback,
+				  void *context)
+{
+	unsigned long flags;
+
+	if (!osf)
+		return;
+
+	spin_lock_irqsave(&osf->lock, flags);

Use guard() for these probably.

Quoted 4 lines
drivers/iio/opensensorfusion/osf_core.c new file +4 -0
+	osf->sample_callback = callback;
+	osf->sample_callback_context = context;
+	spin_unlock_irqrestore(&osf->lock, flags);
+}

drivers/iio/opensensorfusion/osf_core.c line 328

Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10 -0
+
+static int __init osf_core_init(void)
+{
+	return osf_serdev_register_driver();
+}
+
+static void __exit osf_core_exit(void)
+{
+	osf_serdev_unregister_driver();
+}

This smells like code break out not making sense. I'd expect to see the driver_structure right next to these.

Quoted 15 lines
drivers/iio/opensensorfusion/osf_core.c new file +3 -0
+
+module_init(osf_core_init);
+module_exit(osf_core_exit);
drivers/iio/opensensorfusion/osf_iio.c new file +5 -0
new file mode 100644
index 000000000..3d2674e43
@@ -0,0 +1,268 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/bitops.h>
+#include <linux/device.h>
+#include <linux/errno.h>

drivers/iio/opensensorfusion/osf_iio.c line 15

Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +10 -0
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/kernel.h>
+#include <linux/types.h>
+
+#include "osf_core.h"
+#include "osf_iio.h"
+
+#define OSF_SCALE_NANO		1000000000U

NANO is defined in the kernel headers.

Quoted 15 lines
drivers/iio/opensensorfusion/osf_iio.c new file +15 -0
+#define OSF_IIO_SCAN_BYTES						\
+	(ALIGN(OSF_MAX_SAMPLE_CHANNELS * sizeof(s32), sizeof(s64)) +	\
+	 sizeof(s64))
+
+struct osf_iio_sensor_spec {
+	u16 sensor_type;
+	u16 channel_count;
+	const char *name;
+	const struct iio_chan_spec *channels;
+	unsigned int num_channels;
+};
+
+struct osf_iio_state {
+	const struct osf_iio_sensor_spec *spec;
+	u32 scale_nano;

drivers/iio/opensensorfusion/osf_iio.c

Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +8 -0
+	u16 sensor_index;
+	struct osf_device *osf;
+};
 
 
+int osf_iio_push_sample(struct iio_dev *indio_dev,
+			const struct osf_sample_event *event)
+{
+	struct osf_iio_state *state;
+	u8 scan[OSF_IIO_SCAN_BYTES] __aligned(8) = { };

we have macros for this.
IIO_DECLARE_BUFFER_WITH_TS() which will give you a suitable sized s32 array so you don't need the cast that follows.

Mind you the buffer is described as little endian so maybe should be __le32

drivers/iio/opensensorfusion/osf_iio.c line 200

Quoted 7 lines
drivers/iio/opensensorfusion/osf_iio.c new file +7 -0
+	s32 *scan_values = (s32 *)scan;
+	unsigned int i;
+
+	if (!indio_dev || !event)
+		return -EINVAL;
+
+	if (!iio_buffer_enabled(indio_dev))

I haven't checked, but this feels insufficient. Probably want to force the device to stay in buffered mode. iio_device_try_claim_buffer_mode() (rarely used, but this may be one place that needs it).

Quoted 7 lines
drivers/iio/opensensorfusion/osf_iio.c new file +7 -0
+		return 0;
+
+	state = iio_priv(indio_dev);
+	if (event->sensor_type != state->spec->sensor_type ||
+	    event->sensor_index != state->sensor_index)
+		return -EINVAL;
+

drivers/iio/opensensorfusion/osf_iio.c line 217

Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +10 -0
+	if (event->sample_format != OSF_SAMPLE_FORMAT_S32 ||
+	    event->channel_count != state->spec->channel_count ||
+	    event->channel_count > OSF_MAX_SAMPLE_CHANNELS)
+		return -EINVAL;
+
+	for (i = 0; i < event->channel_count; i++)
+		scan_values[i] = event->values[i];
+
+	return iio_push_to_buffers_with_timestamp(indio_dev, scan,
+						  event->host_timestamp_ns);

For new code iio_push_to_buffers_with_ts() It has a bounds check that
this version doesn't have.

Quoted 23 lines
drivers/iio/opensensorfusion/osf_iio.c new file +23 -0
+}
+
+int osf_iio_register_sensor(struct device *dev,
+			    const struct osf_capability_entry *entry,
+			    void *driver_data, struct iio_dev **indio_dev)
+{
+	const struct osf_iio_sensor_spec *spec;
+	struct osf_iio_state *state;
+	struct iio_dev *iio_dev;
+	int ret;
+
+	if (!dev || !entry)
+		return -EINVAL;
+
+	spec = osf_iio_find_sensor_spec(entry->sensor_type,
+					entry->channel_count);
+	if (!spec)
+		return -EOPNOTSUPP;
+
+	if (entry->sample_format != OSF_SAMPLE_FORMAT_S32)
+		return -EOPNOTSUPP;
+
+	iio_dev = devm_iio_device_alloc(dev, sizeof(*state));

drivers/iio/opensensorfusion/osf_iio.c line 250

Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +10 -0
+	if (!iio_dev)
+		return -ENOMEM;
+
+	state = iio_priv(iio_dev);
+	state->spec = spec;
+	state->scale_nano = entry->scale_nano;
+	state->sensor_index = entry->sensor_index;
+	state->osf = driver_data;
+
+	iio_dev->name = spec->name;

What does this end up as? Should be something close to a part number though in multifunction parts can include something about the type.

drivers/iio/opensensorfusion/osf_iio.c line 252

Quoted 2 lines
drivers/iio/opensensorfusion/osf_iio.c new file +2 -0
+	iio_dev->info = &osf_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;

Look at devm_iio_kfifo_buffer_setup() and what it does to this.

drivers/iio/opensensorfusion/osf_iio.c line 265

Quoted 13 lines
drivers/iio/opensensorfusion/osf_iio.c new file +13 -0
+	iio_dev->channels = spec->channels;
+	iio_dev->num_channels = spec->num_channels;
+
+	ret = devm_iio_kfifo_buffer_setup(dev, iio_dev, NULL);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_device_register(dev, iio_dev);
+	if (ret)
+		return ret;
+
+	if (indio_dev)
+		*indio_dev = iio_dev;

why would you call this function without?
For kernel code we don't defined against nonsensical parameters. Catch those in review rather than complicating the code.

Quoted 11 lines
drivers/iio/opensensorfusion/osf_iio.c new file +4 -0
+
+	return 0;
+}
 
+#endif
drivers/iio/opensensorfusion/osf_protocol.c new file +0 -0
new file mode 100644
index 000000000..8235d3af5

drivers/iio/opensensorfusion/osf_protocol.c line 10

Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10 -0
@@ -0,0 +1,234 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/crc32.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+#include "osf_protocol.h"
+
+#define OSF_CRC32_INIT		0xffffffffU
+#define OSF_CRC32_XOROUT	0xffffffffU

GENMASK(31, 0)

No one likes counting fs.

drivers/iio/opensensorfusion/osf_protocol.c line 21

Quoted 11 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +11 -0
+
+static u16 osf_get_le16(const u8 *buf)
+{
+	return buf[0] | buf[1] << 8;
+}
+
+static u32 osf_get_le32(const u8 *buf)
+{
+	return (u32)buf[0] | (u32)buf[1] << 8 |
+	       (u32)buf[2] << 16 | (u32)buf[3] << 24;
+}

Another copy? See comments on the one in osf_stream.c

drivers/iio/opensensorfusion/osf_protocol.c line 25

Quoted 4 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +4 -0
+
+static u64 osf_get_le64(const u8 *buf)
+{
+	return (u64)osf_get_le32(buf) | (u64)osf_get_le32(buf + 4) << 32;

Looks like
get_unaligned_le64()?

Quoted 13 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +11 -0
+}
 
 
+int osf_protocol_decode_capability_entry(const struct osf_capability_report *report,
+					 unsigned int index,
+					 struct osf_capability_entry *entry)
+{
+	const u8 *payload;
+
+	if (!report || !report->entries || !entry)
+		return -EINVAL;
+
+	if (index >= report->capability_count)

drivers/iio/opensensorfusion/osf_protocol.c line 219

Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10 -0
+		return -ERANGE;
+
+	payload = report->entries + index * OSF_CAP_SENSOR_ENTRY_LEN;
+	entry->sensor_type = osf_get_le16(payload);
+	entry->sensor_index = osf_get_le16(payload + 2);
+	entry->channel_count = osf_get_le16(payload + 4);
+	entry->sample_format = osf_get_le16(payload + 6);
+	entry->scale_nano = osf_get_le32(payload + 8);
+	entry->flags = osf_get_le32(payload + 12);
+	entry->reserved = osf_get_le32(payload + 16);

Easier to read as

	*entry = (struct osf_capability_entry) {
		.sensor_type = get_unaligned_le16(payload),
		.sensor_index = get_unaligned_le16(payload + 2),
	....

	};

Probably can apply similar in other places.

Quoted 23 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +15 -0
+
+	if (!osf_sensor_type_valid(entry->sensor_type))
+		return -EPROTO;
+
+	if (entry->sample_format != OSF_SAMPLE_FORMAT_S32)
+		return -EPROTO;
+
+	if (entry->flags & ~OSF_CAPABILITY_FLAGS_MASK)
+		return -EPROTO;
+
+	if (entry->reserved)
+		return -EPROTO;
+
+	return 0;
+}
drivers/iio/opensensorfusion/osf_serdev.c new file +0 -0
new file mode 100644
index 000000000..1827760c2

drivers/iio/opensensorfusion/osf_serdev.c line 10

Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +10 -0
@@ -0,0 +1,354 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/atomic.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>

Not seeing this used. Maybe I missed something of specific. I wouldn't expect there to be anything in a driver like this.

Quoted 30 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +28 -0
+#include <linux/serdev.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/types.h>
+#include <linux/workqueue.h>
 
 
+
+static int osf_serdev_register_iio(struct osf_serdev *osf_uart,
+				   const struct osf_capability_entry *entry)
+{
+	struct device *dev = &osf_uart->serdev->dev;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	if (osf_serdev_iio_registered(osf_uart, entry))
+		return 0;
+
+	if (!osf_iio_sensor_supported(entry->sensor_type, entry->channel_count) ||
+	    entry->sample_format != OSF_SAMPLE_FORMAT_S32) {
+		dev_dbg(dev,
+			"ignoring unsupported capability sensor=%u index=%u channels=%u format=%u\n",
+			entry->sensor_type, entry->sensor_index,
+			entry->channel_count, entry->sample_format);
+		return 0;
+	}
+
+	if (osf_uart->iio_count >= ARRAY_SIZE(osf_uart->iio)) {
+		dev_warn(dev, "IIO registration table full, ignoring sensor=%u index=%u\n",
+			 entry->sensor_type, entry->sensor_index);

drivers/iio/opensensorfusion/osf_serdev.c line 110

Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +10 -0
+		return 0;
+	}
+
+	ret = osf_iio_register_sensor(dev, entry, &osf_uart->osf, &indio_dev);
+	if (ret)
+		return ret;
+
+	osf_uart->iio[osf_uart->iio_count].sensor_type = entry->sensor_type;
+	osf_uart->iio[osf_uart->iio_count].sensor_index = entry->sensor_index;
+	osf_uart->iio[osf_uart->iio_count].indio_dev = indio_dev;
	osf_uart->iio[osf_uart->iio_count++] = (struct osf_serdev_iio) {
		.sensor_type = entry->sensor_type,
		.sensor_index = entry->sensor_index,
		.indio_dev = indio_dev,
	};
Quoted 9 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +9 -0
+	osf_uart->iio_count++;
+
+	return 1;
+}
+
+static void osf_serdev_iio_register_work(struct work_struct *work)
+{
+	struct osf_serdev *osf_uart =
+		container_of(work, struct osf_serdev, iio_register_work);

drivers/iio/opensensorfusion/osf_serdev.c line 129

Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +10 -0
+	struct device *dev = &osf_uart->serdev->dev;
+	struct osf_capability_cache cache;
+	unsigned int registered = 0;
+	unsigned int i;
+	int ret;
+
+	if (!osf_core_copy_capability_cache(&osf_uart->osf, &cache))
+		return;
+
+	mutex_lock(&osf_uart->iio_lock);

I'd use guard(mutex)(); here

drivers/iio/opensensorfusion/osf_serdev.c line 137

Quoted 8 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +8 -0
+	for (i = 0; i < cache.capability_count; i++) {
+		ret = osf_serdev_register_iio(osf_uart, &cache.entries[i]);
+		if (ret) {
+			if (ret > 0) {
+				registered++;
+				continue;
+			}
+
			Then

			if (ret < 0) {
				dev_err();
				return ret;
			}

			registered++;

drivers/iio/opensensorfusion/osf_serdev.c line 146

Quoted 9 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +9 -0
+			dev_err(dev,
+				"failed to register IIO sensor=%u index=%u: %d\n",
+				cache.entries[i].sensor_type,
+				cache.entries[i].sensor_index, ret);
+		}
+	}
+	mutex_unlock(&osf_uart->iio_lock);
+
+	if (registered)

With above, this only doesn't happen if the count was 0. Maybe check that separately so you can then do this unconditionally.

Quoted 33 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +32 -0
+		dev_info(dev,
+			 "registered %u Open Sensor Fusion IIO devices from capability report seq=%llu\n",
+			 registered, (unsigned long long)cache.sequence);
+}
 
+
+static int osf_serdev_probe(struct serdev_device *serdev)
+{
+	struct osf_serdev *osf_uart;
+	unsigned int baudrate;
+	int ret;
+
+	osf_uart = devm_kzalloc(&serdev->dev, sizeof(*osf_uart), GFP_KERNEL);
+	if (!osf_uart)
+		return -ENOMEM;
+
+	osf_uart->serdev = serdev;
+	osf_core_init_device(&osf_uart->osf);
+	osf_stream_init(&osf_uart->stream, &osf_uart->osf);
+	INIT_WORK(&osf_uart->iio_register_work, osf_serdev_iio_register_work);
+	INIT_WORK(&osf_uart->buffer_push_work, osf_serdev_buffer_push_work);
+	mutex_init(&osf_uart->iio_lock);
+	spin_lock_init(&osf_uart->sample_lock);
+	atomic64_set(&osf_uart->capability_sequence_scheduled, -1);
+	osf_core_set_sample_callback(&osf_uart->osf, osf_serdev_sample_ready,
+				     osf_uart);
+
+	serdev_device_set_drvdata(serdev, osf_uart);
+	serdev_device_set_client_ops(serdev, &osf_serdev_ops);
+
+	ret = serdev_device_open(serdev);
+	if (ret)
+		return ret;

drivers/iio/opensensorfusion/osf_serdev.c line 315

Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +10 -0
+
+	baudrate = serdev_device_set_baudrate(serdev, OSF_SERDEV_BAUD);
+	if (baudrate != OSF_SERDEV_BAUD)
+		dev_warn(&serdev->dev, "requested %u baud, controller set %u\n",
+			 OSF_SERDEV_BAUD, baudrate);
+
+	serdev_device_set_flow_control(serdev, false);
+
+	dev_info(&serdev->dev, "Open Sensor Fusion UART opened at %u baud\n",
+		 OSF_SERDEV_BAUD);

Generally we try to avoid prints like this. Is it useful to know the baud rate? It's easy to tell if the rest worked.

Quoted 15 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +14 -0
+
+	return 0;
+}
+
+static void osf_serdev_remove(struct serdev_device *serdev)
+{
+	struct osf_serdev *osf_uart = serdev_device_get_drvdata(serdev);
+
+	osf_core_set_sample_callback(&osf_uart->osf, NULL, NULL);
+	serdev_device_close(serdev);
+	cancel_work_sync(&osf_uart->iio_register_work);
+	cancel_work_sync(&osf_uart->buffer_push_work);
+	osf_stream_reset(&osf_uart->stream);
+}
 

drivers/iio/opensensorfusion/osf_serdev.c line 354

Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +10 -0
+
+int osf_serdev_register_driver(void)
+{
+	return serdev_device_driver_register(&osf_serdev_driver);
+}
+
+void osf_serdev_unregister_driver(void)
+{
+	serdev_device_driver_unregister(&osf_serdev_driver);
+}

As above, feels like the file break up is making this more complex. These should be next to module_init and allow us of the macros that generate that code for us.

Quoted 18 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +1 -0
+#endif
drivers/iio/opensensorfusion/osf_stream.c new file +11 -0
new file mode 100644
index 000000000..e9addf70e
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#include "osf_core.h"
+#include "osf_protocol.h"
+#include "osf_stream.h"
+
+#define OSF_STREAM_MAGIC_LEN	4

drivers/iio/opensensorfusion/osf_stream.c line 21

Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +10 -0
+#define OSF_STREAM_MAX_PAYLOAD_LEN				\
+	(OSF_STREAM_MAX_FRAME_LEN - OSF_FRAME_HEADER_LEN - OSF_FRAME_CRC_LEN)
+
+static const u8 osf_stream_magic[OSF_STREAM_MAGIC_LEN] = {
+	'O', 'S', 'F', '0',
+};
+
+static u16 osf_stream_get_le16(const u8 *buf)
+{
+	return buf[0] | buf[1] << 8;

We have standard functions in the kernel for this. Don't reinvent the wheel.

get_unaligned_le16()

drivers/iio/opensensorfusion/osf_stream.c line 24

Quoted 3 lines
drivers/iio/opensensorfusion/osf_stream.c new file +3 -0
+}
+
+static u32 osf_stream_get_le32(const u8 *buf)

Likewise.

drivers/iio/opensensorfusion/osf_stream.c line 43

Quoted 9 lines
drivers/iio/opensensorfusion/osf_stream.c new file +8 -0
+{
+	return (u32)buf[0] | (u32)buf[1] << 8 |
+	       (u32)buf[2] << 16 | (u32)buf[3] << 24;
+}
 
+
+static bool osf_stream_magic_match(const u8 *buf, size_t len)
+{
+	return !memcmp(buf, osf_stream_magic, len);

Just do this inline. If it was stand alone the length match would belong in here not at the caller.

drivers/iio/opensensorfusion/osf_stream.c line 52

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

In modern kernel code can do something like:

	for (size_t i = 0; i < stream->len; i++)

drivers/iio/opensensorfusion/osf_stream.c line 55

Quoted 3 lines
drivers/iio/opensensorfusion/osf_stream.c new file +3 -0
+		match_len = stream->len - i;
+		if (match_len > OSF_STREAM_MAGIC_LEN)
+			match_len = OSF_STREAM_MAGIC_LEN;
		match_len = min(match_len, OSF_STREAM_MAGIC_LEN);

drivers/iio/opensensorfusion/osf_stream.c line 59

Quoted 4 lines
drivers/iio/opensensorfusion/osf_stream.c new file +4 -0
+
+		if (osf_stream_magic_match(stream->buf + i, match_len)) {
+			if (i)
+				osf_stream_discard(stream, i);

I'd special case 0 as that's your 'good' path.

			if (i == 0)
				return 0; 

			osf_stream_discard(stream, i);
			return i;

drivers/iio/opensensorfusion/osf_stream.c line 64

Quoted 5 lines
drivers/iio/opensensorfusion/osf_stream.c new file +5 -0
+			return i;
+		}
+	}
+
+	stream->len = 0;

Whilst this is a short cut, I'd still call osf_stream_discard() as then it is obvious that this is also discarding. Failing that add a comment /* Discard the lot */

drivers/iio/opensensorfusion/osf_stream.c line 65

Quoted 1 line
drivers/iio/opensensorfusion/osf_stream.c new file +1 -0
+	return old_len;

Why is it useful to return old_len if failed to resync? That's non obvious enough I think this needs documentation on what the return value is.

I'd be tempted to return a bool for whether it succeeded and pass a pointer to put how much was discarded in. That will make the condition below simpler.

Quoted 9 lines
drivers/iio/opensensorfusion/osf_stream.c new file +9 -0
+}
+
+static int osf_stream_process(struct osf_stream *stream)
+{
+	struct osf_frame frame;
+	size_t decoded_len;
+	size_t discarded;
+	size_t frame_len;
+	u32 payload_len;

drivers/iio/opensensorfusion/osf_stream.c line 84

Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +10 -0
+	int first_err = 0;
+	int ret;
+
+	while (stream->len) {
+		discarded = osf_stream_resync(stream);
+		if (discarded) {
+			stream->stats.bad_magic_resyncs++;
+			stream->stats.dropped_bytes += discarded;
+			if (!first_err)
+				first_err = -EPROTO;

So if we resynced but discarded a few

drivers/iio/opensensorfusion/osf_stream.c line 87

Quoted 3 lines
drivers/iio/opensensorfusion/osf_stream.c new file +3 -0
+		}
+
+		if (!stream->len)

This took a bit of figuring out. It's the resync failed path. I'd rather see an explicit return bool for that. The discarded variable can be parameter as mentioned above.

drivers/iio/opensensorfusion/osf_stream.c line 95

Quoted 8 lines
drivers/iio/opensensorfusion/osf_stream.c new file +8 -0
+			break;
+
+		if (stream->len < OSF_FRAME_HEADER_LEN) {
+			stream->stats.partial_frames++;
+			break;
+		}
+
+		if (osf_stream_get_le16(stream->buf + 6) !=
		if (get_unaligned_le16(stream->buf + 6) != OSF_FRAME_HEADER_LEN) {

Slightly long lines are fine in IIO as long as they aren't too much past 80 and improve readability.

drivers/iio/opensensorfusion/osf_stream.c line 98

Quoted 3 lines
drivers/iio/opensensorfusion/osf_stream.c new file +3 -0
+		    OSF_FRAME_HEADER_LEN) {
+			stream->stats.dropped_bytes++;
+			osf_stream_discard(stream, 1);

Needs a comment I think. Not obvious to me at all why you'd drop 1 byte. Is the idea it forces a resync? Might as well drop the magic string in that case. I'd add a helper for this so you can do something like..

osf_force_resync(stream); in all these cases or something easier to follow than a 1 byte drop.

drivers/iio/opensensorfusion/osf_stream.c line 104

Quoted 6 lines
drivers/iio/opensensorfusion/osf_stream.c new file +6 -0
+			if (!first_err)
+				first_err = -EPROTO;
+			continue;
+		}
+
+		payload_len = osf_stream_get_le32(stream->buf + 10);

get_unaligned_le32()

Quoted 108 lines
drivers/iio/opensensorfusion/osf_stream.c new file +108 -0
+		if (payload_len > OSF_STREAM_MAX_PAYLOAD_LEN) {
+			stream->stats.dropped_bytes++;
+			osf_stream_discard(stream, 1);
+			if (!first_err)
+				first_err = -EMSGSIZE;
+			continue;
+		}
+
+		frame_len = OSF_FRAME_HEADER_LEN + payload_len + OSF_FRAME_CRC_LEN;
+		if (stream->len < frame_len) {
+			stream->stats.partial_frames++;
+			break;
+		}
+
+		ret = osf_protocol_decode_frame(stream->buf, frame_len, &frame,
+						&decoded_len);
+		if (ret) {
+			if (ret == -EBADMSG)
+				stream->stats.bad_crc_frames++;
+			stream->stats.dropped_bytes++;
+			osf_stream_discard(stream, 1);
+			if (!first_err)
+				first_err = ret;
+			continue;
+		}
+
+		if (decoded_len != frame_len) {
+			stream->stats.dropped_bytes++;
+			osf_stream_discard(stream, 1);
+			if (!first_err)
+				first_err = -EMSGSIZE;
+			continue;
+		}
+
+		ret = osf_core_receive_frame(stream->osf, stream->buf, frame_len);
+		if (ret) {
+			osf_stream_discard(stream, frame_len);
+			if (!first_err)
+				first_err = ret;
+			continue;
+		}
+
+		stream->stats.valid_frames++;
+		osf_stream_discard(stream, frame_len);
+	}
+
+	return first_err;
+}
+
+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) {
+		stream->len = 0;
+		memset(&stream->stats, 0, sizeof(stream->stats));
+	}
+}
+
+int osf_stream_receive_bytes(struct osf_stream *stream, const u8 *buf,
+			     size_t len)
+{
+	size_t copy_len;
+	size_t space;
+	int first_err = 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;
+	}
+
+	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;
+		}
+
+		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;
+	}
+
+	return first_err;
+}
#6 lore raw

RFC PATCH 3/3 MAINTAINERS: add Open Sensor Fusion IIO driver entry

In reply to Jinseob Kim

Add a maintainer entry for the Open Sensor Fusion IIO driver and binding.

The driver path reflects the current RFC layout. It may need to be adjusted after review if the driver moves under a different IIO directory.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index c2c6d7927..001ba4ea6 100644
@@ -20011,6 +20011,13 @@ F:	Documentation/devicetree/
 F:	arch/*/boot/dts/
 F:	include/dt-bindings/
 
+OPEN SENSOR FUSION IIO DRIVER
+M:	Jinseob Kim <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml
+F:	drivers/iio/opensensorfusion/
+
 OPENCOMPUTE PTP CLOCK DRIVER
 M:	Vadim Fedorenko <[email protected]>
 L:	[email protected]
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH RFC 3/3] MAINTAINERS: add Open Sensor Fusion IIO driver entry

Quoted 16 lines · Jinseob Kim

Add a maintainer entry for the Open Sensor Fusion IIO driver and binding.

The driver path reflects the current RFC layout. It may need to be adjusted after review if the driver moves under a different IIO directory.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +0 -0
index c2c6d7927..001ba4ea6 100644

MAINTAINERS F: Documentation/devicetree/ · line 20018

Quoted 8 lines
MAINTAINERS +5 -0
@@ -20011,6 +20011,13 @@ F:     Documentation/devicetree/
 F:     arch/*/boot/dts/
 F:     include/dt-bindings/
 
+OPEN SENSOR FUSION IIO DRIVER
+M:     Jinseob Kim <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml

All of the above should be added in your dt-bindings patch. If you're adding a new file,
it's expected for you to also add a new entry in MAINTAINERS in the same patch, otherwise it creates confusion and an unnecessary additional patch that the maintainer could overlook (highly unlikely, but could happen).

MAINTAINERS F: Documentation/devicetree/ · line 20019

Quoted 1 line
MAINTAINERS +1 -0
+F:     drivers/iio/opensensorfusion/

And this line would be added in the actual driver patch.

Signature · 3 lines
Kind regards

CJD

A message that is not in the archive (sent off-list or not yet received): CALMSewJinjbnHT_sOgWmHVeThv3su_E6fioyAjEWKzf7uROc2Q@mail.gmail.com

author
#9 lore raw

Re: [PATCH RFC 3/3] MAINTAINERS: add Open Sensor Fusion IIO driver entry

Sorry, resending in plain text.

Thanks, that makes sense.

I will fold the binding-related MAINTAINERS entry into the dt-bindings patch

and add the driver path entry in the driver patch in the next revision.

Thanks again

Jinseob

2026년 5월 20일 (수) 오후 5:07, j k <[email protected]>님이 작성:

Quoted 52 lines

Thanks, that makes sense.

I will fold the binding-related MAINTAINERS entry into the dt-bindings patch and add the driver path entry in the driver patch in the next revision.

Thanks again

Jinseob

2026년 5월 20일 (수) 오후 4:59, Joshua Crofts <[email protected]>님이 작성:

Quoted 41 lines · Jinseob Kim
Quoted 16 lines

Add a maintainer entry for the Open Sensor Fusion IIO driver and binding.

The driver path reflects the current RFC layout. It may need to be adjusted after review if the driver moves under a different IIO directory.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +0 -0
index c2c6d7927..001ba4ea6 100644

MAINTAINERS F: Documentation/devicetree/ · line 20018

Quoted 8 lines
MAINTAINERS +5 -0
@@ -20011,6 +20011,13 @@ F:     Documentation/devicetree/
 F:     arch/*/boot/dts/
 F:     include/dt-bindings/
 
+OPEN SENSOR FUSION IIO DRIVER
+M:     Jinseob Kim <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     Documentation/devicetree/bindings/iio/imu/opensensorfusion,osf-uart.yaml

All of the above should be added in your dt-bindings patch. If you're adding a new file,
it's expected for you to also add a new entry in MAINTAINERS in the same patch, otherwise it creates confusion and an unnecessary additional patch that the maintainer could overlook (highly unlikely, but could happen).

MAINTAINERS F: Documentation/devicetree/ · line 20019

Quoted 1 line
MAINTAINERS +1 -0
+F:     drivers/iio/opensensorfusion/

And this line would be added in the actual driver patch.

--
Kind regards

CJD

#10 lore raw

Re: [PATCH RFC 3/3] MAINTAINERS: add Open Sensor Fusion IIO driver entry

Quoted 5 lines · j k

Sorry, resending in plain text.

Thanks, that makes sense.

Quoted 7 lines

I will fold the binding-related MAINTAINERS entry into the dt-bindings patch

and add the driver path entry in the driver patch in the next revision.

Thanks again

Jinseob

No worries, also please don't top post, just inline your comments like my previous reply. Check the mailing list on lore.kernel.org to see how to reply to messages etc.

Signature · 3 lines
Kind regards

CJD
maintainer
#11 lore raw

Re: [PATCH RFC 3/3] MAINTAINERS: add Open Sensor Fusion IIO driver entry

Quoted 10 lines · On Wed, 20 May 2026 10:22:05 +0200 Joshua Crofts <[email protected]>…
Quoted 9 lines · j k

Sorry, resending in plain text.

Thanks, that makes sense.

I will fold the binding-related MAINTAINERS entry into the dt-bindings patch

and add the driver path entry in the driver patch in the next revision.

Quoted 8 lines
Quoted 3 lines

Thanks again

Jinseob

No worries, also please don't top post, just inline your comments like my previous reply. Check the mailing list on lore.kernel.org to see how to reply to messages etc.

Plus for future reference - if you agree with feedback don't reply at all.

Thanks etc all goes in the change log of v2.

Saves a few seconds for a bunch of reviewers who have far too many emails to read :)

Jonathan

#12 lore raw
In reply to Jinseob Kim
Quoted 36 lines · Jinseob Kim

Open Sensor Fusion is a UART-attached sensor aggregation device. The current prototype sends OSF protocol v0 frames over a host UART link. This RFC adds the first Linux IIO receive path for that UART stream.

The first transport is serdev UART. USB, real sensor reads, fusion output, and production timestamp correlation are intentionally left out of this series.

The driver code is kept in one patch for the first RFC to avoid intermediate non-buildable states. The internal files are still split by protocol, stream, core, transport, and IIO code.

Current validation coverage:

- STM32F405 test firmware OSF protocol v0 UART stream at 115200 8N1.
- Raspberry Pi 4 serdev probe and receive path to osf_core_receive_frame().
- CAPABILITY_REPORT-driven IIO device registration.
- IIO read_raw for accel, gyro, magn, and temp samples.
- IIO software kfifo buffer userspace read.
- scan decode helper for buffered samples.
- dt_binding_check pass for the binding.
- checkpatch.pl --strict with ERROR 0, WARNING 0, CHECK 0.
- W=1 source compile/link phase clean in the staging kernel tree.
- modpost unresolved symbol warnings came from the staging tree missing Module.symvers.

Known limits:

- The sample source is synthetic stream data, not real ICM-42688-P or MMC5983MA sensor reads.
- The IIO timestamp is Linux host receive time. Device time correlation is still open.
- Runtime capability removal is not implemented.
- The staging layout uses drivers/iio/opensensorfusion/. The final directory is open for review.

Quoted 5 lines

Review feedback wanted on the IIO device layout, timestamp policy, binding shape, and driver directory.

 16 files changed, 1706 insertions(+)

No.
Split it to at least 5 patches (as far as I can briefly see it's achievable): base + feature1 + feature2 + …

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#13 lore raw
In reply to Jinseob Kim
Quoted 10 lines · On Wed, 20 May 2026 16:28:40 +0900 Jinseob Kim <[email protected]> wrote:

Open Sensor Fusion is a UART-attached sensor aggregation device. The current prototype sends OSF protocol v0 frames over a host UART link. This RFC adds the first Linux IIO receive path for that UART stream.

The first transport is serdev UART. USB, real sensor reads, fusion output, and production timestamp correlation are intentionally left out of this series.

The driver code is kept in one patch for the first RFC to avoid intermediate non-buildable states. The internal files are still split by protocol, stream, core, transport, and IIO code.

Even for an RFC you need to break it up. If you do it right there won't be non-buildable states. Take a look at how any complex IIO driver is added.

Key thing here seems to be that we need some references.

Superficially sounds like an open source sensor hub implementation but we need a datasheet / reference manual to be able to review this properly.

I'll have a first look but it's likely to be somewhat superficial.