iio: add Open Sensor Fusion UART support
Subsystems (3)
v11 cover iio: add Open Sensor Fusion UART support
This series adds a UART/serdev receiver for the project's fixed OSF Device-to-Host 0.0 protocol. Complete capability snapshots create IIO accelerometer, gyroscope, magnetometer and temperature devices with RAW, descriptor-derived SCALE and initialized buffered scans.
The receiver validates the fixed OSF0 envelope, CRC and exact known message lengths. Equivalent capability reports are idempotent; changes to a bound supported inventory fault the session until explicit rebind. IIO samples use host timestamps.
Changes since v10:
- Move OSF under drivers/iio/sensorhubs/osf/ following review.
- Use the OSF GREEN compatible with the generic OSF fallback.
- Clarify complete capability-report snapshot semantics.
- Use designated decoded-frame initialization and drop decoded reserved fields while retaining reserved wire-byte tolerance.
- Reuse stream reset and simplify core/serdev locking and lifecycle.
- Use IIO buffer-mode claims instead of a private buffer lock.
- Simplify scan storage and channel declarations.
- Fix KUnit worker/device/mutex/resource/completion lifetimes. Generic IIO self-test extraction remains deferred.
- Synchronize KUnit fault observation and configuration completion with completions and thread joins instead of short polling/deadline windows.
The IIO buffer teardown serialization issue found during the v10 review was fixed separately and is now in mainline as commit 84bb0fc46ccf ("iio: buffer: serialize buffer teardown with mode claims").
This series is based on the IIO testing snapshot fetched for this submission:a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
Runtime validation used this testing base plus the eight OSF patches and the exact mainline IIO core fix 84bb0fc46ccf6a545b8fdf57cda83770186ed8dd. The core fix is not included in this series and must be present when running the series on this testing base.
Fresh validation of this source:
- GCC and Clang W=1 kernel builds; module builds.
- osf-core (16 tests) and osf-iio (2 tests), with KASAN, lockdep, DEBUG_MUTEXES, DEBUG_LOCK_ALLOC, DEBUG_OBJECTS and DEBUG_OBJECTS_FREE.
- 200 production module load/unload cycles and 200 test-enriched module load/unload cycles using identical driver/test C sources, exercising 20,000 IIO buffer toggles, scan subsets, timestamps and unregister.
- DT binding/style/example/schema checks and IIO Sphinx build.
- Clean sequential application and relevant builds of all eight patches. Physical board tests were not repeated for this revision.
Canonical specification (fixed revision):
https://github.com/opensensorfusion/opensensorfusion-protocol/blob/ca9cdea1ae550c2b4d6f29f87877adae99470744/spec/osf-d2h-0.0.md
The fixed public specification is maintained and adopted by the OSF project. This does not imply Linux maintainer approval of the specification or driver.
Jinseob Kim (8):
dt-bindings: iio: add Open Sensor Fusion device
Documentation: iio: add Open Sensor Fusion driver overview
iio: osf: add protocol decoding
iio: osf: add validated stream parser
iio: osf: add UART transport and core receive path
iio: osf: add IIO devices from capability reports
iio: osf: add core KUnit tests
iio: osf: add IIO KUnit tests
.../bindings/iio/opensensorfusion,osf.yaml | 55 + .../devicetree/bindings/vendor-prefixes.yaml | 2 + Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 81 ++ MAINTAINERS | 8 + drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/sensorhubs/Kconfig | 7 + drivers/iio/sensorhubs/Makefile | 3 + drivers/iio/sensorhubs/osf/Kconfig | 27 + drivers/iio/sensorhubs/osf/Makefile | 7 + drivers/iio/sensorhubs/osf/osf_core.c | 460 +++++++ drivers/iio/sensorhubs/osf/osf_core.h | 75 ++ drivers/iio/sensorhubs/osf/osf_core_test.c | 1056 +++++++++++++++++ drivers/iio/sensorhubs/osf/osf_iio.c | 288 +++++ drivers/iio/sensorhubs/osf/osf_iio.h | 22 + drivers/iio/sensorhubs/osf/osf_iio_test.c | 386 ++++++ drivers/iio/sensorhubs/osf/osf_protocol.c | 215 ++++ drivers/iio/sensorhubs/osf/osf_protocol.h | 99 ++ drivers/iio/sensorhubs/osf/osf_serdev.c | 151 +++ drivers/iio/sensorhubs/osf/osf_stream.c | 230 ++++ drivers/iio/sensorhubs/osf/osf_stream.h | 53 + 22 files changed, 3228 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml create mode 100644 Documentation/iio/open-sensor-fusion.rst create mode 100644 drivers/iio/sensorhubs/Kconfig create mode 100644 drivers/iio/sensorhubs/Makefile create mode 100644 drivers/iio/sensorhubs/osf/Kconfig create mode 100644 drivers/iio/sensorhubs/osf/Makefile create mode 100644 drivers/iio/sensorhubs/osf/osf_core.c create mode 100644 drivers/iio/sensorhubs/osf/osf_core.h create mode 100644 drivers/iio/sensorhubs/osf/osf_core_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.h create mode 100644 drivers/iio/sensorhubs/osf/osf_iio_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.h create mode 100644 drivers/iio/sensorhubs/osf/osf_serdev.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.h
base-commit: a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
PATCH v11 1/8 dt-bindings: iio: add Open Sensor Fusion device
Describe OSF GREEN as the current hardware model with a generic OSF host-interface fallback. OSF hardware variants may use different sensor configurations. A model-specific compatible allows future power sequencing, errata and quirks without changing the common fallback.
The current Linux driver needs no GREEN-specific handling and matches the generic compatible. Sensor inventory and protocol version are discovered from runtime wire frames rather than encoded in the compatible strings.
--- .../bindings/iio/opensensorfusion,osf.yaml | 55 +++++++++++++++++++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 6 ++ 3 files changed, 63 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +55 -0
@@ -0,0 +1,55 @@ +# 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 Open Sensor Fusion GREEN sensor hub and its + generic 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: + items: + - const: opensensorfusion,osf-green + - 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-green", + "opensensorfusion,osf"; + vcc-supply = <&vcc_sensor>; + }; + }; +...
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -1267,6 +1267,8 @@ patternProperties: description: OpenPandora GmbH "^openrisc,.*": description: OpenRISC.io + "^opensensorfusion,.*": + description: Open Sensor Fusion "^openwrt,.*": description: OpenWrt "^option,.*":
MAINTAINERS +6 -0
@@ -20537,6 +20537,12 @@ F: Documentation/process/maintainer-devicetree.rst 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]
Re: [PATCH v11 1/8] dt-bindings: iio: add Open Sensor Fusion device
pw-bot: not-applicable
PATCH v11 2/8 Documentation: iio: add Open Sensor Fusion driver overview
In reply to Jinseob KimDocument the supported OSF receiver profile, runtime sensor discovery, IIO channel units, scale validation and session lifetime.
Link the project-maintained fixed wire specification instead of duplicating its layouts and compatibility policy. Explain host IIO timestamps and the explicit rebind needed after a changed inventory.
--- Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 81 ++++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 83 insertions(+) create mode 100644 Documentation/iio/open-sensor-fusion.rst
Documentation/iio/index.rst +1 -0
@@ -42,4 +42,5 @@ Industrial I/O Kernel Drivers adxl345 bno055 ep93xx_adc + open-sensor-fusion opt4060
Documentation/iio/open-sensor-fusion.rst new file +81 -0
81 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -20541,6 +20541,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
PATCH v11 3/8 iio: osf: add protocol decoding
In reply to Jinseob KimAdd helpers for decoding Open Sensor Fusion frame headers and supported message payloads.
Validate the fixed OSF0 envelope, payload bounds and CRC before exposing decoded frame contents. Require exact known payload lengths and decode capability entries structurally so the core can apply support policy. Tolerate reserved padding as required by the fixed protocol contract.
Use explicit little-endian wire storage sizes and designated initializers for decoded output structures.
--- MAINTAINERS | 1 + drivers/iio/sensorhubs/osf/osf_protocol.c | 215 ++++++++++++++++++++++ drivers/iio/sensorhubs/osf/osf_protocol.h | 99 ++++++++++ 3 files changed, 315 insertions(+) create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.h
MAINTAINERS +1 -0
@@ -20542,6 +20542,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/sensorhubs/osf/ K: opensensorfusion OPENCOMPUTE PTP CLOCK DRIVER
drivers/iio/sensorhubs/osf/osf_protocol.c new file +215 -0
215 lines not on this page · raw message
drivers/iio/sensorhubs/osf/osf_protocol.h new file +99 -0
99 lines not on this page · raw message
PATCH v11 4/8 iio: osf: add validated stream parser
In reply to Jinseob KimAdd 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 a registered frame callback.
Candidates rejected before validation drop only the current head byte before resynchronizing, so a corrupted unvalidated payload length cannot make the parser skip later valid frames. CRC-valid validated frames are consumed in full and classified as handled, ignored, or rejected.
Use a direct callback member with an opaque context and keep explicit statistics for validated outcomes and framing failures.
--- drivers/iio/sensorhubs/osf/osf_stream.c | 230 ++++++++++++++++++++++++ drivers/iio/sensorhubs/osf/osf_stream.h | 53 ++++++ 2 files changed, 283 insertions(+) create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.h
drivers/iio/sensorhubs/osf/osf_stream.c new file +230 -0
@@ -0,0 +1,230 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/errno.h> +#include <linux/minmax.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/unaligned.h> + +#include "osf_protocol.h" +#include "osf_stream.h" + +#define OSF_STREAM_MAGIC_LEN sizeof(__le32) +#define OSF_STREAM_MAX_PAYLOAD_LEN \ + (OSF_STREAM_MAX_FRAME_LEN - OSF_FRAME_HEADER_LEN - OSF_FRAME_CRC_LEN) + +static void osf_stream_discard (struct osf_stream *stream,size_t count) +{ +if (count >= stream->len) { + stream->len =0 ; +return ; + } + +memmove (stream->buf, stream->buf + count, stream->len - count); + stream->len -= count; +} + +static void osf_stream_drop_invalid_head (struct osf_stream *stream) +{ +osf_stream_discard (stream,1 ); +} + +static bool osf_stream_magic_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; +} + +static size_t osf_stream_discard_to_magic (struct osf_stream *stream) +{ +size_t old_len = stream->len; +size_t keep_len; + +for (size_t i =0 ; i +OSF_STREAM_MAGIC_LEN <= stream->len; i++) { +if (get_unaligned_le32 (stream->buf + i) ==OSF_FRAME_MAGIC ) { +if (i) +osf_stream_discard (stream, i); +return i; + } + } + +/* +* Keep a final 1-3 byte OSF_FRAME_MAGIC prefix so a magic split +* across receive_buf() calls can be completed by the next chunk. +*/ + keep_len =min (stream->len,OSF_STREAM_MAGIC_LEN -1 ); +while (keep_len) { +size_t offset = stream->len - keep_len; + +if (osf_stream_magic_prefix_match (stream->buf + offset, keep_len)) { +if (offset) +osf_stream_discard (stream, offset); +return offset; + } + keep_len--; + } + + stream->len =0 ; +return old_len; +} + +static int osf_stream_process (struct osf_stream *stream) +{ +size_t discarded; +size_t frame_len; +u32 payload_len; +int frame_result; +int first_err =0 ; + +while (stream->len) { + discarded =osf_stream_discard_to_magic (stream); +if (discarded) { + stream->stats.bad_magic_resyncs++; + stream->stats.dropped_bytes += discarded; +if (!first_err) + first_err = -EPROTO ; + } + +if (!stream->len) +break ; + +if (stream->len <OSF_FRAME_HEADER_LEN ) +break ; + +if (get_unaligned_le16 (stream->buf +6 ) !=OSF_FRAME_HEADER_LEN ) { + stream->stats.dropped_bytes++; +osf_stream_drop_invalid_head (stream); +if (!first_err) + first_err = -EPROTO ; +continue ; + } + + payload_len =get_unaligned_le32 (stream->buf +10 ); +if (payload_len >OSF_STREAM_MAX_PAYLOAD_LEN ) { + stream->stats.dropped_bytes++; +osf_stream_drop_invalid_head (stream); +if (!first_err) + first_err = -EMSGSIZE ; +continue ; + } + + frame_len =OSF_FRAME_HEADER_LEN + payload_len +OSF_FRAME_CRC_LEN ; +if (stream->len < frame_len) +break ; + + frame_result = stream->receive_frame (stream->frame_context, + stream->buf, frame_len); +if (frame_result <0 ) { +if (frame_result == -EBADMSG ) + stream->stats.bad_crc_frames++; + +/* +* Decoding failed before the frame was validated; +* payload_len is still untrusted. Drop only the current +* head and resynchronize. +*/ + stream->stats.dropped_bytes++; +osf_stream_drop_invalid_head (stream); +if (!first_err) + first_err = frame_result; +continue ; + } + +/* Count exactly one outcome for every validated frame. */ + stream->stats.validated_frames++; +switch (frame_result) { +case OSF_STREAM_FRAME_HANDLED : + stream->stats.handled_frames++; +break ; +case OSF_STREAM_FRAME_IGNORED : + stream->stats.ignored_frames++; +break ; +case OSF_STREAM_FRAME_REJECTED : + stream->stats.rejected_frames++; +break ; +default : +/* +* Preserve the validated boundary without scanning the +* payload for another magic value. +*/ + stream->stats.rejected_frames++; +if (!first_err) + first_err = -EPROTO ; +break ; + } +osf_stream_discard (stream, frame_len); + } + +return first_err; +} + +void osf_stream_reset (struct osf_stream *stream) +{ +if (!stream) +return ; + + stream->len =0 ; +memset (&stream->stats,0 ,sizeof (stream->stats)); +} + +void osf_stream_init (struct osf_stream *stream, +int (*receive_frame)(void *context,const u8 *buf, +size_t len), +void *frame_context) +{ +if (!stream) +return ; + + stream->receive_frame = receive_frame; + stream->frame_context = frame_context; +osf_stream_reset (stream); +} + +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->receive_frame || (!buf && len)) +return -EINVAL ; + +if (!len) +return osf_stream_process (stream); + +/* +* Continue processing this receive_buf() chunk after recoverable +* framing errors so later valid frames do not wait for another callback. +* first_err retains the first diagnostic return, while the serdev +* callback reports the full byte count consumed. Every validated +* callback result is consumed in full by osf_stream_process(). +*/ +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 =min (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; +}
drivers/iio/sensorhubs/osf/osf_stream.h new file +53 -0
53 lines not on this page · raw message
PATCH v11 5/8 iio: osf: add UART transport and core receive path
In reply to Jinseob KimConnect the validated stream parser to a serdev receiver and cache device status. Configure the UART before enabling the supply, and close and drain the receive producer during managed teardown.
--- drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/sensorhubs/Kconfig | 7 ++ drivers/iio/sensorhubs/Makefile | 3 + drivers/iio/sensorhubs/osf/Kconfig | 12 ++ drivers/iio/sensorhubs/osf/Makefile | 5 + drivers/iio/sensorhubs/osf/osf_core.c | 96 ++++++++++++++++ drivers/iio/sensorhubs/osf/osf_core.h | 29 +++++ drivers/iio/sensorhubs/osf/osf_serdev.c | 146 ++++++++++++++++++++++++ 9 files changed, 300 insertions(+) create mode 100644 drivers/iio/sensorhubs/Kconfig create mode 100644 drivers/iio/sensorhubs/Makefile create mode 100644 drivers/iio/sensorhubs/osf/Kconfig create mode 100644 drivers/iio/sensorhubs/osf/Makefile create mode 100644 drivers/iio/sensorhubs/osf/osf_core.c create mode 100644 drivers/iio/sensorhubs/osf/osf_core.h create mode 100644 drivers/iio/sensorhubs/osf/osf_serdev.c
drivers/iio/Kconfig +1 -0
@@ -112,6 +112,7 @@ source "drivers/iio/potentiostat/Kconfig" source "drivers/iio/pressure/Kconfig" source "drivers/iio/proximity/Kconfig" source "drivers/iio/resolver/Kconfig" +source "drivers/iio/sensorhubs/Kconfig" source "drivers/iio/temperature/Kconfig" endif # IIO
drivers/iio/Makefile +1 -0
@@ -44,6 +44,7 @@ obj-y += potentiostat/ obj-y += pressure/ obj-y += proximity/ obj-y += resolver/ +obj-y += sensorhubs/ obj-y += temperature/ obj-y += test/ obj-y += trigger/
drivers/iio/sensorhubs/Kconfig new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only + +menu "Sensor hubs" + +source "drivers/iio/sensorhubs/osf/Kconfig" + +endmenu
drivers/iio/sensorhubs/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-y += osf/
drivers/iio/sensorhubs/osf/Kconfig new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config OPEN_SENSOR_FUSION + tristate "Open Sensor Fusion UART receiver" + depends on SERIAL_DEV_BUS + select CRC32 + help + Build the Open Sensor Fusion UART receive path. + + The driver receives and validates OSF protocol frames over a serdev + UART and caches device status reported by the sensor hub. + Message types without an application handler are ignored.
drivers/iio/sensorhubs/osf/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_OPEN_SENSOR_FUSION) += open-sensor-fusion.o + +open-sensor-fusion-y := osf_core.o osf_protocol.o osf_serdev.o osf_stream.o
drivers/iio/sensorhubs/osf/osf_core.c new file +96 -0
96 lines not on this page · raw message
drivers/iio/sensorhubs/osf/osf_core.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _OSF_CORE_H +#define _OSF_CORE_H + +#include <linux/types.h> + +#include "osf_protocol.h" + +struct device ; + +struct osf_status_cache { +u32 uptime_s; +u32 status_flags; +u32 error_flags; +u32 dropped_frames; +u64 sequence; +bool valid; +}; + +struct osf_device { +struct device *dev; +struct osf_status_cache status_cache; +u64 last_sequence; +}; + +void osf_core_init (struct osf_device *osf,struct device *dev); +int osf_core_receive_frame (struct osf_device *osf,const u8 *buf,size_t len); + +#endif
drivers/iio/sensorhubs/osf/osf_serdev.c new file +146 -0
146 lines not on this page · raw message
PATCH v11 6/8 iio: osf: add IIO devices from capability reports
In reply to Jinseob KimRegister IIO devices from supported capability descriptors and expose RAW, descriptor-derived SCALE and initialized active-scan buffers. Validate complete inventories and sample meaning, and fault incompatible bound sessions.
Hold an IIO buffer-mode claim while packing and publishing samples so that the scan layout and buffer lifetime remain stable through reconfiguration and unregister.
--- drivers/iio/sensorhubs/osf/Kconfig | 13 +- drivers/iio/sensorhubs/osf/Makefile | 3 +- drivers/iio/sensorhubs/osf/osf_core.c | 364 ++++++++++++++++++++++++ drivers/iio/sensorhubs/osf/osf_core.h | 46 +++ drivers/iio/sensorhubs/osf/osf_iio.c | 288 +++++++++++++++++++ drivers/iio/sensorhubs/osf/osf_iio.h | 22 ++ drivers/iio/sensorhubs/osf/osf_serdev.c | 7 +- 7 files changed, 736 insertions(+), 7 deletions(-) create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.h
drivers/iio/sensorhubs/osf/Kconfig +8 -5
@@ -1,12 +1,15 @@ # SPDX-License-Identifier: GPL-2.0-only config OPEN_SENSOR_FUSION - tristate "Open Sensor Fusion UART receiver" + 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 receive path. + Build the Open Sensor Fusion UART IIO driver. - The driver receives and validates OSF protocol frames over a serdev - UART and caches device status reported by the sensor hub. - Message types without an application handler are ignored. + 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/sensorhubs/osf/Makefile +2 -1
@@ -2,4 +2,5 @@ obj-$(CONFIG_OPEN_SENSOR_FUSION) += open-sensor-fusion.o -open-sensor-fusion-y := osf_core.o osf_protocol.o osf_serdev.o osf_stream.o +open-sensor-fusion-y := osf_core.o osf_iio.o osf_protocol.o osf_serdev.o \ + osf_stream.o
drivers/iio/sensorhubs/osf/osf_core.c +364 -0
398 lines not on this page · raw message
drivers/iio/sensorhubs/osf/osf_core.h +46 -0
70 lines not on this page · raw message
drivers/iio/sensorhubs/osf/osf_iio.c new file +288 -0
288 lines not on this page · raw message
drivers/iio/sensorhubs/osf/osf_iio.h new file +22 -0
@@ -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/sensorhubs/osf/osf_serdev.c +6 -1
@@ -68,9 +68,10 @@ static void osf_serdev_release(void *data) {struct osf_serdev *osf_uart = data; -/* The TTY controller drains RX work on close; hold no RX lock. */ +/* The TTY controller drains RX work on close; hold no RX or IIO lock. */ serdev_device_close (osf_uart->serdev);osf_stream_reset (&osf_uart->stream); +osf_core_unregister_iio (&osf_uart->osf);if (osf_uart->vcc_enabled)regulator_disable (osf_uart->vcc); }
@@ -124,6 +125,10 @@ static int osf_serdev_probe(struct serdev_device *serdev)return dev_err_probe (dev, ret,"failed to enable vcc regulator\n" ); osf_uart->vcc_enabled = true; +/* No fallible probe steps remain when IIO children become visible. */ + scoped_guard(mutex, &osf_uart->lock) +osf_core_start (&osf_uart->osf); +return 0 ; }
[email protected]
AI reviewRe: [PATCH v11 6/8] iio: osf: add IIO devices from capability reports
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] iio: osf: dropping valid frames and breaking sysfs cache on full kfifo --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] iio: osf: dropping valid frames and breaking sysfs cache on full kfifo --
commit 615eea31301059211a3c98d8229f156971694125
Author: Jinseob Kim <[email protected]>
iio: osf: add IIO devices from capability reports
This commit registers IIO devices from supported capability descriptors and exposes RAW, descriptor-derived SCALE, and initialized active-scan buffers. It validates complete inventories and sample meaning, and faults incompatible bound sessions.
Quoted 4 lines
drivers/iio/sensorhubs/osf/osf_core.c +0
-0
[ ... ]
Quoted 4 lines
drivers/iio/sensorhubs/osf/osf_core.c +4
-0+/* Serialize publication and cache admission with the session fault. */ +guard (mutex)(&osf->latest_lock); +if (osf->session_fault) +return -EPROTO ;
drivers/iio/sensorhubs/osf/osf_core.c void osf_core_init(struct osf_device *osf, struct device *dev) · line 241
Quoted 10 lines
drivers/iio/sensorhubs/osf/osf_core.c +10
-0+ + ret =osf_iio_push_sample (indio_dev, values, sample.channel_count); +if (ret) { +dev_err_ratelimited (osf->dev, +"failed to push sensor %#x:%u sample: %d\n" , + sample.sensor_type, sample.sensor_index, ret); +return ret; + } + + latest = osf_core_find_latest_sample(osf, sample.sensor_type,
[Severity: High]
If userspace falls behind reading from the IIO buffer and the kfifo fills up, osf_iio_push_sample() will return an error (e.g. -EBUSY from the IIO core).
Does returning early here mean we drop the frame completely and skip updating the latest_samples cache?
Since a full software ring buffer is a normal timing condition and not a malformed hardware packet, should this code perhaps ignore the buffer push error and proceed to update the sysfs cache so direct reads continue to work?
Quoted 2 lines
drivers/iio/sensorhubs/osf/osf_core.c +2
-0+ sample.sensor_index); +if (!latest) {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v11 7/8 iio: osf: add core KUnit tests
In reply to Jinseob KimExercise capability discovery, complete repeated inventories, sample-cache admission, exact lengths, reserved wire bytes and session faults. Protect producer lifetime with deferred cleanup and close the session mutex lifecycle before reinitialization.
--- drivers/iio/sensorhubs/osf/Kconfig | 12 + drivers/iio/sensorhubs/osf/Makefile | 1 + drivers/iio/sensorhubs/osf/osf_core_test.c | 1056 ++++++++++++++++++++ 3 files changed, 1069 insertions(+) create mode 100644 drivers/iio/sensorhubs/osf/osf_core_test.c
drivers/iio/sensorhubs/osf/Kconfig +12 -0
@@ -13,3 +13,15 @@ config OPEN_SENSOR_FUSION registers IIO devices for supported capability entries. It exposes accelerometer, gyroscope, magnetometer, and temperature samples through IIO direct reads and software buffers. + +config OPEN_SENSOR_FUSION_KUNIT_TEST + bool "KUnit tests for Open Sensor Fusion" if !KUNIT_ALL_TESTS + depends on OPEN_SENSOR_FUSION=y && KUNIT=y + default KUNIT_ALL_TESTS + help + Build focused unit tests for the Open Sensor Fusion core sample + acceptance, direct-read cache, and capability/session handling. The tests + exercise accepted, ignored, and rejected sample handling without + exposing additional production interfaces. + + If unsure, say N.
drivers/iio/sensorhubs/osf/Makefile +1 -0
@@ -4,3 +4,4 @@ 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 +open-sensor-fusion-$(CONFIG_OPEN_SENSOR_FUSION_KUNIT_TEST) += osf_core_test.o
drivers/iio/sensorhubs/osf/osf_core_test.c new file +1056 -0
1,056 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v11 7/8] iio: osf: add core KUnit tests
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] kunit: missing mutex_destroy() for dynamically allocated locks - [Low] kconfig: OSF KUnit tests…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] kunit: missing mutex_destroy() for dynamically allocated locks
- [Low] kconfig: OSF KUnit tests unnecessarily restricted to built-in modularity --
commit 168e51876dbdd13bb34af3a59ad653d5632d1c07
Author: Jinseob Kim <[email protected]>
iio: osf: add core KUnit tests
This commit adds focused unit tests for the Open Sensor Fusion core, exercising capability discovery, inventories, exact lengths, and reserved wire bytes. It also covers sample-cache admission and deferred cleanup routines.
Quoted 5 lines
drivers/iio/sensorhubs/osf/Kconfig +0
-0@@ -13,3 +13,15 @@ config OPEN_SENSOR_FUSION
[ ... ]
drivers/iio/sensorhubs/osf/Kconfig config OPEN_SENSOR_FUSION · line 19
Quoted 3 lines
drivers/iio/sensorhubs/osf/Kconfig +3
-0+config OPEN_SENSOR_FUSION_KUNIT_TEST + bool "KUnit tests for Open Sensor Fusion" if !KUNIT_ALL_TESTS + depends on OPEN_SENSOR_FUSION=y && KUNIT=y
[Severity: Low]
Does this unnecessarily restrict test coverage when the driver is built as a loadable module?
Since the test object is appended to open-sensor-fusion-y in the Makefile, it seems it can be successfully built and executed inside the loadable module open-sensor-fusion.ko.
While it makes sense to avoid OPEN_SENSOR_FUSION=y with KUNIT=m to prevent link errors, forcing the driver to be built-in reduces coverage on standard distribution kernels. Could a dependency like depends on OPEN_SENSOR_FUSION && !(OPEN_SENSOR_FUSION=y && KUNIT=m) work better here to allow modular test execution?
Quoted 6 lines
drivers/iio/sensorhubs/osf/osf_core_test.c new file +0
-0@@ -0,0 +1,1056 @@
[ ... ]
drivers/iio/sensorhubs/osf/osf_core_test.c line 488
Quoted 4 lines
drivers/iio/sensorhubs/osf/osf_core_test.c new file +4
-0+static void osf_test_unregister_pending (void *data) +{ +osf_core_unregister_iio (data); +}
[Severity: Low]
Does this custom teardown path miss a call to mutex_destroy() for the pending device's latest_lock initialized during osf_core_init()?
Since KUnit will automatically free the allocated memory for the pending device at the end of the test, leaving the active mutex undestroyed can leave stale pointers in the lockdep tracking structures, causing debug splats when CONFIG_DEBUG_MUTEXES is enabled.
[ ... ]
Quoted 3 lines
drivers/iio/sensorhubs/osf/osf_core_test.c new file +3
-0+static void osf_session_fault_buffer_race_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv;
[ ... ]
drivers/iio/sensorhubs/osf/osf_core_test.c line 990
Quoted 5 lines
drivers/iio/sensorhubs/osf/osf_core_test.c new file +5
-0+ race =kunit_kzalloc (test,sizeof (*race),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, race); + race->ctx = ctx; +mutex_init (&race->rx_lock); +init_completion (&race->started);
[Severity: Low]
Are we missing a corresponding mutex_destroy() for the dynamically allocated race->rx_lock before the test finishes?
Similar to the issue above, the race object is managed by kunit_kzalloc() and will be automatically freed. Freeing active mutex memory without destroying it could trigger lockdep warnings.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Jonathan Cameron
maintainerRe: [PATCH v11 7/8] iio: osf: add core KUnit tests
In reply to Jinseob KimQuoted 7 lines · On Mon, 12 Oct 2026 08:13:39 +0900 Jinseob Kim <[email protected]> wrote:
Exercise capability discovery, complete repeated inventories, sample-cache admission, exact lengths, reserved wire bytes and session faults. Protect producer lifetime with deferred cleanup and close the session mutex lifecycle before reinitialization.
Assisted-by LLMSigned-off-by Jinseob Kim <[email protected]>
https://sashiko.dev/#/patchset/20261011231340.42885-1-kimjinseob88%40gmail.com
The comment about maybe allowing building when osf is a module looks superficially valid. Some of the others are about outdated code, but please take a look.
Thanks,
Jonathan
Quoted 1,099 lines
--- drivers/iio/sensorhubs/osf/Kconfig | 12 + drivers/iio/sensorhubs/osf/Makefile | 1 + drivers/iio/sensorhubs/osf/osf_core_test.c | 1056 ++++++++++++++++++++ 3 files changed, 1069 insertions(+) create mode 100644 drivers/iio/sensorhubs/osf/osf_core_test.cdrivers/iio/sensorhubs/osf/Kconfig +12
-0@@ -13,3 +13,15 @@ config OPEN_SENSOR_FUSION registers IIO devices for supported capability entries. It exposes accelerometer, gyroscope, magnetometer, and temperature samples through IIO direct reads and software buffers. + +config OPEN_SENSOR_FUSION_KUNIT_TEST + bool "KUnit tests for Open Sensor Fusion" if !KUNIT_ALL_TESTS + depends on OPEN_SENSOR_FUSION=y && KUNIT=y + default KUNIT_ALL_TESTS + help + Build focused unit tests for the Open Sensor Fusion core sample + acceptance, direct-read cache, and capability/session handling. The tests + exercise accepted, ignored, and rejected sample handling without + exposing additional production interfaces. + + If unsure, say N.drivers/iio/sensorhubs/osf/Makefile +1
-0@@ -4,3 +4,4 @@ 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 +open-sensor-fusion-$(CONFIG_OPEN_SENSOR_FUSION_KUNIT_TEST) += osf_core_test.odrivers/iio/sensorhubs/osf/osf_core_test.c new file +1056
-0@@ -0,0 +1,1056 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <kunit/device.h> +#include <kunit/test.h> + +#include <linux/bitmap.h> +#include <linux/crc32.h> +#include <linux/completion.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/iio/buffer.h> +#include <linux/iio/buffer_impl.h> +#include <linux/iio/iio.h> +#include <linux/module.h> +#include <linux/kthread.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/unaligned.h> + +#include "osf_core.h" +#include "osf_protocol.h" +#include "osf_stream.h" + +#define OSF_TEST_SENSOR_INDEX 0 +#define OSF_TEST_UNSUPPORTED_INDEX 7 +#define OSF_TEST_SCALE_NANO 1000000 +#define OSF_TEST_FLOOD_COUNT (OSF_MAX_CAPABILITIES + 4) +#define OSF_TEST_MAX_PAYLOAD_LEN \ + (OSF_SENSOR_SAMPLE_BASE_LEN + OSF_MAX_SAMPLE_CHANNELS * sizeof(__le32)) +#define OSF_TEST_MAX_FRAME_LEN \ + (OSF_FRAME_HEADER_LEN + OSF_CAP_REPORT_BASE_LEN + \ + 3 * OSF_CAP_SENSOR_ENTRY_LEN + OSF_FRAME_CRC_LEN) + +struct osf_test_context { +struct osf_device osf; +struct device *dev; +struct iio_dev *indio_dev; +struct iio_buffer *buffer; +bool buffer_enabled; +}; + +struct osf_test_scan_3axis { +s32 values[3 ]; +}; + +static size_t osf_test_build_frame (u8 *buf,size_t buf_size, +u16 message_type,const u8 *payload, +u32 payload_len,u64 sequence) +{ +size_t frame_len =OSF_FRAME_HEADER_LEN + payload_len + +OSF_FRAME_CRC_LEN ; +u32 crc; + +if (frame_len > buf_size) +return 0 ; + +memset (buf,0 , frame_len); +put_unaligned_le32 (OSF_FRAME_MAGIC , buf); + buf[4 ] =OSF_PROTOCOL_MAJOR ; + buf[5 ] =OSF_PROTOCOL_MINOR ; +put_unaligned_le16 (OSF_FRAME_HEADER_LEN , buf +6 ); +put_unaligned_le16 (message_type, buf +8 ); +put_unaligned_le32 (payload_len, buf +10 ); +put_unaligned_le64 (sequence, buf +14 ); +put_unaligned_le64 (sequence *1000 , buf +22 ); +memcpy (buf +OSF_FRAME_HEADER_LEN , payload, payload_len); + + crc =crc32_le (~0U , buf,OSF_FRAME_HEADER_LEN + payload_len) ^ ~0U ; +put_unaligned_le32 (crc, buf +OSF_FRAME_HEADER_LEN + payload_len); + +return frame_len; +} + +static size_t osf_test_build_capability_frame (u8 *buf,size_t buf_size, +u64 sequence) +{ +u8 payload[OSF_CAP_REPORT_BASE_LEN + +2 *OSF_CAP_SENSOR_ENTRY_LEN ] = { }; +u8 *supported = payload +OSF_CAP_REPORT_BASE_LEN ; +u8 *unsupported = supported +OSF_CAP_SENSOR_ENTRY_LEN ; + +put_unaligned_le16 (2 , payload); +put_unaligned_le16 (OSF_SENSOR_ACCELEROMETER , supported); +put_unaligned_le16 (OSF_TEST_SENSOR_INDEX , supported +2 ); +put_unaligned_le16 (3 , supported +4 ); +put_unaligned_le16 (OSF_SAMPLE_FORMAT_S32 , supported +6 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO , supported +8 ); + +put_unaligned_le16 (OSF_SENSOR_BAROMETER , unsupported); +put_unaligned_le16 (OSF_TEST_UNSUPPORTED_INDEX , unsupported +2 ); +put_unaligned_le16 (1 , unsupported +4 ); +put_unaligned_le16 (OSF_SAMPLE_FORMAT_S32 , unsupported +6 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO , unsupported +8 ); + +return osf_test_build_frame (buf, buf_size,OSF_MSG_CAPABILITY_REPORT , + payload,sizeof (payload), sequence); +} + +static size_t osf_test_build_sample_frame (u8 *buf,size_t buf_size, +u16 sensor_type,u16 sensor_index, +u16 channel_count,u16 sample_format, +const s32 *values,u64 sequence) +{ +u8 payload[OSF_TEST_MAX_PAYLOAD_LEN ] = { }; +u32 payload_len; + +if (!channel_count || channel_count >OSF_MAX_SAMPLE_CHANNELS ) +return 0 ; + +put_unaligned_le16 (sensor_type, payload); +put_unaligned_le16 (sensor_index, payload +2 ); +put_unaligned_le16 (channel_count, payload +4 ); +put_unaligned_le16 (sample_format, payload +6 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO , payload +8 ); + +for (u16 i =0 ; i < channel_count; i++) +put_unaligned_le32 (values[i], + payload +OSF_SENSOR_SAMPLE_BASE_LEN + + i *sizeof (__le32)); + + payload_len =OSF_SENSOR_SAMPLE_BASE_LEN + + channel_count *sizeof (__le32); + +return osf_test_build_frame (buf, buf_size,OSF_MSG_SENSOR_SAMPLE , + payload, payload_len, sequence); +} + +static size_t osf_test_build_truncated_sample_frame (u8 *buf,size_t buf_size, +u64 sequence) +{ +u8 payload[OSF_SENSOR_SAMPLE_BASE_LEN +sizeof (__le32)] = { }; + +put_unaligned_le16 (OSF_SENSOR_ACCELEROMETER , payload); +put_unaligned_le16 (OSF_TEST_SENSOR_INDEX , payload +2 ); +put_unaligned_le16 (3 , payload +4 ); +put_unaligned_le16 (OSF_SAMPLE_FORMAT_S32 , payload +6 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO , payload +8 ); +put_unaligned_le32 (42 , payload +OSF_SENSOR_SAMPLE_BASE_LEN ); + +return osf_test_build_frame (buf, buf_size,OSF_MSG_SENSOR_SAMPLE , + payload,sizeof (payload), sequence); +} + +/* Keep frame-construction scratch out of the caller's KASAN stack frame. */ +static noinline int osf_test_submit_sample(struct osf_test_context *ctx, +u16 sensor_type,u16 sensor_index, +u16 channel_count,u16 sample_format, +const s32 *values,u64 sequence) +{ +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t frame_len; + + frame_len =osf_test_build_sample_frame (frame,sizeof (frame), + sensor_type, sensor_index, + channel_count, sample_format, + values, sequence); +if (!frame_len) +return -EINVAL ; + +return osf_core_receive_frame (&ctx->osf, frame, frame_len); +} + +static int osf_test_read_raw (struct osf_test_context *ctx, +unsigned int channel,int *value) +{ +int value2 =0 ; + +return ctx->indio_dev->info->read_raw (ctx->indio_dev, + &ctx->indio_dev->channels[channel], + value, &value2, +IIO_CHAN_INFO_RAW ); +} + +static int osf_test_remove_scan (struct osf_test_context *ctx, +struct osf_test_scan_3axis *scan) +{ +return iio_pop_from_buffer (ctx->buffer, scan); +} + +static int osf_test_enable_buffer (struct osf_test_context *ctx) +{ +unsigned long *scan_mask; +int ret; + + scan_mask = (unsigned long *)ctx->buffer->scan_mask; +for (unsigned int i =0 ; i <3 ; i++) +bitmap_set (scan_mask, ctx->indio_dev->channels[i].scan_index,1 ); + ret =iio_update_buffers (ctx->indio_dev, ctx->buffer,NULL ); +if (!ret) + ctx->buffer_enabled = true; + +return ret; +} + +static void osf_test_cleanup (void *data) +{ +struct osf_test_context *ctx = data; + +if (ctx->buffer_enabled) { +iio_update_buffers (ctx->indio_dev,NULL , ctx->buffer); + ctx->buffer_enabled = false; + } + +osf_core_unregister_iio (&ctx->osf); +mutex_destroy (&ctx->osf.latest_lock); +} + +static int osf_test_init (struct kunit *test) +{ +struct osf_test_context *ctx; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t frame_len; +int ret; + + ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return -ENOMEM ; + + test->priv = ctx; + ctx->dev =kunit_device_register (test,"osf-core" ); +if (IS_ERR (ctx->dev)) +return PTR_ERR (ctx->dev); +if (!ctx->dev) +return -ENOMEM ; + +osf_core_init (&ctx->osf, ctx->dev); +osf_core_start (&ctx->osf); + ret =kunit_add_action_or_reset (test, osf_test_cleanup, ctx); +if (ret) +return ret; + + frame_len =osf_test_build_capability_frame (frame,sizeof (frame),1 ); +if (!frame_len) +return -EINVAL ; + + ret =osf_core_receive_frame (&ctx->osf, frame, frame_len); +if (ret !=OSF_STREAM_FRAME_HANDLED ) +return ret <0 ? ret : -EINVAL ; +if (ctx->osf.iio_dev_count !=1 || + ctx->osf.capability_cache.capability_count !=1 ) +return -EINVAL ; + + ctx->indio_dev = ctx->osf.iio_devs[0 ].indio_dev; + ctx->buffer = ctx->indio_dev->buffer; +if (!ctx->buffer) +return -EINVAL ; + +return 0 ; +} + +static void osf_test_expect_direct_sample (struct kunit *test, +struct osf_test_context *ctx, +const s32 *expected) +{ +int value; +int ret; + +for (unsigned int i =0 ; i <3 ; i++) { + value =0 ; + ret =osf_test_read_raw (ctx, i, &value); +KUNIT_EXPECT_EQ (test, ret,IIO_VAL_INT ); +KUNIT_EXPECT_EQ (test, value, expected[i]); + } +} + +static void +osf_rejected_channel_count_preserves_latest_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 baseline_values[] = {11 ,22 ,33 }; +const s32 rejected_values[] = {-999 }; +struct osf_sensor_sample decoded_sample; +struct osf_latest_sample baseline; +struct osf_test_scan_3axis scan; +struct osf_frame decoded_frame; +u8 rejected_frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t decoded_len; +size_t frame_len; +u64 baseline_sequence; +int ret; + +KUNIT_ASSERT_EQ (test,osf_test_enable_buffer (ctx),0 ); +KUNIT_ASSERT_TRUE (test,iio_buffer_enabled (ctx->indio_dev)); + + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,3 , +OSF_SAMPLE_FORMAT_S32 , + baseline_values,2 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,1U ); +osf_test_expect_direct_sample (test, ctx, baseline_values); + +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); + + baseline = ctx->osf.latest_samples[0 ]; + baseline_sequence = ctx->osf.last_sequence; + frame_len =osf_test_build_sample_frame (rejected_frame, +sizeof (rejected_frame), +OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,1 , +OSF_SAMPLE_FORMAT_S32 , + rejected_values,3 ); +KUNIT_ASSERT_NE (test, frame_len, (size_t )0 ); +KUNIT_ASSERT_EQ (test, +osf_protocol_decode_frame (rejected_frame, frame_len, + &decoded_frame, &decoded_len),0 ); +KUNIT_ASSERT_EQ (test, decoded_len, frame_len); +KUNIT_ASSERT_EQ (test, +osf_protocol_decode_sensor_sample (&decoded_frame, + &decoded_sample),0 ); +KUNIT_ASSERT_EQ (test, decoded_sample.channel_count, (u16 )1 ); + ret =osf_core_receive_frame (&ctx->osf, rejected_frame, frame_len); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_REJECTED ); +KUNIT_EXPECT_EQ (test, ctx->osf.latest_sample_count,1U ); +KUNIT_EXPECT_MEMEQ (test, &ctx->osf.latest_samples[0 ], &baseline, +sizeof (baseline)); +KUNIT_EXPECT_EQ (test, ctx->osf.last_sequence, baseline_sequence); +osf_test_expect_direct_sample (test, ctx, baseline_values); +KUNIT_EXPECT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); +} + +static void +osf_valid_sample_updates_direct_and_buffer_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 baseline_values[] = {1 ,2 ,3 }; +const s32 rejected_values[] = {-1 }; +const s32 valid_values[] = {101 ,-202 ,303 }; +struct osf_test_scan_3axis scan = { }; +int ret; + +KUNIT_ASSERT_EQ (test,osf_test_enable_buffer (ctx),0 ); + + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,3 , +OSF_SAMPLE_FORMAT_S32 , + baseline_values,2 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); + + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,1 , +OSF_SAMPLE_FORMAT_S32 , + rejected_values,3 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_REJECTED ); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); + + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,3 , +OSF_SAMPLE_FORMAT_S32 , + valid_values,4 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_HANDLED ); +osf_test_expect_direct_sample (test, ctx, valid_values); +KUNIT_ASSERT_EQ (test, ctx->indio_dev->scan_bytes, (int )sizeof (scan)); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); +KUNIT_EXPECT_EQ (test, scan.values[0 ], valid_values[0 ]); +KUNIT_EXPECT_EQ (test, scan.values[1 ], valid_values[1 ]); +KUNIT_EXPECT_EQ (test, scan.values[2 ], valid_values[2 ]); +KUNIT_EXPECT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); +} + +static void osf_unregistered_sample_is_ignored_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 baseline_values[] = {10 ,20 ,30 }; +const s32 ignored_values[] = {-10 ,-20 ,-30 }; +struct osf_latest_sample baseline; +struct osf_test_scan_3axis scan; +s32 value; +int ret; + +KUNIT_ASSERT_EQ (test,osf_test_enable_buffer (ctx),0 ); + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,3 , +OSF_SAMPLE_FORMAT_S32 , + baseline_values,2 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); + baseline = ctx->osf.latest_samples[0 ]; + + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX +1 ,3 , +OSF_SAMPLE_FORMAT_S32 , + ignored_values,3 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_IGNORED ); +KUNIT_EXPECT_EQ (test, ctx->osf.iio_dev_count,1U ); +KUNIT_EXPECT_EQ (test, ctx->osf.latest_sample_count,1U ); +KUNIT_EXPECT_MEMEQ (test, &ctx->osf.latest_samples[0 ], &baseline, +sizeof (baseline)); +osf_test_expect_direct_sample (test, ctx, baseline_values); + ret =osf_core_read_latest_sample (&ctx->osf, +OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX +1 , +0 , &value); +KUNIT_EXPECT_EQ (test, ret, -ENODATA ); +KUNIT_EXPECT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); +} + +static void +osf_unaccepted_samples_do_not_exhaust_cache_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 three_values[] = {7 ,8 ,9 }; +const s32 one_value[] = {42 }; +struct osf_latest_sample *empty_cache; +struct osf_sensor_sample decoded_sample; +struct osf_frame decoded_frame; +u8 malformed_frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t decoded_len; +size_t frame_len; +int value; +int ret; + +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,0U ); + empty_cache =kunit_kzalloc (test,sizeof (ctx->osf.latest_samples),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, empty_cache); +memcpy (empty_cache, ctx->osf.latest_samples,sizeof (ctx->osf.latest_samples)); + +for (unsigned int i =0 ; i <OSF_TEST_FLOOD_COUNT ; i++) { + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX + i +1 , +3 ,OSF_SAMPLE_FORMAT_S32 , + three_values,100 + i); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_IGNORED ); + } +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,0U ); + +for (unsigned int i =0 ; i <OSF_TEST_FLOOD_COUNT ; i++) { + ret =osf_test_submit_sample (ctx,OSF_SENSOR_BAROMETER , +OSF_TEST_UNSUPPORTED_INDEX ,1 , +OSF_SAMPLE_FORMAT_S32 , + one_value,200 + i); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_IGNORED ); + } +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,0U ); + +for (unsigned int i =0 ; i <OSF_TEST_FLOOD_COUNT ; i++) { + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,1 , +OSF_SAMPLE_FORMAT_S32 , + one_value,300 + i); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_REJECTED ); + } +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,0U ); + + frame_len =osf_test_build_truncated_sample_frame (malformed_frame, +sizeof (malformed_frame),400 ); +KUNIT_ASSERT_NE (test, frame_len, (size_t )0 ); +KUNIT_ASSERT_EQ (test, +osf_protocol_decode_frame (malformed_frame, frame_len, + &decoded_frame, &decoded_len),0 ); +KUNIT_ASSERT_EQ (test, decoded_len, frame_len); +KUNIT_ASSERT_EQ (test, +osf_protocol_decode_sensor_sample (&decoded_frame, + &decoded_sample), + -EMSGSIZE ); + +for (unsigned int i =0 ; i <OSF_TEST_FLOOD_COUNT ; i++) { + ret =osf_core_receive_frame (&ctx->osf, malformed_frame, + frame_len); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_REJECTED ); + } +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,0U ); + +KUNIT_EXPECT_MEMEQ (test, ctx->osf.latest_samples, empty_cache, +sizeof (ctx->osf.latest_samples)); +KUNIT_EXPECT_EQ (test, ctx->osf.last_sequence, (u64 )1 ); + + ret =osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +OSF_TEST_SENSOR_INDEX ,3 , +OSF_SAMPLE_FORMAT_S32 , + three_values,500 ); +KUNIT_ASSERT_EQ (test, ret,OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test, ctx->osf.latest_sample_count,1U ); + +for (unsigned int i =0 ; i <3 ; i++) { + value =0 ; + ret =osf_test_read_raw (ctx, i, &value); +KUNIT_ASSERT_EQ (test, ret,IIO_VAL_INT ); +KUNIT_EXPECT_EQ (test, value, three_values[i]); + } +} + +static void osf_test_unregister_pending (void *data) +{ +osf_core_unregister_iio (data); +} + +static void osf_early_capability_is_owned_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_device *pending; +struct iio_dev *indio_dev; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t len; +int scale, nano, ret; + + pending =kunit_kzalloc (test,sizeof (*pending),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, pending); +osf_core_init (pending, ctx->dev); + ret =kunit_add_action_or_reset (test, osf_test_unregister_pending, pending); +KUNIT_ASSERT_EQ (test, ret,0 ); + len =osf_test_build_capability_frame (frame,sizeof (frame),10 ); +KUNIT_ASSERT_NE (test, len, (size_t )0 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (pending, frame, len), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_EXPECT_EQ (test, pending->iio_dev_count,0U ); +KUNIT_ASSERT_TRUE (test, pending->capability_cache.valid); +KUNIT_EXPECT_EQ (test, pending->capability_cache.capability_count, (u16 )1 ); + +/* The stream may reuse its storage before probe opens the registration gate. */ +memset (frame,0xa5 ,sizeof (frame)); +osf_core_start (pending); +KUNIT_ASSERT_EQ (test, pending->iio_dev_count,1U ); + indio_dev = pending->iio_devs[0 ].indio_dev; + ret = indio_dev->info->read_raw (indio_dev, &indio_dev->channels[0 ], + &scale, &nano,IIO_CHAN_INFO_SCALE ); +KUNIT_EXPECT_EQ (test, ret,IIO_VAL_INT_PLUS_NANO ); +KUNIT_EXPECT_EQ (test, scale,0 ); +KUNIT_EXPECT_EQ (test, nano,OSF_TEST_SCALE_NANO ); +osf_core_start (pending); +KUNIT_EXPECT_EQ (test, pending->iio_dev_count,1U ); + len =osf_test_build_capability_frame (frame,sizeof (frame),11 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (pending, frame, len), +OSF_STREAM_FRAME_IGNORED ); +KUNIT_EXPECT_EQ (test, pending->iio_dev_count,1U ); +KUNIT_EXPECT_EQ (test, pending->latest_sample_count,0U ); +} + +static void osf_early_duplicate_capability_is_rejected_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_device *pending; +u8 payload[OSF_CAP_REPORT_BASE_LEN +2 *OSF_CAP_SENSOR_ENTRY_LEN ] = { }; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +u8 *entry = payload +OSF_CAP_REPORT_BASE_LEN ; +size_t len; +int ret; + + pending =kunit_kzalloc (test,sizeof (*pending),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, pending); +osf_core_init (pending, ctx->dev); + ret =kunit_add_action_or_reset (test, osf_test_unregister_pending, pending); +KUNIT_ASSERT_EQ (test, ret,0 ); +put_unaligned_le16 (2 , payload); +put_unaligned_le16 (OSF_SENSOR_ACCELEROMETER , entry); +put_unaligned_le16 (3 , entry +4 ); +put_unaligned_le16 (OSF_SAMPLE_FORMAT_S32 , entry +6 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO , entry +8 ); +memcpy (entry +OSF_CAP_SENSOR_ENTRY_LEN , entry,OSF_CAP_SENSOR_ENTRY_LEN ); + len =osf_test_build_frame (frame,sizeof (frame),OSF_MSG_CAPABILITY_REPORT , + payload,sizeof (payload),20 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (pending, frame, len), +OSF_STREAM_FRAME_REJECTED ); +KUNIT_EXPECT_FALSE (test, pending->capability_cache.valid); +KUNIT_EXPECT_EQ (test, pending->iio_dev_count,0U ); + +/* A failed early report must not prevent a subsequent valid report. */ + len =osf_test_build_capability_frame (frame,sizeof (frame),21 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (pending, frame, len), +OSF_STREAM_FRAME_HANDLED ); +osf_core_start (pending); +KUNIT_EXPECT_EQ (test, pending->iio_dev_count,1U ); +} + +static const struct osf_capability_entry osf_test_accel = { + .sensor_type =OSF_SENSOR_ACCELEROMETER , + .channel_count =3 , + .sample_format =OSF_SAMPLE_FORMAT_S32 , + .scale_nano =OSF_TEST_SCALE_NANO , +}; + +static void osf_test_crc (u8 *frame,size_t len) +{ +put_unaligned_le32 (crc32_le (~0U , frame, len -OSF_FRAME_CRC_LEN ) ^ ~0U , + frame + len -OSF_FRAME_CRC_LEN ); +} + +static size_t osf_test_report (u8 *frame, +const struct osf_capability_entry *entries,u16 count) +{ +u8 payload[OSF_CAP_REPORT_BASE_LEN +3 *OSF_CAP_SENSOR_ENTRY_LEN ] = { }; + +put_unaligned_le16 (count, payload); +for (u16 i =0 ; i < count; i++) { +u8 *p = payload +OSF_CAP_REPORT_BASE_LEN + i *OSF_CAP_SENSOR_ENTRY_LEN ; + +put_unaligned_le16 (entries[i].sensor_type, p); +put_unaligned_le16 (entries[i].sensor_index, p +2 ); +put_unaligned_le16 (entries[i].channel_count, p +4 ); +put_unaligned_le16 (entries[i].sample_format, p +6 ); +put_unaligned_le32 (entries[i].scale_nano, p +8 ); +put_unaligned_le32 (entries[i].flags, p +12 ); + } +return osf_test_build_frame (frame,OSF_TEST_MAX_FRAME_LEN , +OSF_MSG_CAPABILITY_REPORT , payload, +OSF_CAP_REPORT_BASE_LEN + count *OSF_CAP_SENSOR_ENTRY_LEN , +10 ); +} + +static int osf_test_submit_report (struct osf_test_context *ctx, +const struct osf_capability_entry *entries,u16 count) +{ +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t len =osf_test_report (frame, entries, count); + +return osf_core_receive_frame (&ctx->osf, frame, len); +} + +static void osf_test_new_session (struct osf_test_context *ctx) +{ +osf_test_cleanup (ctx); +osf_core_init (&ctx->osf, ctx->dev); +osf_core_start (&ctx->osf); + ctx->indio_dev =NULL ; + ctx->buffer =NULL ; +} + +static void osf_test_bind_accel (struct kunit *test,struct osf_test_context *ctx) +{ +KUNIT_ASSERT_EQ (test,osf_test_submit_report (ctx, &osf_test_accel,1 ), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test, ctx->osf.iio_dev_count,1U ); + ctx->indio_dev = ctx->osf.iio_devs[0 ].indio_dev; + ctx->buffer = ctx->indio_dev->buffer; +} + +static void osf_zero_scale_is_atomic_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_capability_entry entries[] = { osf_test_accel, osf_test_accel }; + + entries[1 ].sensor_index =1 ; + entries[1 ].scale_nano =0 ; +/* Even a repeat with a valid prefix must be rejected without a fault. */ +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries,2 ), +OSF_STREAM_FRAME_REJECTED ); +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +KUNIT_EXPECT_EQ (test, ctx->osf.iio_dev_count,1U ); +osf_test_new_session (ctx); +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries,2 ), +OSF_STREAM_FRAME_REJECTED ); +KUNIT_EXPECT_EQ (test, ctx->osf.iio_dev_count,0U ); +KUNIT_EXPECT_FALSE (test, ctx->osf.capability_cache.valid); +osf_test_bind_accel (test, ctx); +} + +static int osf_test_stream_frame (void *context,const u8 *buf,size_t len) +{ +return osf_core_receive_frame (context, buf, len); +} + +static void osf_scale_mismatch_preserves_and_recovers_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 values[] = {OSF_FRAME_MAGIC ,-22 ,33 }; +const s32 next[] = {101 ,202 ,303 }; +struct osf_latest_sample baseline; +struct osf_test_scan_3axis scan; +struct osf_stream *stream; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t len; + + stream =kunit_kzalloc (test,sizeof (*stream),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, stream); +osf_stream_init (stream, osf_test_stream_frame, &ctx->osf); +KUNIT_ASSERT_EQ (test,osf_test_enable_buffer (ctx),0 ); +KUNIT_ASSERT_EQ (test,osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , values,2 ), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); + baseline = ctx->osf.latest_samples[0 ]; + len =osf_test_build_sample_frame (frame,sizeof (frame),OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , values,3 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO +1 , frame +OSF_FRAME_HEADER_LEN +8 ); +osf_test_crc (frame, len); +KUNIT_EXPECT_EQ (test,osf_stream_receive_bytes (stream, frame, len),0 ); +KUNIT_EXPECT_EQ (test, stream->stats.rejected_frames,1ULL ); +KUNIT_EXPECT_EQ (test, stream->stats.validated_frames,1ULL ); +KUNIT_EXPECT_EQ (test, stream->stats.bad_magic_resyncs,0ULL ); +KUNIT_EXPECT_MEMEQ (test, &ctx->osf.latest_samples[0 ], &baseline,sizeof (baseline)); +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +osf_test_expect_direct_sample (test, ctx, values); +KUNIT_EXPECT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); +KUNIT_EXPECT_EQ (test,osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , next,4 ), +OSF_STREAM_FRAME_HANDLED ); +osf_test_expect_direct_sample (test, ctx, next); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); +KUNIT_EXPECT_MEMEQ (test, scan.values, next,sizeof (next)); +} + +static void osf_test_initial_recovery (struct kunit *test,bool unsupported) +{ +struct osf_test_context *ctx = test->priv; +struct osf_capability_entry entry = osf_test_accel; + +osf_test_new_session (ctx); + entry.sensor_type =0xffff ; +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, &entry, unsupported ?1 :0 ), +OSF_STREAM_FRAME_IGNORED ); +KUNIT_EXPECT_FALSE (test, ctx->osf.capability_cache.valid); +KUNIT_EXPECT_EQ (test, ctx->osf.iio_dev_count,0U ); +osf_test_bind_accel (test, ctx); +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +} + +static void osf_initial_empty_recovers_test (struct kunit *test) +{ +osf_test_initial_recovery (test, false); +} + +static void osf_initial_unsupported_recovers_test (struct kunit *test) +{ +osf_test_initial_recovery (test, true); +} + +static void osf_equivalent_reports_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_capability_entry entries[] = { osf_test_accel, osf_test_accel, + osf_test_accel }; +struct osf_capability_entry swap; +struct iio_dev *first; + +osf_test_new_session (ctx); + entries[1 ].sensor_type =OSF_SENSOR_GYROSCOPE ; +KUNIT_ASSERT_EQ (test,osf_test_submit_report (ctx, entries,2 ),OSF_STREAM_FRAME_HANDLED ); + first = ctx->osf.iio_devs[0 ].indio_dev; +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries,2 ),OSF_STREAM_FRAME_IGNORED ); + swap = entries[0 ]; + entries[0 ] = entries[1 ]; + entries[1 ] = swap; +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries,2 ),OSF_STREAM_FRAME_IGNORED ); + entries[0 ].flags =OSF_CAPABILITY_FLAGS_MASK ; + entries[2 ].sensor_type =0xffff ; +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries,3 ),OSF_STREAM_FRAME_IGNORED ); +/* Unknown flags make the added key unsupported. */ + entries[2 ] = osf_test_accel; + entries[2 ].sensor_index =1 ; + entries[2 ].flags =BIT (7 ); +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries,3 ),OSF_STREAM_FRAME_IGNORED ); +KUNIT_EXPECT_EQ (test, ctx->osf.iio_dev_count,2U ); +KUNIT_EXPECT_PTR_EQ (test, ctx->osf.iio_devs[0 ].indio_dev, first); +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +} + +static void osf_changed_reports_fault_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 v[] = {11 ,22 ,33 }; +struct osf_test_scan_3axis scan; + +for (unsigned int change =0 ; change <8 ; change++) { +struct osf_capability_entry entries[] = { osf_test_accel, osf_test_accel }; +struct osf_latest_sample baseline; +struct iio_dev *child; +u16 count =1 ; +int value =0 ; + +osf_test_new_session (ctx); +osf_test_bind_accel (test, ctx); + child = ctx->indio_dev; +KUNIT_ASSERT_EQ (test,osf_test_enable_buffer (ctx),0 ); +KUNIT_ASSERT_EQ (test,osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , v,2 ), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_ASSERT_EQ (test,osf_test_remove_scan (ctx, &scan),0 ); + baseline = ctx->osf.latest_samples[0 ]; +switch (change) { +case 0 : + entries[0 ].scale_nano++; +break ; +case 1 : + entries[0 ].channel_count =1 ; +break ; +case 2 : + entries[0 ].sample_format =2 ; +break ; +case 3 : + entries[0 ].sensor_index =1 ; +break ; +case 4 : + count =0 ; +break ; +case 5 : + entries[0 ].sensor_type =0xffff ; +break ; +case 6 : + entries[1 ].sensor_index =1 ; + count =2 ; +break ; +case 7 : + entries[0 ].flags =BIT (7 ); +break ; + } +kunit_info (test,"meaning change=%u\n" , change); +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, entries, count), +OSF_STREAM_FRAME_REJECTED ); +KUNIT_EXPECT_TRUE (test, ctx->osf.session_fault); +KUNIT_EXPECT_EQ (test, ctx->osf.iio_dev_count,1U ); +KUNIT_EXPECT_PTR_EQ (test, ctx->osf.iio_devs[0 ].indio_dev, child); +KUNIT_EXPECT_EQ (test,osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , v,3 ), +OSF_STREAM_FRAME_REJECTED ); +KUNIT_EXPECT_MEMEQ (test, &ctx->osf.latest_samples[0 ], &baseline,sizeof (baseline)); +KUNIT_EXPECT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); +KUNIT_EXPECT_EQ (test,osf_test_read_raw (ctx,0 , &value), -EPROTO ); +/* Equivalent reports do not repair a faulted host session. */ +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, &osf_test_accel,1 ), +OSF_STREAM_FRAME_IGNORED ); +KUNIT_EXPECT_EQ (test,osf_test_read_raw (ctx,0 , &value), -EPROTO ); + } +} + +static void osf_rebind_and_timestamp_restart_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_capability_entry entry = osf_test_accel; +const s32 values[] = {101 ,-202 ,303 }; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t len; + + entry.scale_nano++; +KUNIT_ASSERT_EQ (test,osf_test_submit_report (ctx, &entry,1 ),OSF_STREAM_FRAME_REJECTED ); +KUNIT_ASSERT_TRUE (test, ctx->osf.session_fault); +osf_test_new_session (ctx); +KUNIT_ASSERT_EQ (test,osf_test_submit_report (ctx, &entry,1 ),OSF_STREAM_FRAME_HANDLED ); + ctx->indio_dev = ctx->osf.iio_devs[0 ].indio_dev; + ctx->buffer = ctx->indio_dev->buffer; +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +KUNIT_EXPECT_EQ (test, ctx->osf.latest_sample_count,0U ); +for (unsigned int i =0 ; i <2 ; i++) { + len =osf_test_build_sample_frame (frame,sizeof (frame),OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , values, i ?0 :100 ); +put_unaligned_le32 (entry.scale_nano, frame +OSF_FRAME_HEADER_LEN +8 ); +osf_test_crc (frame, len); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_EXPECT_EQ (test,osf_test_submit_report (ctx, &entry,1 ), +OSF_STREAM_FRAME_IGNORED ); + } +osf_test_expect_direct_sample (test, ctx, values); +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +} + +static void osf_reserved_padding_is_ignored_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_capability_entry entry = osf_test_accel; +const s32 values[] = {11 ,22 ,33 }; +u8 status[OSF_DEVICE_STATUS_LEN ] = { }; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +size_t len; + +osf_test_new_session (ctx); + len =osf_test_report (frame, &entry,1 ); +put_unaligned_le32 (0xdeadbeef , frame +34 ); +put_unaligned_le16 (0x1234 , frame +OSF_FRAME_HEADER_LEN +2 ); +put_unaligned_le32 (0xa5a5a5a5 , + frame +OSF_FRAME_HEADER_LEN +OSF_CAP_REPORT_BASE_LEN +16 ); +osf_test_crc (frame, len); +KUNIT_ASSERT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_IGNORED ); +KUNIT_ASSERT_EQ (test, ctx->osf.iio_dev_count,1U ); + ctx->indio_dev = ctx->osf.iio_devs[0 ].indio_dev; + ctx->buffer = ctx->indio_dev->buffer; + len =osf_test_build_sample_frame (frame,sizeof (frame),OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , values,2 ); +put_unaligned_le32 (0xaabbccdd , frame +OSF_FRAME_HEADER_LEN +12 ); +osf_test_crc (frame, len); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_HANDLED ); +osf_test_expect_direct_sample (test, ctx, values); +put_unaligned_le32 (0x12345678 , status +16 ); + len =osf_test_build_frame (frame,sizeof (frame),OSF_MSG_DEVICE_STATUS , + status,sizeof (status),3 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_HANDLED ); +KUNIT_EXPECT_TRUE (test, ctx->osf.status_cache.valid); +} + +static void osf_bad_crc_bounds_and_extensions_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +const s32 values[] = {11 ,22 ,33 }; +struct osf_latest_sample baseline; +u8 frame[OSF_TEST_MAX_FRAME_LEN ]; +u8 payload[OSF_TEST_MAX_PAYLOAD_LEN +1 ] = { }; +size_t len; + +KUNIT_ASSERT_EQ (test,osf_test_submit_sample (ctx,OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , values,2 ), +OSF_STREAM_FRAME_HANDLED ); + baseline = ctx->osf.latest_samples[0 ]; + len =osf_test_report (frame, &osf_test_accel,1 ); + frame[34 ] =1 ; +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), -EBADMSG ); +osf_test_crc (frame, len); +put_unaligned_le32 (U32_MAX , frame +10 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), -EMSGSIZE ); + len =osf_test_report (frame, &osf_test_accel,1 ); +put_unaligned_le16 (2 , frame +OSF_FRAME_HEADER_LEN ); +osf_test_crc (frame, len); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_REJECTED ); +/* A CRC-valid trailing byte is not an extension of any fixed baseline ID. */ +for (u16 type =OSF_MSG_SENSOR_SAMPLE ; type <=OSF_MSG_CAPABILITY_REPORT ; type++) { +u32 size; + +memset (payload,0 ,sizeof (payload)); +if (type ==OSF_MSG_SENSOR_SAMPLE ) { +put_unaligned_le16 (OSF_SENSOR_ACCELEROMETER , payload); +put_unaligned_le16 (3 , payload +4 ); +put_unaligned_le16 (OSF_SAMPLE_FORMAT_S32 , payload +6 ); +put_unaligned_le32 (OSF_TEST_SCALE_NANO , payload +8 ); + size =OSF_TEST_MAX_PAYLOAD_LEN ; + }else if (type ==OSF_MSG_DEVICE_STATUS ) { + size =OSF_DEVICE_STATUS_LEN ; + }else { + size =OSF_CAP_REPORT_BASE_LEN ; + } + len =osf_test_build_frame (frame,sizeof (frame), type, payload, size +1 ,3 ); +KUNIT_EXPECT_EQ (test,osf_core_receive_frame (&ctx->osf, frame, len), +OSF_STREAM_FRAME_REJECTED ); + } +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +KUNIT_EXPECT_MEMEQ (test, &ctx->osf.latest_samples[0 ], &baseline,sizeof (baseline)); +} + +struct osf_session_race { +struct osf_test_context *ctx; +/* Model the transport serialization separately from IIO configuration. */ +struct mutex rx_lock; +struct completion started; +struct completion fault_observed; +atomic_t faulted; +atomic_t rejected; +atomic_t errors; +}; + +static int osf_session_producer (void *data) +{ +struct osf_session_race *race = data; +const s32 values[] = {101 ,202 ,303 }; +u64 sequence =100 ; +int ret, raw; + +complete (&race->started); +while (!kthread_should_stop ()) { +mutex_lock (&race->rx_lock); + ret =osf_test_submit_sample (race->ctx,OSF_SENSOR_ACCELEROMETER , +0 ,3 ,OSF_SAMPLE_FORMAT_S32 , values, sequence++); +if (atomic_read (&race->faulted)) { +if (ret ==OSF_STREAM_FRAME_REJECTED ) +atomic_inc (&race->rejected); +else +atomic_inc (&race->errors); +complete (&race->fault_observed); + } +mutex_unlock (&race->rx_lock); + ret =osf_test_read_raw (race->ctx,0 , &raw); +if (ret !=IIO_VAL_INT && ret != -EPROTO && ret != -ENODATA ) +atomic_inc (&race->errors); +cond_resched (); + } +return 0 ; +} + +KUNIT_DEFINE_ACTION_WRAPPER (osf_test_stop_kthread, kthread_stop, + struct task_struct *); + +static void osf_session_fault_buffer_race_test (struct kunit *test) +{ +struct osf_test_context *ctx = test->priv; +struct osf_capability_entry entry = osf_test_accel; +struct osf_latest_sample baseline; +struct osf_test_scan_3axis scan; +struct osf_session_race *race; +struct task_struct *producer; +int ret, raw; + + race =kunit_kzalloc (test,sizeof (*race),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, race); + race->ctx = ctx; +mutex_init (&race->rx_lock); +init_completion (&race->started); +init_completion (&race->fault_observed); +KUNIT_ASSERT_EQ (test,osf_test_enable_buffer (ctx),0 ); + producer =kthread_run (osf_session_producer, race,"osf-session-test" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, producer); + ret =kunit_add_action_or_reset (test, osf_test_stop_kthread, producer); +KUNIT_ASSERT_EQ (test, ret,0 ); +KUNIT_EXPECT_NE (test,wait_for_completion_timeout (&race->started,HZ ),0UL ); +/* Exercise buffer reconfiguration against latest_lock -> IIO mlock. */ +for (unsigned int i =0 ; i <100 ; i++) { +KUNIT_EXPECT_EQ (test,iio_update_buffers (ctx->indio_dev,NULL , ctx->buffer),0 ); +KUNIT_EXPECT_EQ (test,iio_update_buffers (ctx->indio_dev, ctx->buffer,NULL ),0 ); +cond_resched (); + } +mutex_lock (&race->rx_lock); + entry.scale_nano++; + ret =osf_test_submit_report (ctx, &entry,1 ); +KUNIT_EXPECT_EQ (test, ret,OSF_STREAM_FRAME_REJECTED ); +atomic_set (&race->faulted,1 ); + baseline = ctx->osf.latest_samples[0 ]; +while (!osf_test_remove_scan (ctx, &scan)) + ; +mutex_unlock (&race->rx_lock); +KUNIT_EXPECT_NE (test,wait_for_completion_timeout (&race->fault_observed, +5 *HZ ),0UL ); +kunit_release_action (test, osf_test_stop_kthread, producer); +KUNIT_EXPECT_GT (test,atomic_read (&race->rejected),0 ); +KUNIT_EXPECT_EQ (test,atomic_read (&race->errors),0 ); +KUNIT_EXPECT_MEMEQ (test, &ctx->osf.latest_samples[0 ], &baseline,sizeof (baseline)); +KUNIT_EXPECT_EQ (test,osf_test_remove_scan (ctx, &scan), -EBUSY ); +KUNIT_EXPECT_EQ (test,osf_test_read_raw (ctx,0 , &raw), -EPROTO ); +/* Cleanup follows the real producer-stop -> child-unregister ordering. */ +osf_test_new_session (ctx); +osf_test_bind_accel (test, ctx); +KUNIT_EXPECT_FALSE (test, ctx->osf.session_fault); +} + +static struct kunit_case osf_core_test_cases[] = { +KUNIT_CASE (osf_zero_scale_is_atomic_test), +KUNIT_CASE (osf_scale_mismatch_preserves_and_recovers_test), +KUNIT_CASE (osf_initial_empty_recovers_test), +KUNIT_CASE (osf_initial_unsupported_recovers_test), +KUNIT_CASE (osf_equivalent_reports_test), +KUNIT_CASE (osf_changed_reports_fault_test), +KUNIT_CASE (osf_rebind_and_timestamp_restart_test), +KUNIT_CASE (osf_reserved_padding_is_ignored_test), +KUNIT_CASE (osf_bad_crc_bounds_and_extensions_test), +KUNIT_CASE (osf_session_fault_buffer_race_test), + +KUNIT_CASE (osf_early_capability_is_owned_test), +KUNIT_CASE (osf_early_duplicate_capability_is_rejected_test), +KUNIT_CASE (osf_rejected_channel_count_preserves_latest_test), +KUNIT_CASE (osf_valid_sample_updates_direct_and_buffer_test), +KUNIT_CASE (osf_unregistered_sample_is_ignored_test), +KUNIT_CASE (osf_unaccepted_samples_do_not_exhaust_cache_test), + { } +}; + +static struct kunit_suite osf_core_test_suite = { + .name ="osf-core" , + .init = osf_test_init, + .test_cases = osf_core_test_cases, +}; + +kunit_test_suite (osf_core_test_suite); + +MODULE_LICENSE ("GPL" );
PATCH v11 8/8 iio: osf: add IIO KUnit tests
In reply to Jinseob KimExercise active-scan packing, timestamp alignment and producer quiescence during buffer reconfiguration and unregister. Check that a blocked OSF push retains its active scan mask until its mode claim is released.
Use deferred actions for
workers, registered devices, temporary buffer callbacks and extra device references so assertion aborts release resources in ownership order.
--- drivers/iio/sensorhubs/osf/Kconfig | 2 +- drivers/iio/sensorhubs/osf/Makefile | 2 +- drivers/iio/sensorhubs/osf/osf_iio_test.c | 386 ++++++++++++++++++++++ 3 files changed, 388 insertions(+), 2 deletions(-) create mode 100644 drivers/iio/sensorhubs/osf/osf_iio_test.c
drivers/iio/sensorhubs/osf/Kconfig +1 -1
@@ -20,7 +20,7 @@ config OPEN_SENSOR_FUSION_KUNIT_TEST default KUNIT_ALL_TESTS help Build focused unit tests for the Open Sensor Fusion core sample - acceptance, direct-read cache, and capability/session handling. The tests + acceptance, direct-read cache, and IIO buffer paths. The tests exercise accepted, ignored, and rejected sample handling without exposing additional production interfaces.
drivers/iio/sensorhubs/osf/Makefile +1 -1
@@ -4,4 +4,4 @@ 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 -open-sensor-fusion-$(CONFIG_OPEN_SENSOR_FUSION_KUNIT_TEST) += osf_core_test.o +open-sensor-fusion-$(CONFIG_OPEN_SENSOR_FUSION_KUNIT_TEST) += osf_core_test.o osf_iio_test.o
drivers/iio/sensorhubs/osf/osf_iio_test.c new file +386 -0
386 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v11 8/8] iio: osf: add IIO KUnit tests
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] iio: osf: KUnit test hangs kernel due to invalid assumption about unregister serialization…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] iio: osf: KUnit test hangs kernel due to invalid assumption about unregister serialization
- [Medium] iio: osf: KUnit test osf_iio_buffer_lifetime_test fails due to incorrect locking assumption --
commit ca6325902957e47173f8e4aee82de054cadca7cd
Author: Jinseob Kim <[email protected]>
iio: osf: add IIO KUnit tests
This patch adds KUnit tests for the Open Sensor Fusion core sample acceptance, direct-read cache, and IIO buffer paths. It exercises active-scan packing, timestamp alignment, and producer quiescence during buffer reconfiguration and unregistration.
Quoted 7 lines
drivers/iio/sensorhubs/osf/osf_iio_test.c new file +1
-0@@ -0,0 +1,386 @@ +// SPDX-License-Identifier: GPL-2.0-only
[ ... ]
Quoted 10 lines
drivers/iio/sensorhubs/osf/osf_iio_test.c new file +10
-0+static void osf_iio_expect_quiesce (struct kunit *test,struct osf_iio_race *race) +{ +struct iio_dev *indio_dev = race->indio_dev; +const unsigned long *scan_mask = indio_dev->active_scan_mask; +unsigned long mask; +unsigned long waited; +int ret; + +KUNIT_ASSERT_NOT_NULL (test, scan_mask); + mask = *scan_mask;
drivers/iio/sensorhubs/osf/osf_iio_test.c line 262
Quoted 10 lines
drivers/iio/sensorhubs/osf/osf_iio_test.c new file +10
-0+ race->config =kthread_run (osf_iio_test_configure, race,"osf-cfg-test" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, race->config); + ret =kunit_add_action_or_reset (test, osf_iio_stop_config, race); +KUNIT_ASSERT_EQ (test, ret,0 ); +KUNIT_EXPECT_NE (test,wait_for_completion_timeout (&race->config_started,HZ ),0UL ); + waited =wait_for_completion_timeout (&race->config_done,msecs_to_jiffies (20 )); +KUNIT_EXPECT_EQ (test, waited,0UL ); +/* The blocked OSF push owns the mode claim until its store returns. */ +KUNIT_EXPECT_TRUE (test,iio_buffer_enabled (indio_dev)); +KUNIT_EXPECT_PTR_EQ (test, indio_dev->active_scan_mask, scan_mask);
[Severity: Medium]
Will these assertions fail unconditionally?
The test assumes iio_device_unregister() blocks on mlock while the producer is active. However, iio_device_unregister() only takes info_exist_lock, not mlock. It proceeds directly to disable all buffers and nullifies active_scan_mask immediately, which would cause the timeout assertion and state assertions here to fail.
[ ... ]
drivers/iio/sensorhubs/osf/osf_iio_test.c line 365
Quoted 5 lines
drivers/iio/sensorhubs/osf/osf_iio_test.c new file +5
-0+/* Publish the completions to the producer's fully ordered atomic_xchg(). */ +atomic_set_release (&race->block_store,1 ); +KUNIT_EXPECT_NE (test,wait_for_completion_timeout (&race->entered,HZ ),0UL ); + race->unregister = true; +osf_iio_expect_quiesce (test, race);
[Severity: High]
Can this sequence cause a kernel soft lockup?
When race->unregister is true, the test concurrently triggers iio_device_unregister() while the producer is actively blocked pushing a sample. Because iio_device_unregister() does not block on mlock, it removes the active buffer from buffer_list using list_del_init().
When the test unblocks the producer, it resumes in iio_push_to_buffers() inside list_for_each_entry(). Since the buffer was removed with list_del_init(), its next pointer points to itself, creating an infinite loop.
Quoted 6 lines
drivers/iio/sensorhubs/osf/osf_iio_test.c new file +6
-0+kunit_release_action (test, osf_iio_stop_producer, race); +KUNIT_EXPECT_EQ (test,atomic_read (&race->bad_store),0 ); +KUNIT_EXPECT_GT (test,atomic_read (&race->stores),0 ); +kunit_release_action (test, osf_iio_restore_access, race); +kunit_release_action (test, osf_iio_test_put_device, &indio_dev->dev); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Jonathan Cameron
maintainerQuoted 10 lines · On Mon, 12 Oct 2026 08:13:32 +0900 Jinseob Kim <[email protected]> wrote:
This series adds a UART/serdev receiver for the project's fixed OSF Device-to-Host 0.0 protocol. Complete capability snapshots create IIO accelerometer, gyroscope, magnetometer and temperature devices with RAW, descriptor-derived SCALE and initialized buffered scans.
The receiver validates the fixed OSF0 envelope, CRC and exact known message lengths. Equivalent capability reports are idempotent; changes to a bound supported inventory fault the session until explicit rebind. IIO samples use host timestamps.
Hi Jinseob,
I'm going to do something a little unusual which is more about the time I have available today and desire to look more closely at your unit tests to learn from them. So for now I'm just going to apply the driver (so patches 1-6) and leave the last two on list and in patchwork as still needing review. Hopefully I will get time to circle back to them later this kernel cycle! In general I'm keen to get broad testing into IIO so want to learn from what you have done there.
Jonathan
Quoted 97 lines
Changes since v10:
- Move OSF underdrivers/iio/sensorhubs/osf/following review.
- Use the OSF GREEN compatible with the generic OSF fallback.
- Clarify complete capability-report snapshot semantics.
- Use designated decoded-frame initialization and drop decoded reserved fields while retaining reserved wire-byte tolerance.
- Reuse stream reset and simplify core/serdev locking and lifecycle.
- Use IIO buffer-mode claims instead of a private buffer lock.
- Simplify scan storage and channel declarations.
- Fix KUnit worker/device/mutex/resource/completion lifetimes. Generic IIO self-test extraction remains deferred.
- Synchronize KUnit fault observation and configuration completion with completions and thread joins instead of short polling/deadline windows.The IIO buffer teardown serialization issue found during the v10 review was fixed separately and is now in mainline as commit
84bb0fc46ccf("iio: buffer: serialize buffer teardown with mode claims").This series is based on the IIO testing snapshot fetched for this submission:
a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
Runtime validation used this testing base plus the eight OSF patches and the exact mainline IIO core fix84bb0fc46ccf6a545b8fdf57cda83770186ed8dd. The core fix is not included in this series and must be present when running the series on this testing base.Fresh validation of this source:
- GCC and Clang W=1 kernel builds; module builds.
- osf-core (16 tests) and osf-iio (2 tests), with KASAN, lockdep,DEBUG_MUTEXES,DEBUG_LOCK_ALLOC,DEBUG_OBJECTSandDEBUG_OBJECTS_FREE.
- 200 production module load/unload cycles and 200 test-enriched module load/unload cycles using identical driver/test C sources, exercising 20,000 IIO buffer toggles, scan subsets, timestamps and unregister.
- DT binding/style/example/schema checks and IIO Sphinx build.
- Clean sequential application and relevant builds of all eight patches. Physical board tests were not repeated for this revision.Canonical specification (fixed revision):
https://github.com/opensensorfusion/opensensorfusion-protocol/blob/ca9cdea1ae550c2b4d6f29f87877adae99470744/spec/osf-d2h-0.0.mdThe fixed public specification is maintained and adopted by the OSF project. This does not imply Linux maintainer approval of the specification or driver.
Jinseob Kim (8):
dt-bindings: iio: add Open Sensor Fusion device
Documentation: iio: add Open Sensor Fusion driver overview
iio: osf: add protocol decoding
iio: osf: add validated stream parser
iio: osf: add UART transport and core receive path
iio: osf: add IIO devices from capability reports
iio: osf: add core KUnit tests
iio: osf: add IIO KUnit tests.../bindings/iio/opensensorfusion,osf.yaml | 55 + .../devicetree/bindings/vendor-prefixes.yaml | 2 + Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 81 ++ MAINTAINERS | 8 + drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/sensorhubs/Kconfig | 7 + drivers/iio/sensorhubs/Makefile | 3 + drivers/iio/sensorhubs/osf/Kconfig | 27 + drivers/iio/sensorhubs/osf/Makefile | 7 + drivers/iio/sensorhubs/osf/osf_core.c | 460 +++++++ drivers/iio/sensorhubs/osf/osf_core.h | 75 ++ drivers/iio/sensorhubs/osf/osf_core_test.c | 1056 +++++++++++++++++ drivers/iio/sensorhubs/osf/osf_iio.c | 288 +++++ drivers/iio/sensorhubs/osf/osf_iio.h | 22 + drivers/iio/sensorhubs/osf/osf_iio_test.c | 386 ++++++ drivers/iio/sensorhubs/osf/osf_protocol.c | 215 ++++ drivers/iio/sensorhubs/osf/osf_protocol.h | 99 ++ drivers/iio/sensorhubs/osf/osf_serdev.c | 151 +++ drivers/iio/sensorhubs/osf/osf_stream.c | 230 ++++ drivers/iio/sensorhubs/osf/osf_stream.h | 53 + 22 files changed, 3228 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml create mode 100644 Documentation/iio/open-sensor-fusion.rst create mode 100644 drivers/iio/sensorhubs/Kconfig create mode 100644 drivers/iio/sensorhubs/Makefile create mode 100644 drivers/iio/sensorhubs/osf/Kconfig create mode 100644 drivers/iio/sensorhubs/osf/Makefile create mode 100644 drivers/iio/sensorhubs/osf/osf_core.c create mode 100644 drivers/iio/sensorhubs/osf/osf_core.h create mode 100644 drivers/iio/sensorhubs/osf/osf_core_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.h create mode 100644 drivers/iio/sensorhubs/osf/osf_iio_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.h create mode 100644 drivers/iio/sensorhubs/osf/osf_serdev.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.hbase-commit:
a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
Hi Jonathan,
I investigated the review comments on the two pending OSF KUnit patches.
The revised Kconfig allows the tests to run when OSF is built as a module, using the existing composite driver without exporting internal symbols.
I validated the following configurations:
OSF=y, KUNIT=y
OSF=m, KUNIT=y
OSF=m, KUNIT=m
All configurations used the enabled KUnit test selector. The existing 16 core and 2 IIO tests passed, including actual module load/unload testing.
I also added deferred mutex cleanup for the pending-session, receive and IIO test fixtures, and strengthened the producer/configuration synchronization. The relevant cleanup paths were validated, including assertion-abort cases.
The unregister concurrency test depends on mainline commit 84bb0fc46ccf, which is not yet in jic23/testing. I therefore ran that test on a separate integration tree containing the fix.
I have prepared and validated an updated, signed two-patch KUnit-only series based on the already applied OSF driver patches.
Since you mentioned wanting to review the KUnit tests further, would you prefer that I send the updated two-patch series now, or wait until you have completed your review?
Thanks,
Jinseob
2026년 10월 18일 (일) 오전 9:02, Jonathan Cameron <[email protected]>님이 작성:
Quoted 124 lines
Quoted 10 lines · On Mon, 12 Oct 2026 08:13:32 +0900 Jinseob Kim <[email protected]> wrote:
This series adds a UART/serdev receiver for the project's fixed OSF Device-to-Host 0.0 protocol. Complete capability snapshots create IIO accelerometer, gyroscope, magnetometer and temperature devices with RAW, descriptor-derived SCALE and initialized buffered scans.
The receiver validates the fixed OSF0 envelope, CRC and exact known message lengths. Equivalent capability reports are idempotent; changes to a bound supported inventory fault the session until explicit rebind. IIO samples use host timestamps.
Hi Jinseob,
I'm going to do something a little unusual which is more about the time I have available today and desire to look more closely at your unit tests to learn from them. So for now I'm just going to apply the driver (so patches 1-6) and leave the last two on list and in patchwork as still needing review. Hopefully I will get time to circle back to them later this kernel cycle! In general I'm keen to get broad testing into IIO so want to learn from what you have done there.
Jonathan
Quoted 97 lines
Changes since v10:
- Move OSF underdrivers/iio/sensorhubs/osf/following review.
- Use the OSF GREEN compatible with the generic OSF fallback.
- Clarify complete capability-report snapshot semantics.
- Use designated decoded-frame initialization and drop decoded reserved fields while retaining reserved wire-byte tolerance.
- Reuse stream reset and simplify core/serdev locking and lifecycle.
- Use IIO buffer-mode claims instead of a private buffer lock.
- Simplify scan storage and channel declarations.
- Fix KUnit worker/device/mutex/resource/completion lifetimes. Generic IIO self-test extraction remains deferred.
- Synchronize KUnit fault observation and configuration completion with completions and thread joins instead of short polling/deadline windows.The IIO buffer teardown serialization issue found during the v10 review was fixed separately and is now in mainline as commit
84bb0fc46ccf("iio: buffer: serialize buffer teardown with mode claims").This series is based on the IIO testing snapshot fetched for this submission:
a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
Runtime validation used this testing base plus the eight OSF patches and the exact mainline IIO core fix84bb0fc46ccf6a545b8fdf57cda83770186ed8dd. The core fix is not included in this series and must be present when running the series on this testing base.Fresh validation of this source:
- GCC and Clang W=1 kernel builds; module builds.
- osf-core (16 tests) and osf-iio (2 tests), with KASAN, lockdep,DEBUG_MUTEXES,DEBUG_LOCK_ALLOC,DEBUG_OBJECTSandDEBUG_OBJECTS_FREE.
- 200 production module load/unload cycles and 200 test-enriched module load/unload cycles using identical driver/test C sources, exercising 20,000 IIO buffer toggles, scan subsets, timestamps and unregister.
- DT binding/style/example/schema checks and IIO Sphinx build.
- Clean sequential application and relevant builds of all eight patches. Physical board tests were not repeated for this revision.Canonical specification (fixed revision):
https://github.com/opensensorfusion/opensensorfusion-protocol/blob/ca9cdea1ae550c2b4d6f29f87877adae99470744/spec/osf-d2h-0.0.mdThe fixed public specification is maintained and adopted by the OSF project. This does not imply Linux maintainer approval of the specification or driver.
Jinseob Kim (8):
dt-bindings: iio: add Open Sensor Fusion device
Documentation: iio: add Open Sensor Fusion driver overview
iio: osf: add protocol decoding
iio: osf: add validated stream parser
iio: osf: add UART transport and core receive path
iio: osf: add IIO devices from capability reports
iio: osf: add core KUnit tests
iio: osf: add IIO KUnit tests.../bindings/iio/opensensorfusion,osf.yaml | 55 + .../devicetree/bindings/vendor-prefixes.yaml | 2 + Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 81 ++ MAINTAINERS | 8 + drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/sensorhubs/Kconfig | 7 + drivers/iio/sensorhubs/Makefile | 3 + drivers/iio/sensorhubs/osf/Kconfig | 27 + drivers/iio/sensorhubs/osf/Makefile | 7 + drivers/iio/sensorhubs/osf/osf_core.c | 460 +++++++ drivers/iio/sensorhubs/osf/osf_core.h | 75 ++ drivers/iio/sensorhubs/osf/osf_core_test.c | 1056 +++++++++++++++++ drivers/iio/sensorhubs/osf/osf_iio.c | 288 +++++ drivers/iio/sensorhubs/osf/osf_iio.h | 22 + drivers/iio/sensorhubs/osf/osf_iio_test.c | 386 ++++++ drivers/iio/sensorhubs/osf/osf_protocol.c | 215 ++++ drivers/iio/sensorhubs/osf/osf_protocol.h | 99 ++ drivers/iio/sensorhubs/osf/osf_serdev.c | 151 +++ drivers/iio/sensorhubs/osf/osf_stream.c | 230 ++++ drivers/iio/sensorhubs/osf/osf_stream.h | 53 + 22 files changed, 3228 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml create mode 100644 Documentation/iio/open-sensor-fusion.rst create mode 100644 drivers/iio/sensorhubs/Kconfig create mode 100644 drivers/iio/sensorhubs/Makefile create mode 100644 drivers/iio/sensorhubs/osf/Kconfig create mode 100644 drivers/iio/sensorhubs/osf/Makefile create mode 100644 drivers/iio/sensorhubs/osf/osf_core.c create mode 100644 drivers/iio/sensorhubs/osf/osf_core.h create mode 100644 drivers/iio/sensorhubs/osf/osf_core_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.h create mode 100644 drivers/iio/sensorhubs/osf/osf_iio_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.h create mode 100644 drivers/iio/sensorhubs/osf/osf_serdev.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.hbase-commit:
a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
Jonathan Cameron
maintainerQuoted 29 lines · On Sun, 18 Oct 2026 18:46:56 +0900 Kim Jinseob <[email protected]> wrote:
Hi Jonathan,
I investigated the review comments on the two pending OSF KUnit patches.
The revised
Kconfigallows the tests to run when OSF is built as a module, using the existing composite driver without exporting internal symbols.I validated the following configurations:
OSF=y, KUNIT=y
OSF=m, KUNIT=y
OSF=m, KUNIT=m
All configurations used the enabled KUnit test selector. The existing 16 core and 2 IIO tests passed, including actual module load/unload testing.
I also added deferred mutex cleanup for the pending-session, receive and IIO test fixtures, and strengthened the producer/configuration synchronization. The relevant cleanup paths were validated, including assertion-abort cases.
The unregister concurrency test depends on mainline commit
84bb0fc46ccf, which is not yet in jic23/testing. I therefore ran that test on a separate integration tree containing the fix.
Quoted 6 lines
I have prepared and validated an updated, signed two-patch KUnit-only series based on the already applied OSF driver patches.
Since you mentioned wanting to review the KUnit tests further, would you prefer that I send the updated two-patch series now, or wait until you have completed your review?
If you have them ready go ahead and send the updated ones. I'll mark this versions as superceeded in patchwork.
Thanks
Jonathan
Quoted 131 lines
Thanks,
Jinseob
2026년 10월 18일 (일) 오전 9:02, Jonathan Cameron <[email protected]>님이 작성:
Quoted 124 lines
Quoted 10 lines · On Mon, 12 Oct 2026 08:13:32 +0900 Jinseob Kim <[email protected]> wrote:
This series adds a UART/serdev receiver for the project's fixed OSF Device-to-Host 0.0 protocol. Complete capability snapshots create IIO accelerometer, gyroscope, magnetometer and temperature devices with RAW, descriptor-derived SCALE and initialized buffered scans.
The receiver validates the fixed OSF0 envelope, CRC and exact known message lengths. Equivalent capability reports are idempotent; changes to a bound supported inventory fault the session until explicit rebind. IIO samples use host timestamps.
Hi Jinseob,
I'm going to do something a little unusual which is more about the time I have available today and desire to look more closely at your unit tests to learn from them. So for now I'm just going to apply the driver (so patches 1-6) and leave the last two on list and in patchwork as still needing review. Hopefully I will get time to circle back to them later this kernel cycle! In general I'm keen to get broad testing into IIO so want to learn from what you have done there.
Jonathan
Quoted 97 lines
Changes since v10:
- Move OSF underdrivers/iio/sensorhubs/osf/following review.
- Use the OSF GREEN compatible with the generic OSF fallback.
- Clarify complete capability-report snapshot semantics.
- Use designated decoded-frame initialization and drop decoded reserved fields while retaining reserved wire-byte tolerance.
- Reuse stream reset and simplify core/serdev locking and lifecycle.
- Use IIO buffer-mode claims instead of a private buffer lock.
- Simplify scan storage and channel declarations.
- Fix KUnit worker/device/mutex/resource/completion lifetimes. Generic IIO self-test extraction remains deferred.
- Synchronize KUnit fault observation and configuration completion with completions and thread joins instead of short polling/deadline windows.The IIO buffer teardown serialization issue found during the v10 review was fixed separately and is now in mainline as commit
84bb0fc46ccf("iio: buffer: serialize buffer teardown with mode claims").This series is based on the IIO testing snapshot fetched for this submission:
a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7
Runtime validation used this testing base plus the eight OSF patches and the exact mainline IIO core fix84bb0fc46ccf6a545b8fdf57cda83770186ed8dd. The core fix is not included in this series and must be present when running the series on this testing base.Fresh validation of this source:
- GCC and Clang W=1 kernel builds; module builds.
- osf-core (16 tests) and osf-iio (2 tests), with KASAN, lockdep,DEBUG_MUTEXES,DEBUG_LOCK_ALLOC,DEBUG_OBJECTSandDEBUG_OBJECTS_FREE.
- 200 production module load/unload cycles and 200 test-enriched module load/unload cycles using identical driver/test C sources, exercising 20,000 IIO buffer toggles, scan subsets, timestamps and unregister.
- DT binding/style/example/schema checks and IIO Sphinx build.
- Clean sequential application and relevant builds of all eight patches. Physical board tests were not repeated for this revision.Canonical specification (fixed revision):
https://github.com/opensensorfusion/opensensorfusion-protocol/blob/ca9cdea1ae550c2b4d6f29f87877adae99470744/spec/osf-d2h-0.0.mdThe fixed public specification is maintained and adopted by the OSF project. This does not imply Linux maintainer approval of the specification or driver.
Jinseob Kim (8):
dt-bindings: iio: add Open Sensor Fusion device
Documentation: iio: add Open Sensor Fusion driver overview
iio: osf: add protocol decoding
iio: osf: add validated stream parser
iio: osf: add UART transport and core receive path
iio: osf: add IIO devices from capability reports
iio: osf: add core KUnit tests
iio: osf: add IIO KUnit tests.../bindings/iio/opensensorfusion,osf.yaml | 55 + .../devicetree/bindings/vendor-prefixes.yaml | 2 + Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 81 ++ MAINTAINERS | 8 + drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/sensorhubs/Kconfig | 7 + drivers/iio/sensorhubs/Makefile | 3 + drivers/iio/sensorhubs/osf/Kconfig | 27 + drivers/iio/sensorhubs/osf/Makefile | 7 + drivers/iio/sensorhubs/osf/osf_core.c | 460 +++++++ drivers/iio/sensorhubs/osf/osf_core.h | 75 ++ drivers/iio/sensorhubs/osf/osf_core_test.c | 1056 +++++++++++++++++ drivers/iio/sensorhubs/osf/osf_iio.c | 288 +++++ drivers/iio/sensorhubs/osf/osf_iio.h | 22 + drivers/iio/sensorhubs/osf/osf_iio_test.c | 386 ++++++ drivers/iio/sensorhubs/osf/osf_protocol.c | 215 ++++ drivers/iio/sensorhubs/osf/osf_protocol.h | 99 ++ drivers/iio/sensorhubs/osf/osf_serdev.c | 151 +++ drivers/iio/sensorhubs/osf/osf_stream.c | 230 ++++ drivers/iio/sensorhubs/osf/osf_stream.h | 53 + 22 files changed, 3228 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml create mode 100644 Documentation/iio/open-sensor-fusion.rst create mode 100644 drivers/iio/sensorhubs/Kconfig create mode 100644 drivers/iio/sensorhubs/Makefile create mode 100644 drivers/iio/sensorhubs/osf/Kconfig create mode 100644 drivers/iio/sensorhubs/osf/Makefile create mode 100644 drivers/iio/sensorhubs/osf/osf_core.c create mode 100644 drivers/iio/sensorhubs/osf/osf_core.h create mode 100644 drivers/iio/sensorhubs/osf/osf_core_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.c create mode 100644 drivers/iio/sensorhubs/osf/osf_iio.h create mode 100644 drivers/iio/sensorhubs/osf/osf_iio_test.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.c create mode 100644 drivers/iio/sensorhubs/osf/osf_protocol.h create mode 100644 drivers/iio/sensorhubs/osf/osf_serdev.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.c create mode 100644 drivers/iio/sensorhubs/osf/osf_stream.hbase-commit:
a22dc7ebb21a6bd4c8b63bc2c1dd46a24945aae7