iio: add Open Sensor Fusion IIO driver
Subsystems (3)
v5 cover iio: add Open Sensor Fusion IIO driver
Open Sensor Fusion is a sensor aggregation hub interface. The Linux IIO driver receives OSF protocol frames from a serdev-attached device, discovers supported sensor streams from capability reports, and exposes the supported raw sensor data through IIO devices.
The initial driver supports protocol major version 0 and the receive path for accelerometer, gyroscope, magnetometer, and temperature samples. The current wire magic is OSF0, but OSF0 is a wire-format detail and not the Linux driver identity. Protocol compatibility is carried by the protocol_major and protocol_minor fields in the fixed OSF frame header.
This is still RFC because the driver-facing OSF protocol subset, the compatible binding, and future protocol compatibility rules are being reviewed.
Runtime testing was done with an OSF GREEN prototype connected to a Raspberry Pi over UART. The driver registered osf-accel, osf-gyro, osf-magn, and osf-temp IIO devices. Direct raw reads and software kfifo buffer reads were tested.
Changes since v4:
- Regenerated the series as a full standalone replacement series from a clean upstream base.
- Removed previous-version add/delete churn from the generated series.
- Clarified OSF0, protocol_major, and protocol_minor compatibility handling.
- Added required vcc-supply support to the binding.
- Added probe-time regulator enablement with devm_regulator_get_enable().
- Added the opensensorfusion vendor prefix.
- Fixed checkpatch cleanup issues in commit messages and driver style.
Jinseob Kim (6):
dt-bindings: iio: add Open Sensor Fusion device
Documentation: iio: add Open Sensor Fusion driver overview
iio: osf: add protocol decoding
iio: osf: add stream parser
iio: osf: add UART transport
iio: osf: register IIO devices from capabilities
.../bindings/iio/opensensorfusion,osf.yaml | 59 ++++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 71 ++++ MAINTAINERS | 13 + drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/opensensorfusion/Kconfig | 14 + drivers/iio/opensensorfusion/Makefile | 6 + drivers/iio/opensensorfusion/osf_core.c | 306 ++++++++++++++++++ drivers/iio/opensensorfusion/osf_core.h | 70 ++++ drivers/iio/opensensorfusion/osf_iio.c | 275 ++++++++++++++++ drivers/iio/opensensorfusion/osf_iio.h | 22 ++ drivers/iio/opensensorfusion/osf_protocol.c | 249 ++++++++++++++ drivers/iio/opensensorfusion/osf_protocol.h | 97 ++++++ drivers/iio/opensensorfusion/osf_serdev.c | 117 +++++++ drivers/iio/opensensorfusion/osf_stream.c | 187 +++++++++++ drivers/iio/opensensorfusion/osf_stream.h | 31 ++ 18 files changed, 1522 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/opensensorfusion/Kconfig create mode 100644 drivers/iio/opensensorfusion/Makefile create mode 100644 drivers/iio/opensensorfusion/osf_core.c create mode 100644 drivers/iio/opensensorfusion/osf_core.h create mode 100644 drivers/iio/opensensorfusion/osf_iio.c create mode 100644 drivers/iio/opensensorfusion/osf_iio.h create mode 100644 drivers/iio/opensensorfusion/osf_protocol.c create mode 100644 drivers/iio/opensensorfusion/osf_protocol.h create mode 100644 drivers/iio/opensensorfusion/osf_serdev.c create mode 100644 drivers/iio/opensensorfusion/osf_stream.c create mode 100644 drivers/iio/opensensorfusion/osf_stream.h
Signature · 1 line
2.43.0
RFC PATCH v5 1/6 dt-bindings: iio: add Open Sensor Fusion device
Add the generic Open Sensor Fusion device binding for a serdev-attached IIO sensor aggregation hub, and document the opensensorfusion vendor prefix.
The opensensorfusion,osf compatible describes the generic Open Sensor Fusion host interface. OSF GREEN is not the Linux compatible identity. Likewise, OSF0 is the current wire magic and a wire-format detail, not the Linux driver identity.
The fixed OSF frame header carries protocol_major and protocol_minor at fixed offsets. This driver currently supports protocol_major 0. protocol_minor changes are intended to remain backward-compatible within that fixed header layout. Incompatible wire-format changes require a new protocol_major. If a future device cannot expose compatible version discovery through the fixed header layout, it will need a different compatible.
Require vcc-supply so the driver can enable device power before starting communication.
--- .../bindings/iio/opensensorfusion,osf.yaml | 59 +++++++++++++++++++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 13 ++++ 3 files changed, 74 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +59 -0
@@ -0,0 +1,59 @@ +# 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: | + Open Sensor Fusion is a sensor aggregation hub. The hub exposes an OSF + protocol data stream over its host interface and may report capabilities and + samples for multiple sensor classes. The Linux driver discovers the actual + sensor channels from OSF capability reports instead of describing those + sensors in Device Tree. + + Open Sensor Fusion is not a generic industry standard. Public project + documentation is available at: + + https://github.com/opensensorfusion + + The compatible describes the generic Open Sensor Fusion host interface. It + is not an OSF GREEN board identity, and it does not encode the OSF0 wire + magic. OSF0, protocol_major, and protocol_minor are wire-protocol details + exchanged in OSF frames. + +allOf: + - $ref: /schemas/serial/serial-peripheral-props.yaml# + +properties: + compatible: + const: opensensorfusion,osf + + vcc-supply: + description: + Regulator supplying power to the Open Sensor Fusion device. + +required: + - compatible + - vcc-supply + +unevaluatedProperties: false + +examples: + - | + vcc_sensor: regulator-0 { + compatible = "regulator-fixed"; + regulator-name = "sensor-vcc"; + }; + + serial { + sensor { + compatible = "opensensorfusion,osf"; + vcc-supply = <&vcc_sensor>; + }; + }; +...
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -1237,6 +1237,8 @@ patternProperties: description: OpenPandora GmbH "^openrisc,.*": description: OpenRISC.io + "^opensensorfusion,.*": + description: Open Sensor Fusion "^openwrt,.*": description: OpenWrt "^option,.*":
MAINTAINERS +13 -0
@@ -20011,6 +20011,19 @@ F: Documentation/devicetree/ F: arch/*/boot/dts/ F: include/dt-bindings/ +OPEN SENSOR FUSION IIO DRIVER +M: Jinseob Kim <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml +F: Documentation/iio/open-sensor-fusion.rst +F: drivers/iio/opensensorfusion/Kconfig +F: drivers/iio/opensensorfusion/Makefile +F: drivers/iio/opensensorfusion/osf_core.* +F: drivers/iio/opensensorfusion/osf_iio.* +F: drivers/iio/opensensorfusion/osf_protocol.* +F: drivers/iio/opensensorfusion/osf_serdev.c +F: drivers/iio/opensensorfusion/osf_stream.* + OPENCOMPUTE PTP CLOCK DRIVER M: Vadim Fedorenko <[email protected]> L: [email protected]
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH RFC v5 1/6] dt-bindings: iio: add Open Sensor Fusion device
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The binding uses a generic software protocol as the compatible string instead of describing…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The binding uses a generic software protocol as the compatible string instead of describing specific hardware. --
commit d9858298f5a88a8d0d1665f79878488487e06393
Author: Jinseob Kim <[email protected]>
dt-bindings: iio: add Open Sensor Fusion device
Adds the generic Open Sensor Fusion device binding for a serdev-attached IIO sensor aggregation hub and documents the vendor prefix.
Quoted 4 lines
The opensensorfusion,osf compatible describes the generic Open Sensor Fusion host interface. OSF GREEN is not the Linux compatible identity. Likewise, OSF0 is the current wire magic and a wire-format detail, not the Linux driver identity.
[Severity: Medium]
Does this approach align with Device Tree design principles?
Device Tree is typically intended to describe specific hardware components (such as vendor,chip-model), rather than software interfaces or generic wire protocols.
Quoted 5 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +0
-0
[ ... ]
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 42
Quoted 11 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +11
-0+properties: + compatible: + const: opensensorfusion,osf + + vcc-supply: + description: + Regulator supplying power to the Open Sensor Fusion device. + +required: + - compatible + - vcc-supply
[Severity: Medium]
Is it appropriate to constrain the compatible string to a generic software protocol identifier here instead of a hardware-specific string?
Because this generic compatible string also requires specific hardware constraints like vcc-supply, will future hardware variants that speak the same OSF protocol but have different electrical needs (such as needing a vddio-supply or reset GPIOs) be able to distinguish themselves without clashing with these generic constraints?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH RFC v5 1/6] dt-bindings: iio: add Open Sensor Fusion device
In reply to Jinseob KimQuoted 38 lines · Jinseob Kim
Add the generic Open Sensor Fusion device binding for a serdev-attached IIO sensor aggregation hub, and document the opensensorfusion vendor prefix.
The opensensorfusion,osf compatible describes the generic Open Sensor Fusion host interface. OSF GREEN is not the Linux compatible identity. Likewise, OSF0 is the current wire magic and a wire-format detail, not the Linux driver identity.
The fixed OSF frame header carries
protocol_majorandprotocol_minorat fixed offsets. This driver currently supportsprotocol_major0.protocol_minorchanges are intended to remain backward-compatible within that fixed header layout. Incompatible wire-format changes require a newprotocol_major. If a future device cannot expose compatible version discovery through the fixed header layout, it will need a different compatible.Require vcc-supply so the driver can enable device power before starting communication.
Signed-off-by Jinseob Kim <[email protected]>--- .../bindings/iio/opensensorfusion,osf.yaml | 59 +++++++++++++++++++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 13 ++++ 3 files changed, 74 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/opensensorfusion,osf.yamlDocumentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +4
-0@@ -0,0 +1,59 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/opensensorfusion,osf.yaml#
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 14
Quoted 10 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +10
-0+$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Open Sensor Fusion Sensor Aggregation Hub + +maintainers: + - Jinseob Kim <[email protected]> + +description: | + Open Sensor Fusion is a sensor aggregation hub. The hub exposes an OSF + protocol data stream over its host interface and may report capabilities and
s/may report/reports/ because mandatory reporting, even if nothing is actually there, is a requirement for having a compatible describing the "bus".
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 15
Quoted 1 line
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +1
-0+ samples for multiple sensor classes. The Linux driver discovers the actual
s/The Linux driver discovers the actual sensor channels/The actual sensor channels are discovered" Because although maybe only linux will use this it should not be
specific.
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 19
Quoted 4 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +4
-0+ sensor channels from OSF capability reports instead of describing those + sensors in Device Tree. + + Open Sensor Fusion is not a generic industry standard. Public project
I would drop this first sentence to be honest.
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 27
Quoted 8 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +8
-0+ documentation is available at: + + https://github.com/opensensorfusion + + The compatible describes the generic Open Sensor Fusion host interface. It + is not an OSF GREEN board identity, and it does not encode the OSF0 wire + magic. OSF0, protocol_major, and protocol_minor are wire-protocol details + exchanged in OSF frames.
I think I move this to the first paragraph and would say something like
| This binding documents the generic Open Sensor Fusion host interface. | | Open Sensor Fusion is a sensor aggregation hub. The hub exposes an OSF | protocol data stream over its host interface and reports capabilities and | samples for multiple sensor classes. The actual sensor channels are discovered | at runtime from OSF capability reports instead of describing them in Device | Tree. | | The protocol version is discovered at runtime.
Does that sound about right?
Quoted 14 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +14
-0+ +allOf: + - $ref: /schemas/serial/serial-peripheral-props.yaml# + +properties: + compatible: + const: opensensorfusion,osf + + vcc-supply: + description: + Regulator supplying power to the Open Sensor Fusion device. + +required: + - compatible
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml line 51
Quoted 10 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +10
-0+ - vcc-supply + +unevaluatedProperties: false + +examples: + - | + vcc_sensor: regulator-0 { + compatible = "regulator-fixed"; + regulator-name = "sensor-vcc"; + };
Drop this node, the tooling fakes one when running the checks. Only keep nodes that actually form your device here. Same way you don't need to actually fill out the serial port.
pw-bot: changes-requested
Quoted 32 lines
Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml new file +8
-0+ + serial { + sensor { + compatible = "opensensorfusion,osf"; + vcc-supply = <&vcc_sensor>; + }; + }; +...Documentation/devicetree/bindings/vendor-prefixes.yaml +2
-0@@ -1237,6 +1237,8 @@ patternProperties: description: OpenPandora GmbH "^openrisc,.*": description: OpenRISC.io + "^opensensorfusion,.*": + description: Open Sensor Fusion "^openwrt,.*": description: OpenWrt "^option,.*":MAINTAINERS +2
-0@@ -20011,6 +20011,19 @@ F: Documentation/devicetree/ F: arch/*/boot/dts/ F: include/dt-bindings/ +OPEN SENSOR FUSION IIO DRIVER +M: Jinseob Kim <[email protected]>
MAINTAINERS F: Documentation/devicetree/ · line 20026
Quoted 10 lines
MAINTAINERS +10
-0+S: Maintained +F: Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml +F: Documentation/iio/open-sensor-fusion.rst +F: drivers/iio/opensensorfusion/Kconfig +F: drivers/iio/opensensorfusion/Makefile +F: drivers/iio/opensensorfusion/osf_core.* +F: drivers/iio/opensensorfusion/osf_iio.* +F: drivers/iio/opensensorfusion/osf_protocol.* +F: drivers/iio/opensensorfusion/osf_serdev.c +F: drivers/iio/opensensorfusion/osf_stream.*
At this stage, only add the binding. The rest should be added when the files are.
Cheers,
Conor.
Quoted 7 lines
MAINTAINERS +1
-0+ OPENCOMPUTE PTP CLOCK DRIVER M: Vadim Fedorenko <[email protected]>Signature · 1 line
2.43.0
RFC PATCH v5 2/6 Documentation: iio: add Open Sensor Fusion driver overview
In reply to Jinseob KimDocument the Linux IIO mapping for Open Sensor Fusion devices, including capability-driven IIO device registration and the initially supported receive path.
Call out that OSF0 is a wire magic value, while protocol_major and protocol_minor carry protocol compatibility inside frames. The Linux compatible remains the generic Open Sensor Fusion host interface.
--- Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 71 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Documentation/iio/open-sensor-fusion.rst
Documentation/iio/index.rst +1 -0
@@ -38,4 +38,5 @@ Industrial I/O Kernel Drivers adxl345 bno055 ep93xx_adc + open-sensor-fusion opt4060
Documentation/iio/open-sensor-fusion.rst new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH RFC v5 2/6] Documentation: iio: add Open Sensor Fusion driver overview
Quoted 9 lines · On Tue, 16 Jun 2026 16:22:38 +0900 Jinseob Kim <[email protected]> wrote:
Document the Linux IIO mapping for Open Sensor Fusion devices, including capability-driven IIO device registration and the initially supported receive path.
Call out that OSF0 is a wire magic value, while
protocol_majorandprotocol_minorcarry protocol compatibility inside frames. The Linux compatible remains the generic Open Sensor Fusion host interface.Signed-off-by Jinseob Kim <[email protected]>
Just one small thing inline.
Otherwise this look good to me.
Thanks,
Jonathan
Quoted 49 lines
--- Documentation/iio/index.rst | 1 + Documentation/iio/open-sensor-fusion.rst | 71 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Documentation/iio/open-sensor-fusion.rstDocumentation/iio/index.rst +1
-0@@ -38,4 +38,5 @@ Industrial I/O Kernel Drivers adxl345 bno055 ep93xx_adc + open-sensor-fusion opt4060Documentation/iio/open-sensor-fusion.rst new file +27
-0@@ -0,0 +1,71 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +Open Sensor Fusion +================== + +Open Sensor Fusion is a sensor aggregation hub interface. The Linux IIO driver +receives OSF protocol frames from an attached device, discovers supported sensor +streams through capability reports, and registers matching IIO devices for the +sensor classes supported by the driver. + +This document is a driver-facing overview for the Linux IIO mapping. The full +wire protocol, firmware behavior, and hardware model details belong in the Open +Sensor Fusion project documentation. + +Device Model +------------ + +An OSF device sends binary frames from the device to the host. The host driver +uses ``CAPABILITY_REPORT`` messages to discover which sensor streams are +available. Device Tree describes the attached OSF sensor aggregation hub; it does +not enumerate the individual sensors discovered at runtime. + +The currently supported Linux subset exposes: + +* accelerometer samples as ``IIO_ACCEL`` X/Y/Z channels, +* gyroscope samples as ``IIO_ANGL_VEL`` X/Y/Z channels, +* magnetometer samples as ``IIO_MAGN`` X/Y/Z channels, and
Documentation/iio/open-sensor-fusion.rst line 37
Quoted 10 lines
Documentation/iio/open-sensor-fusion.rst new file +10
-0+* temperature samples as ``IIO_TEMP``. + +Protocol Scope +--------------- + +The driver supports OSF protocol major version 0 for the initial IIO receive +path. The current wire magic is ``OSF0``; that string is a wire-format detail and +is not the Linux driver identity. Device Tree keeps the generic +``opensensorfusion,osf`` compatible rather than naming a product such as OSF +GREEN or a wire magic value.
I'd remove anything that is documented elsewhere (i.e. the dt-binding) and instead use a cross reference to it. That will reduce the chance of this getting out of sync.
Quoted 34 lines
Documentation/iio/open-sensor-fusion.rst new file +34
-0+ +Protocol versioning is carried by the ``protocol_major`` and ``protocol_minor`` +fields at fixed offsets in the OSF frame header. The driver currently +supports ``protocol_major`` 0. ``protocol_minor`` changes within major version +0 are intended to remain backward-compatible within the fixed header layout. +Incompatible wire-format changes require a new ``protocol_major``. A future +device that cannot expose compatible version discovery through that fixed +header layout would need a different Device Tree compatible. + +The initial Linux driver handles device-to-host frames for: + +* ``SENSOR_SAMPLE`` buffered and direct-mode sample data, +* ``CAPABILITY_REPORT`` based IIO device registration, and +* ``DEVICE_STATUS`` cache updates. + +Vendor-private message types are ignored. Command transport, calibration +control ABI, fusion output ABI, and runtime capability removal are outside the +initial Linux IIO receive path. + +Timestamps +---------- + +OSF frames include a device-side ``timestamp_us`` field. Buffered IIO samples use +an IIO timestamp captured on the host when samples are pushed to IIO buffers. +The initial driver does not correlate the device timestamp with the host IIO +clock. + +Compatibility Notes +------------------- + +The project protocol documentation should define the compatibility rules for +reserved fields, optional flags, and trailing extension data. Until those rules +are finalized, the Linux decoder keeps conservative bounds checks around the +currently supported message layouts.
RFC PATCH v5 3/6 iio: osf: add protocol decoding
In reply to Jinseob KimAdd helpers for validating and decoding Open Sensor Fusion frames and the message payloads used by the initial receive path.
--- drivers/iio/opensensorfusion/osf_protocol.c | 249 ++++++++++++++++++++ drivers/iio/opensensorfusion/osf_protocol.h | 97 ++++++++ 2 files changed, 346 insertions(+) create mode 100644 drivers/iio/opensensorfusion/osf_protocol.c create mode 100644 drivers/iio/opensensorfusion/osf_protocol.h
drivers/iio/opensensorfusion/osf_protocol.c new file +249 -0
@@ -0,0 +1,249 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/bits.h> +#include <linux/crc32.h> +#include <linux/errno.h> +#include <linux/limits.h> +#include <linux/types.h> +#include <linux/unaligned.h> + +#include "osf_protocol.h" + +#define OSF_CRC32_INIT GENMASK(31, 0) +#define OSF_CRC32_XOROUT GENMASK(31, 0) +#define OSF_FRAME_MAGIC 0x3046534f/* "OSF0" little-endian */ + +static bool osf_sensor_type_valid (u16 sensor_type) +{ +return sensor_type >=OSF_SENSOR_ACCELEROMETER && + sensor_type <=OSF_SENSOR_PROXIMITY ; +} + +static u32 osf_crc32_ieee (const u8 *buf,size_t len) +{ +return crc32_le (OSF_CRC32_INIT , buf, len) ^OSF_CRC32_XOROUT ; +} + +int osf_protocol_decode_frame (const u8 *buf,size_t len, +struct osf_frame *frame,size_t *frame_len) +{ +u32 expected_crc; +u32 actual_crc; +u32 payload_len; +size_t total_len; +u8 major; + +if (!buf || !frame || !frame_len) +return -EINVAL ; + +if (len <OSF_FRAME_MIN_LEN ) +return -EMSGSIZE ; + +if (get_unaligned_le32 (buf) !=OSF_FRAME_MAGIC ) +return -EPROTO ; + + major = buf[4 ]; +if (major !=OSF_PROTOCOL_MAJOR ) +return -EPROTO ; + +if (get_unaligned_le16 (buf +6 ) !=OSF_FRAME_HEADER_LEN ) +return -EPROTO ; + + payload_len =get_unaligned_le32 (buf +10 ); +if (payload_len > len -OSF_FRAME_MIN_LEN ) +return -EMSGSIZE ; + +if (get_unaligned_le32 (buf +34 )) +return -EPROTO ; + + total_len =OSF_FRAME_HEADER_LEN + payload_len +OSF_FRAME_CRC_LEN ; + expected_crc =osf_crc32_ieee (buf,OSF_FRAME_HEADER_LEN + payload_len); + actual_crc =get_unaligned_le32 (buf +OSF_FRAME_HEADER_LEN + payload_len); + +if (actual_crc != expected_crc) +return -EBADMSG ; + + frame->protocol_minor = buf[5 ]; + frame->message_type =get_unaligned_le16 (buf +8 ); + frame->payload_len = payload_len; + frame->sequence =get_unaligned_le64 (buf +14 ); + frame->timestamp_us =get_unaligned_le64 (buf +22 ); + frame->flags =get_unaligned_le32 (buf +30 ); + frame->payload = buf +OSF_FRAME_HEADER_LEN ; + frame->crc = actual_crc; + *frame_len = total_len; + +return 0 ; +} + +int osf_protocol_decode_sensor_sample (const struct osf_frame *frame, +struct osf_sensor_sample *sample) +{ +u16 channel_count; +u16 sample_format; +u16 sensor_type; +size_t expected_len; +const u8 *payload; + +if (!frame || !sample || !frame->payload) +return -EINVAL ; + +if (frame->message_type !=OSF_MSG_SENSOR_SAMPLE ) +return -EPROTO ; + +if (frame->payload_len <OSF_SENSOR_SAMPLE_BASE_LEN ) +return -EMSGSIZE ; + + payload = frame->payload; + sensor_type =get_unaligned_le16 (payload); + channel_count =get_unaligned_le16 (payload +4 ); + sample_format =get_unaligned_le16 (payload +6 ); + +if (!osf_sensor_type_valid (sensor_type)) +return -EPROTO ; + +if (!channel_count) +return -EPROTO ; + +if (sample_format !=OSF_SAMPLE_FORMAT_S32 ) +return -EPROTO ; + +if (get_unaligned_le32 (payload +12 )) +return -EPROTO ; + +if (channel_count > (SIZE_MAX -OSF_SENSOR_SAMPLE_BASE_LEN ) /sizeof (s32)) +return -EOVERFLOW ; + + expected_len =OSF_SENSOR_SAMPLE_BASE_LEN + channel_count *sizeof (s32); +if (frame->payload_len != expected_len) +return -EMSGSIZE ; + + sample->sensor_type = sensor_type; + sample->sensor_index =get_unaligned_le16 (payload +2 ); + sample->channel_count = channel_count; + sample->sample_format = sample_format; + sample->scale_nano =get_unaligned_le32 (payload +8 ); + sample->samples = payload +OSF_SENSOR_SAMPLE_BASE_LEN ; + +return 0 ; +} + +int osf_protocol_sensor_sample_value (const struct osf_sensor_sample *sample, +unsigned int index,s32 *value) +{ +if (!sample || !sample->samples || !value) +return -EINVAL ; + +if (index >= sample->channel_count) +return -ERANGE ; + +/* Samples are little-endian two's-complement signed values. */ + *value = (s32 )get_unaligned_le32 (sample->samples + index *sizeof (s32)); + +return 0 ; +} + +int osf_protocol_decode_device_status (const struct osf_frame *frame, +struct osf_device_status *status) +{ +const u8 *payload; + +if (!frame || !status || !frame->payload) +return -EINVAL ; + +if (frame->message_type !=OSF_MSG_DEVICE_STATUS ) +return -EPROTO ; + +if (frame->payload_len !=OSF_DEVICE_STATUS_LEN ) +return -EMSGSIZE ; + + payload = frame->payload; +if (get_unaligned_le32 (payload +16 )) +return -EPROTO ; + + status->uptime_s =get_unaligned_le32 (payload); + status->status_flags =get_unaligned_le32 (payload +4 ); + status->error_flags =get_unaligned_le32 (payload +8 ); + status->dropped_frames =get_unaligned_le32 (payload +12 ); + +return 0 ; +} + +int osf_protocol_decode_capability_report (const struct osf_frame *frame, +struct osf_capability_report *report) +{ +u16 capability_count; +size_t expected_len; +const u8 *payload; + +if (!frame || !report || !frame->payload) +return -EINVAL ; + +if (frame->message_type !=OSF_MSG_CAPABILITY_REPORT ) +return -EPROTO ; + +if (frame->payload_len <OSF_CAP_REPORT_BASE_LEN ) +return -EMSGSIZE ; + + payload = frame->payload; + capability_count =get_unaligned_le16 (payload); + +if (get_unaligned_le16 (payload +2 )) +return -EPROTO ; + +if (capability_count > (SIZE_MAX -OSF_CAP_REPORT_BASE_LEN ) / +OSF_CAP_SENSOR_ENTRY_LEN ) +return -EOVERFLOW ; + + expected_len =OSF_CAP_REPORT_BASE_LEN + + capability_count *OSF_CAP_SENSOR_ENTRY_LEN ; +if (frame->payload_len != expected_len) +return -EMSGSIZE ; + + report->capability_count = capability_count; + report->entries = payload +OSF_CAP_REPORT_BASE_LEN ; + +return 0 ; +} + +int osf_protocol_decode_capability_entry (const struct osf_capability_report *report, +unsigned int index, +struct osf_capability_entry *entry) +{ +u16 sample_format; +u16 sensor_type; +u32 flags; +const u8 *payload; + +if (!report || !report->entries || !entry) +return -EINVAL ; + +if (index >= report->capability_count) +return -ERANGE ; + + payload = report->entries + index *OSF_CAP_SENSOR_ENTRY_LEN ; + sensor_type =get_unaligned_le16 (payload); + sample_format =get_unaligned_le16 (payload +6 ); + flags =get_unaligned_le32 (payload +12 ); + +if (!osf_sensor_type_valid (sensor_type)) +return -EPROTO ; + +if (sample_format !=OSF_SAMPLE_FORMAT_S32 ) +return -EPROTO ; + +if (flags & ~OSF_CAPABILITY_FLAGS_MASK ) +return -EPROTO ; + +if (get_unaligned_le32 (payload +16 )) +return -EPROTO ; + + entry->sensor_type = sensor_type; + entry->sensor_index =get_unaligned_le16 (payload +2 ); + entry->channel_count =get_unaligned_le16 (payload +4 ); + entry->sample_format = sample_format; + entry->scale_nano =get_unaligned_le32 (payload +8 ); + entry->flags = flags; + +return 0 ; +}
drivers/iio/opensensorfusion/osf_protocol.h new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH RFC v5 3/6] iio: osf: add protocol decoding
Quoted 2 lines · Jinseob Kim
Add helpers for validating and decoding Open Sensor Fusion frames and the message payloads used by the initial receive path.
...
drivers/iio/opensensorfusion/osf_protocol.c line 141
Quoted 11 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +11
-0+int osf_protocol_sensor_sample_value (const struct osf_sensor_sample *sample, +unsigned int index,s32 *value) +{ +if (!sample || !sample->samples || !value) +return -EINVAL ; + +if (index >= sample->channel_count) +return -ERANGE ; + +/* Samples are little-endian two's-complement signed values. */ + *value = (s32 )get_unaligned_le32 (sample->samples + index *sizeof (s32));
This casting does not add anything.
Quoted 2 lines
(quoted diff) +2
-0+ return 0; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH RFC v5 3/6] iio: osf: add protocol decoding
In reply to Jinseob KimQuoted 4 lines · On Tue, 16 Jun 2026 16:22:39 +0900 Jinseob Kim <[email protected]> wrote:
Add helpers for validating and decoding Open Sensor Fusion frames and the message payloads used by the initial receive path.
Signed-off-by Jinseob Kim <[email protected]>
A few things inline.
Quoted 50 lines
--- drivers/iio/opensensorfusion/osf_protocol.c | 249 ++++++++++++++++++++ drivers/iio/opensensorfusion/osf_protocol.h | 97 ++++++++ 2 files changed, 346 insertions(+) create mode 100644 drivers/iio/opensensorfusion/osf_protocol.c create mode 100644 drivers/iio/opensensorfusion/osf_protocol.hdrivers/iio/opensensorfusion/osf_protocol.c new file +37
-0+int osf_protocol_decode_frame (const u8 *buf,size_t len, +struct osf_frame *frame,size_t *frame_len) +{ +u32 expected_crc; +u32 actual_crc; +u32 payload_len; +size_t total_len; +u8 major; + +if (!buf || !frame || !frame_len) +return -EINVAL ; + +if (len <OSF_FRAME_MIN_LEN ) +return -EMSGSIZE ; + +if (get_unaligned_le32 (buf) !=OSF_FRAME_MAGIC ) +return -EPROTO ; + + major = buf[4 ]; +if (major !=OSF_PROTOCOL_MAJOR ) +return -EPROTO ; + +if (get_unaligned_le16 (buf +6 ) !=OSF_FRAME_HEADER_LEN ) +return -EPROTO ; + + payload_len =get_unaligned_le32 (buf +10 ); +if (payload_len > len -OSF_FRAME_MIN_LEN ) +return -EMSGSIZE ; + +if (get_unaligned_le32 (buf +34 )) +return -EPROTO ; + + total_len =OSF_FRAME_HEADER_LEN + payload_len +OSF_FRAME_CRC_LEN ; + expected_crc =osf_crc32_ieee (buf,OSF_FRAME_HEADER_LEN + payload_len); + actual_crc =get_unaligned_le32 (buf +OSF_FRAME_HEADER_LEN + payload_len); + +if (actual_crc != expected_crc)
drivers/iio/opensensorfusion/osf_protocol.c line 73
Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10
-0+return -EBADMSG ; + + frame->protocol_minor = buf[5 ]; + frame->message_type =get_unaligned_le16 (buf +8 ); + frame->payload_len = payload_len; + frame->sequence =get_unaligned_le64 (buf +14 ); + frame->timestamp_us =get_unaligned_le64 (buf +22 ); + frame->flags =get_unaligned_le32 (buf +30 ); + frame->payload = buf +OSF_FRAME_HEADER_LEN ; + frame->crc = actual_crc;
Same as below wrt to to using designated initializers for these structure fills
Quoted 43 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +43
-0+ *frame_len = total_len; + +return 0 ; +} + +int osf_protocol_decode_sensor_sample (const struct osf_frame *frame, +struct osf_sensor_sample *sample) +{ +u16 channel_count; +u16 sample_format; +u16 sensor_type; +size_t expected_len; +const u8 *payload; + +if (!frame || !sample || !frame->payload) +return -EINVAL ; + +if (frame->message_type !=OSF_MSG_SENSOR_SAMPLE ) +return -EPROTO ; + +if (frame->payload_len <OSF_SENSOR_SAMPLE_BASE_LEN ) +return -EMSGSIZE ; + + payload = frame->payload; + sensor_type =get_unaligned_le16 (payload); + channel_count =get_unaligned_le16 (payload +4 ); + sample_format =get_unaligned_le16 (payload +6 ); + +if (!osf_sensor_type_valid (sensor_type)) +return -EPROTO ; + +if (!channel_count) +return -EPROTO ; + +if (sample_format !=OSF_SAMPLE_FORMAT_S32 ) +return -EPROTO ; + +if (get_unaligned_le32 (payload +12 )) +return -EPROTO ; + +if (channel_count > (SIZE_MAX -OSF_SENSOR_SAMPLE_BASE_LEN ) /sizeof (s32)) +return -EOVERFLOW ; +
drivers/iio/opensensorfusion/osf_protocol.c line 126
Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10
-0+ expected_len =OSF_SENSOR_SAMPLE_BASE_LEN + channel_count *sizeof (s32); +if (frame->payload_len != expected_len) +return -EMSGSIZE ; + + sample->sensor_type = sensor_type; + sample->sensor_index =get_unaligned_le16 (payload +2 ); + sample->channel_count = channel_count; + sample->sample_format = sample_format; + sample->scale_nano =get_unaligned_le32 (payload +8 ); + sample->samples = payload +OSF_SENSOR_SAMPLE_BASE_LEN ;
See below. Designated initializer would help readability a little here.
drivers/iio/opensensorfusion/osf_protocol.c line 132
Quoted 6 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +6
-0+ +return 0 ; +} + +int osf_protocol_sensor_sample_value (const struct osf_sensor_sample *sample, +unsigned int index,s32 *value)
Given channel count is a u16 and we can't be equal or bigger than it, perhaps use a u16 for index as well.
drivers/iio/opensensorfusion/osf_protocol.c line 141
Quoted 9 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +9
-0+{ +if (!sample || !sample->samples || !value) +return -EINVAL ; + +if (index >= sample->channel_count) +return -ERANGE ; + +/* Samples are little-endian two's-complement signed values. */ + *value = (s32 )get_unaligned_le32 (sample->samples + index *sizeof (s32));
sizeof(__le32) slightly more appropriate given that is what you are treating it as.
Quoted 16 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +16
-0+ +return 0 ; +} + +int osf_protocol_decode_device_status (const struct osf_frame *frame, +struct osf_device_status *status) +{ +const u8 *payload; + +if (!frame || !status || !frame->payload) +return -EINVAL ; + +if (frame->message_type !=OSF_MSG_DEVICE_STATUS ) +return -EPROTO ; + +if (frame->payload_len !=OSF_DEVICE_STATUS_LEN )
drivers/iio/opensensorfusion/osf_protocol.c line 167
Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10
-0+return -EMSGSIZE ; + + payload = frame->payload; +if (get_unaligned_le32 (payload +16 )) +return -EPROTO ; + + status->uptime_s =get_unaligned_le32 (payload); + status->status_flags =get_unaligned_le32 (payload +4 ); + status->error_flags =get_unaligned_le32 (payload +8 ); + status->dropped_frames =get_unaligned_le32 (payload +12 );
Similar to below. I'd use a designated initializer for status as it is all written in one place.
Quoted 8 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +7
-0+ +return 0 ; +} + +int osf_protocol_decode_capability_entry (const struct osf_capability_report *report, +unsigned int index, +struct osf_capability_entry *entry)
drivers/iio/opensensorfusion/osf_protocol.c line 221
Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10
-0+{ +u16 sample_format; +u16 sensor_type; +u32 flags; +const u8 *payload; + +if (!report || !report->entries || !entry) +return -EINVAL ; + +if (index >= report->capability_count)
Neater to size index to match capability_count. Not that important though.
Quoted 15 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +15
-0+return -ERANGE ; + + payload = report->entries + index *OSF_CAP_SENSOR_ENTRY_LEN ; + sensor_type =get_unaligned_le16 (payload); + sample_format =get_unaligned_le16 (payload +6 ); + flags =get_unaligned_le32 (payload +12 ); + +if (!osf_sensor_type_valid (sensor_type)) +return -EPROTO ; + +if (sample_format !=OSF_SAMPLE_FORMAT_S32 ) +return -EPROTO ; + +if (flags & ~OSF_CAPABILITY_FLAGS_MASK ) +return -EPROTO ;
drivers/iio/opensensorfusion/osf_protocol.c line 246
Quoted 10 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +10
-0+ +if (get_unaligned_le32 (payload +16 )) +return -EPROTO ; + + entry->sensor_type = sensor_type; + entry->sensor_index =get_unaligned_le16 (payload +2 ); + entry->channel_count =get_unaligned_le16 (payload +4 ); + entry->sample_format = sample_format; + entry->scale_nano =get_unaligned_le32 (payload +8 ); + entry->flags = flags;
neater as designated initializer I think.
*entry = (struct osf_capability_entry) {
.sensor_type = sensor_type, .sensor_index = get_unaligned_le16(payload + 2),
etc
};
Quoted 3 lines
drivers/iio/opensensorfusion/osf_protocol.c new file +3
-0+ +return 0 ; +}
RFC PATCH v5 4/6 iio: osf: add stream parser
In reply to Jinseob KimAdd a byte-stream parser that resynchronizes on OSF frame magic, validates complete frames, and forwards decoded frames to the OSF core.
--- drivers/iio/opensensorfusion/osf_stream.c | 187 ++++++++++++++++++++++ drivers/iio/opensensorfusion/osf_stream.h | 31 ++++ 2 files changed, 218 insertions(+) create mode 100644 drivers/iio/opensensorfusion/osf_stream.c create mode 100644 drivers/iio/opensensorfusion/osf_stream.h
drivers/iio/opensensorfusion/osf_stream.c new file +187 -0
@@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/errno.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_STREAM_MAGIC_LEN 4 +#define OSF_STREAM_MAX_PAYLOAD_LEN \ + (OSF_STREAM_MAX_FRAME_LEN - OSF_FRAME_HEADER_LEN - OSF_FRAME_CRC_LEN) + +static const u8 osf_stream_magic[OSF_STREAM_MAGIC_LEN ] = { +'O' ,'S' ,'F' ,'0' , +}; + +static 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_match (const u8 *buf,size_t len) +{ +return !memcmp (buf, osf_stream_magic, len); +} + +static size_t osf_stream_discard_to_magic (struct osf_stream *stream) +{ +size_t old_len = stream->len; +size_t match_len; +size_t i; + +for (i =0 ; i < stream->len; i++) { + match_len = stream->len - i; +if (match_len >OSF_STREAM_MAGIC_LEN ) + match_len =OSF_STREAM_MAGIC_LEN ; + +if (osf_stream_magic_match (stream->buf + i, match_len)) { +if (i) +osf_stream_discard (stream, i); +return i; + } + } + + 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 first_err =0 ; +int ret; + +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 ; + + ret =osf_core_receive_frame (stream->osf, stream->buf, frame_len); +if (ret) { +if (ret == -EBADMSG ) { + stream->stats.bad_crc_frames++; + stream->stats.dropped_bytes++; +osf_stream_drop_invalid_head (stream); + }else { +osf_stream_discard (stream, frame_len); + } +if (!first_err) + first_err = ret; +continue ; + } + + stream->stats.valid_frames++; +osf_stream_discard (stream, frame_len); + } + +return first_err; +} + +void osf_stream_init (struct osf_stream *stream,struct osf_device *osf) +{ +if (!stream) +return ; + + stream->osf = osf; + stream->len =0 ; +memset (&stream->stats,0 ,sizeof (stream->stats)); +} + +void osf_stream_reset (struct osf_stream *stream) +{ +if (stream) { + stream->len =0 ; +memset (&stream->stats,0 ,sizeof (stream->stats)); + } +} + +int osf_stream_receive_bytes (struct osf_stream *stream,const u8 *buf, +size_t len) +{ +size_t copy_len; +size_t space; +int first_err =0 ; +int ret; + +if (!stream || !stream->osf || (!buf && len)) +return -EINVAL ; + +if (!len) { + ret =osf_stream_process (stream); +if (ret && !first_err) + first_err = ret; +return first_err; + } + +while (len) { + space =OSF_STREAM_MAX_FRAME_LEN - stream->len; +if (!space) { + stream->stats.dropped_bytes++; +osf_stream_discard (stream,1 ); +if (!first_err) + first_err = -EMSGSIZE ; +continue ; + } + + copy_len = len < space ? len : space; +memcpy (stream->buf + stream->len, buf, copy_len); + stream->len += copy_len; + buf += copy_len; + len -= copy_len; + + ret =osf_stream_process (stream); +if (ret && !first_err) + first_err = ret; + } + +return first_err; +}
drivers/iio/opensensorfusion/osf_stream.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _OSF_STREAM_H +#define _OSF_STREAM_H + +#include <linux/types.h> + +#define OSF_STREAM_MAX_FRAME_LEN 4096 + +struct osf_device ; + +struct osf_stream_stats { +u64 valid_frames; +u64 bad_magic_resyncs; +u64 bad_crc_frames; +u64 partial_frames; +u64 dropped_bytes; +}; + +struct osf_stream { +struct osf_device *osf; +u8 buf[OSF_STREAM_MAX_FRAME_LEN ]; +size_t len; +struct osf_stream_stats stats; +}; + +void osf_stream_init (struct osf_stream *stream,struct osf_device *osf); +void osf_stream_reset (struct osf_stream *stream); +int osf_stream_receive_bytes (struct osf_stream *stream,const u8 *buf, +size_t len); + +#endif
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH RFC v5 4/6] iio: osf: add stream parser
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The stream parser discards up to `frame_len` bytes of unauthenticated data on protocol errors…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The stream parser discards up to frame_len bytes of unauthenticated data on protocol errors (-EPROTO), allowing malformed or random data to cause prolonged desynchronization and data loss of up to 4096 bytes of valid frames. --
commit 9d1c984a937aba83c2b56bd0cda78b4b642ec15a
Author: Jinseob Kim <[email protected]>
iio: osf: add stream parser
Add a byte-stream parser that resynchronizes on OSF frame magic, validates complete frames, and forwards decoded frames to the OSF core.
Quoted 5 lines
drivers/iio/opensensorfusion/osf_stream.c new file +0
-0
[ ... ]
Quoted 2 lines
drivers/iio/opensensorfusion/osf_stream.c new file +2
-0+static int osf_stream_process (struct osf_stream *stream) +{
[ ... ]
drivers/iio/opensensorfusion/osf_stream.c line 116
Quoted 9 lines
drivers/iio/opensensorfusion/osf_stream.c new file +9
-0+ ret =osf_core_receive_frame (stream->osf, stream->buf, frame_len); +if (ret) { +if (ret == -EBADMSG ) { + stream->stats.bad_crc_frames++; + stream->stats.dropped_bytes++; +osf_stream_drop_invalid_head (stream); + }else { +osf_stream_discard (stream, frame_len); + }
[Severity: High]
If osf_core_receive_frame() returns a non-CRC error like -EPROTO, is it safe to discard frame_len bytes here?
It looks like osf_protocol_decode_frame() returns -EPROTO for framing errors before validating the CRC:
osf_protocol_decode_frame() {
...
major = buf[4];
if (major != OSF_PROTOCOL_MAJOR)
return -EPROTO;
...
}Because the CRC hasn't been verified yet when this error is returned, frame_len could be an unauthenticated garbage value if line noise happened to match the magic bytes.
Could this blindly discard up to 4096 bytes of potentially valid frames and cause prolonged desynchronization, instead of dropping just the invalid head to quickly resynchronize?
Quoted 4 lines
drivers/iio/opensensorfusion/osf_stream.c new file +4
-0+if (!first_err) + first_err = ret; +continue ; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH RFC v5 4/6] iio: osf: add stream parser
In reply to Jinseob KimQuoted 2 lines · Jinseob Kim
Add a byte-stream parser that resynchronizes on OSF frame magic, validates complete frames, and forwards decoded frames to the OSF core.
...
drivers/iio/opensensorfusion/osf_stream.c line 18
Quoted 3 lines
drivers/iio/opensensorfusion/osf_stream.c new file +3
-0+static const u8 osf_stream_magic[OSF_STREAM_MAGIC_LEN ] = { +'O' ,'S' ,'F' ,'0' , +};
Why?! You have already a definition, use it instead.
...
drivers/iio/opensensorfusion/osf_stream.c line 47
Quoted 8 lines
drivers/iio/opensensorfusion/osf_stream.c new file +7
-0+static size_t osf_stream_discard_to_magic (struct osf_stream *stream) +{ +size_t old_len = stream->len; +size_t match_len; +size_t i; + +for (i =0 ; i < stream->len; i++) {
In current form it's as simple as
for (size_t i = 0; i < stream->len; i++) {Quoted 14 lines
drivers/iio/opensensorfusion/osf_stream.c new file +14
-0+ match_len = stream->len - i; +if (match_len >OSF_STREAM_MAGIC_LEN ) + match_len =OSF_STREAM_MAGIC_LEN ; + +if (osf_stream_magic_match (stream->buf + i, match_len)) { +if (i) +osf_stream_discard (stream, i); +return i; + } + } + + stream->len =0 ; +return old_len; +}
...
Quoted 15 lines
drivers/iio/opensensorfusion/osf_stream.c new file +15
-0+static int osf_stream_process (struct osf_stream *stream) +{ +size_t discarded; +size_t frame_len; +u32 payload_len; +int first_err =0 ; +int ret; + +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 ;
drivers/iio/opensensorfusion/osf_stream.c line 87
Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +9
-0+ } + +if (!stream->len) +break ; + +if (stream->len <OSF_FRAME_HEADER_LEN ) +break ; +if (get_unaligned_le16 (stream->buf +6 ) != +OSF_FRAME_HEADER_LEN ) {
Make it a single line for readability.
Quoted 29 lines
drivers/iio/opensensorfusion/osf_stream.c new file +29
-0+ 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 ; + + ret =osf_core_receive_frame (stream->osf, stream->buf, frame_len); +if (ret) { +if (ret == -EBADMSG ) { + stream->stats.bad_crc_frames++; + stream->stats.dropped_bytes++; +osf_stream_drop_invalid_head (stream); + }else { +osf_stream_discard (stream, frame_len); + }
drivers/iio/opensensorfusion/osf_stream.c line 126
Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +9
-0+if (!first_err) + first_err = ret; +continue ; + } + + stream->stats.valid_frames++; +osf_stream_discard (stream, frame_len); + } +return first_err;
Why do we continue on the error and then still return an error? Same Q for the receive part.
Quoted 1 line
+}
...
Quoted 6 lines
drivers/iio/opensensorfusion/osf_stream.c new file +6
-0+void osf_stream_init (struct osf_stream *stream,struct osf_device *osf) +{ +if (!stream) +return ; + + stream->osf = osf;
drivers/iio/opensensorfusion/osf_stream.c line 144
Quoted 10 lines
drivers/iio/opensensorfusion/osf_stream.c new file +10
-0+ stream->len =0 ; +memset (&stream->stats,0 ,sizeof (stream->stats)); +} + +void osf_stream_reset (struct osf_stream *stream) +{ +if (stream) { + stream->len =0 ; +memset (&stream->stats,0 ,sizeof (stream->stats)); + }
As per above
if (!stream) return;
Quoted 1 line
+}
...
drivers/iio/opensensorfusion/osf_stream.h line 17
Quoted 7 lines
drivers/iio/opensensorfusion/osf_stream.h new file +7
-0+struct osf_stream_stats { +u64 valid_frames; +u64 bad_magic_resyncs; +u64 bad_crc_frames; +u64 partial_frames; +u64 dropped_bytes; +};
Don't you want to use linux/u64_stats_sync.h APIs?
Signature · 2 lines
With Best Regards, Andy Shevchenko
RFC PATCH v5 5/6 iio: osf: add UART transport
In reply to Jinseob KimAdd the serdev UART transport and the initial OSF core receive path.
Enable the required vcc regulator with devm_regulator_get_enable() before opening the UART, keeping power handling limited to the simple probe-time requirement for this RFC.
--- drivers/iio/Kconfig | 1 + drivers/iio/Makefile | 1 + drivers/iio/opensensorfusion/Kconfig | 15 +++ drivers/iio/opensensorfusion/Makefile | 5 + drivers/iio/opensensorfusion/osf_core.c | 99 ++++++++++++++++++ drivers/iio/opensensorfusion/osf_core.h | 18 ++++ drivers/iio/opensensorfusion/osf_serdev.c | 117 ++++++++++++++++++++++ 7 files changed, 256 insertions(+) create mode 100644 drivers/iio/opensensorfusion/Kconfig create mode 100644 drivers/iio/opensensorfusion/Makefile create mode 100644 drivers/iio/opensensorfusion/osf_core.c create mode 100644 drivers/iio/opensensorfusion/osf_core.h create mode 100644 drivers/iio/opensensorfusion/osf_serdev.c
drivers/iio/Kconfig +1 -0
@@ -101,6 +101,7 @@ source "drivers/iio/light/Kconfig" source "drivers/iio/magnetometer/Kconfig" source "drivers/iio/multiplexer/Kconfig" source "drivers/iio/orientation/Kconfig" +source "drivers/iio/opensensorfusion/Kconfig" source "drivers/iio/test/Kconfig" if IIO_TRIGGER source "drivers/iio/trigger/Kconfig"
drivers/iio/Makefile +1 -0
@@ -37,6 +37,7 @@ obj-y += light/ obj-y += magnetometer/ obj-y += multiplexer/ obj-y += orientation/ +obj-y += opensensorfusion/ obj-y += position/ obj-y += potentiometer/ obj-y += potentiostat/
drivers/iio/opensensorfusion/Kconfig new file +15 -0
@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config OPEN_SENSOR_FUSION + tristate "Open Sensor Fusion UART IIO driver" + depends on IIO + depends on SERIAL_DEV_BUS + select CRC32 + help + Build the Open Sensor Fusion UART receive path. + + The driver receives OSF protocol frames over a serdev UART. + Frames are decoded and validated before being passed to the + driver core. + This patch only adds the transport path. + IIO device registration is added separately.
drivers/iio/opensensorfusion/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/opensensorfusion/osf_core.c new file +99 -0
99 lines not on this page · raw message
drivers/iio/opensensorfusion/osf_core.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _OSF_CORE_H +#define _OSF_CORE_H + +#include <linux/types.h> + +struct device ; + +struct osf_device { +struct device *dev; +u64 last_sequence; +}; + +void osf_core_init (struct osf_device *osf,struct device *dev); +void osf_core_unregister_iio (struct osf_device *osf); +int osf_core_receive_frame (struct osf_device *osf,const u8 *buf,size_t len); + +#endif
drivers/iio/opensensorfusion/osf_serdev.c new file +117 -0
117 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH RFC v5 5/6] iio: osf: add UART transport
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The frame decoder validates header fields before verifying the frame CRC, returning `-EPROTO` for…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The frame decoder validates header fields before verifying the frame CRC, returning -EPROTO for structural errors and causing the stream parser to drop unauthenticated payload lengths. --
commit ca765586fd1687d99b4b8cd4c1d18456513a142b
Author: Jinseob Kim <[email protected]>
iio: osf: add UART transport
Add the serdev UART transport and the initial OSF core receive path. Enable the required vcc regulator with devm_regulator_get_enable() before opening the UART, keeping power handling limited to the simple probe-time requirement for this RFC.
Quoted 6 lines
drivers/iio/opensensorfusion/osf_core.c new file +0
-0@@ -0,0 +1,99 @@
[ ... ]
drivers/iio/opensensorfusion/osf_core.c line 67
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10
-0+int osf_core_receive_frame (struct osf_device *osf,const u8 *buf,size_t len) +{ +struct osf_frame frame; +size_t frame_len; +int ret; + +if (!osf || !buf) +return -EINVAL ; + + ret =osf_protocol_decode_frame (buf, len, &frame, &frame_len);
[Severity: High]
Could this error handling cause a regression where data is lost during parsing?
osf_protocol_decode_frame() validates header fields (like the major version and header length) before checking the CRC, returning -EPROTO on structural errors:
drivers/iio/opensensorfusion/osf_protocol.c:osf_protocol_decode_frame() {
...
major = buf[4];
if (major != OSF_PROTOCOL_MAJOR)
return -EPROTO;
if (get_unaligned_le16(buf + 6) != OSF_FRAME_HEADER_LEN)
return -EPROTO;
...
}If -EPROTO is returned here, the stream parser discards the unauthenticated frame_len number of bytes instead of dropping 1 byte for resynchronization:
drivers/iio/opensensorfusion/osf_stream.c:osf_stream_process() {
...
ret = osf_core_receive_frame(stream->osf, stream->buf, frame_len);
if (ret) {
if (ret == -EBADMSG) {
stream->stats.bad_crc_frames++;
stream->stats.dropped_bytes++;
osf_stream_drop_invalid_head(stream);
} else {
osf_stream_discard(stream, frame_len);
}
...
}Because the CRC wasn't evaluated yet, frame_len is derived from an unauthenticated payload_len, which could be a large, corrupted value. This regression might cause the stream parser to skip legitimate frames hidden in the incoming buffer under noisy conditions.
Quoted 5 lines
drivers/iio/opensensorfusion/osf_core.c new file +5
-0+if (ret) +return ret; + +if (frame_len != len) +return -EMSGSIZE ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH RFC v5 5/6] iio: osf: add UART transport
In reply to Jinseob KimQuoted 5 lines · Jinseob Kim
Add the serdev UART transport and the initial OSF core receive path.
Enable the required vcc regulator with
devm_regulator_get_enable()before opening the UART, keeping power handling limited to the simple probe-time requirement for this RFC.
...
Quoted 4 lines
drivers/iio/opensensorfusion/Kconfig new file +4
-0+config OPEN_SENSOR_FUSION + tristate "Open Sensor Fusion UART IIO driver" + depends on IIO + depends on SERIAL_DEV_BUS
drivers/iio/opensensorfusion/Kconfig line 15
Quoted 10 lines
drivers/iio/opensensorfusion/Kconfig new file +9
-0+ select CRC32 + help + Build the Open Sensor Fusion UART receive path. + + The driver receives OSF protocol frames over a serdev UART. + Frames are decoded and validated before being passed to the + driver core. + This patch only adds the transport path. + IIO device registration is added separately.
What is this paragraph supposed to mean?
...
drivers/iio/opensensorfusion/osf_core.c line 49
Quoted 12 lines
drivers/iio/opensensorfusion/osf_core.c new file +12
-0+static int osf_core_validate_capability_report (const struct osf_frame *frame) +{ +struct osf_capability_entry entry; +struct osf_capability_report report; +unsigned int i; +int ret; + + ret =osf_protocol_decode_capability_report (frame, &report); +if (ret) +return ret; + +for (i =0 ; i < report.capability_count; i++) {
for (unsigned int i = 0; i < report.capability_count; i++) {Quoted 7 lines
drivers/iio/opensensorfusion/osf_core.c new file +7
-0+ ret =osf_protocol_decode_capability_entry (&report, i, &entry); +if (ret) +return ret; + } + +return 0 ; +}
...
drivers/iio/opensensorfusion/osf_core.c line 65
Quoted 8 lines
drivers/iio/opensensorfusion/osf_core.c new file +7
-0+int osf_core_receive_frame (struct osf_device *osf,const u8 *buf,size_t len) +{ +struct osf_frame frame; +size_t frame_len; +int ret; +if (!osf || !buf) +return -EINVAL ;
How can this be called with osf == NULL?
Quoted 16 lines
drivers/iio/opensensorfusion/osf_core.c new file +16
-0+ ret =osf_protocol_decode_frame (buf, len, &frame, &frame_len); +if (ret) +return ret; + +if (frame_len != len) +return -EMSGSIZE ; + +switch (frame.message_type) { +case OSF_MSG_SENSOR_SAMPLE : + ret =osf_core_validate_sensor_sample (&frame); +break ; +case OSF_MSG_DEVICE_STATUS : + ret =osf_core_validate_device_status (&frame); +break ; +case OSF_MSG_CAPABILITY_REPORT : + ret =osf_core_validate_capability_report (&frame);
drivers/iio/opensensorfusion/osf_core.c line 92
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10
-0+break ; +default : +if (frame.message_type >=OSF_RESERVED_MSG_FIRST && + frame.message_type <=OSF_RESERVED_MSG_LAST ) + ret =0 ; +else if (frame.message_type >=OSF_VENDOR_PRIVATE_FIRST ) + ret =0 ; +else + ret = -EOPNOTSUPP ; +break ;
You may invert this and return directly
if ((frame.message_type < OSF_VENDOR_PRIVATE_FIRST) && (frame.message_type < OSF_RESERVED_MSG_FIRST || frame.message_type > OSF_RESERVED_MSG_LAST)) return -EOPNOTSUPP;
drivers/iio/opensensorfusion/osf_core.c line 98
Quoted 6 lines
drivers/iio/opensensorfusion/osf_core.c new file +5
-0+ } +if (!ret) + osf->last_sequence = frame.sequence; + +return ret;
No. Use regular pattern
if (ret) return ret; ... return 0;
Quoted 1 line
+}
...
drivers/iio/opensensorfusion/osf_serdev.c line 7
Quoted 6 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +5
-0+#include <linux/device.h> +#include <linux/errno.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/of.h>
What is this for?
Quoted 27 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +27
-0+#include <linux/regulator/consumer.h> +#include <linux/serdev.h> +#include <linux/slab.h> +#include <linux/types.h> + +#include "osf_core.h" +#include "osf_stream.h" + +#define OSF_SERDEV_BAUD 115200 + +struct osf_serdev { +struct serdev_device *serdev; +struct osf_device osf; +struct osf_stream stream; +}; + +static size_t osf_serdev_receive_buf (struct serdev_device *serdev, +const u8 *buf,size_t count) +{ +struct osf_serdev *osf_uart =serdev_device_get_drvdata (serdev); +const struct osf_stream_stats *stats; +u64 valid_before; +int ret; + + valid_before = osf_uart->stream.stats.valid_frames; + ret =osf_stream_receive_bytes (&osf_uart->stream, buf, count); + stats = &osf_uart->stream.stats;
drivers/iio/opensensorfusion/osf_serdev.c line 44
Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +10
-0+ +if (ret || stats->valid_frames != valid_before) + dev_dbg_ratelimited(&serdev->dev, + "rx count=%zu valid=%llu bad_magic=%llu bad_crc=%llu partial=%llu dropped=%llu ret=%d\n", + count, + (unsigned long long)stats->valid_frames, + (unsigned long long)stats->bad_magic_resyncs, + (unsigned long long)stats->bad_crc_frames, + (unsigned long long)stats->partial_frames, + (unsigned long long)stats->dropped_bytes,
Why casting?
Quoted 4 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +4
-0+ ret); + +return count; +}
...
Review
Quoted 2 lines
(quoted diff) +2
-0+static int osf_serdev_probe(struct serdev_device *serdev) +{
struct device *dev = &serdev->dev;
makes the below look better.
Quoted 42 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +42
-0+struct osf_serdev *osf_uart; +unsigned int baudrate; +int ret; + + osf_uart =devm_kzalloc (&serdev->dev,sizeof (*osf_uart),GFP_KERNEL ); +if (!osf_uart) +return -ENOMEM ; + + osf_uart->serdev = serdev; +osf_core_init (&osf_uart->osf, &serdev->dev); +osf_stream_init (&osf_uart->stream, &osf_uart->osf); + +serdev_device_set_drvdata (serdev, osf_uart); +serdev_device_set_client_ops (serdev, &osf_serdev_ops); + + ret =devm_regulator_get_enable (&serdev->dev,"vcc" ); +if (ret) +return dev_err_probe (&serdev->dev, ret, +"failed to enable vcc regulator\n" ); + + ret =serdev_device_open (serdev); +if (ret) +return ret; + + baudrate =serdev_device_set_baudrate (serdev,OSF_SERDEV_BAUD ); +if (baudrate !=OSF_SERDEV_BAUD ) +dev_warn (&serdev->dev,"requested %u baud, controller set %u\n" , +OSF_SERDEV_BAUD , baudrate); + +serdev_device_set_flow_control (serdev, false); + +return 0 ; +} + +static void osf_serdev_remove (struct serdev_device *serdev) +{ +struct osf_serdev *osf_uart =serdev_device_get_drvdata (serdev); + +serdev_device_close (serdev); +osf_stream_reset (&osf_uart->stream); +osf_core_unregister_iio (&osf_uart->osf); +}
...
drivers/iio/opensensorfusion/osf_serdev.c line 113
Quoted 10 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +9
-0+static struct serdev_device_driver osf_serdev_driver = { + .probe = osf_serdev_probe, + .remove = osf_serdev_remove, + .driver = { + .name ="open-sensor-fusion-uart" , + .of_match_table = osf_serdev_of_match, + }, +}; +
No blank line needed here.
Quoted 1 line
+module_serdev_device_driver(osf_serdev_driver);
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH RFC v5 5/6] iio: osf: add UART transport
In reply to Jinseob KimQuoted 7 lines · On Tue, 16 Jun 2026 16:22:41 +0900 Jinseob Kim <[email protected]> wrote:
Add the serdev UART transport and the initial OSF core receive path.
Enable the required vcc regulator with
devm_regulator_get_enable()before opening the UART, keeping power handling limited to the simple probe-time requirement for this RFC.Signed-off-by Jinseob Kim <[email protected]>
A few things inline.
Thanks,
Jonathan
Quoted 11 lines
drivers/iio/opensensorfusion/Kconfig new file +5
-0@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config OPEN_SENSOR_FUSION + tristate "Open Sensor Fusion UART IIO driver" + depends on IIO
drivers/iio/opensensorfusion/Kconfig line 15
Quoted 10 lines
drivers/iio/opensensorfusion/Kconfig new file +10
-0+ depends on SERIAL_DEV_BUS + select CRC32 + help + Build the Open Sensor Fusion UART receive path. + + The driver receives OSF protocol frames over a serdev UART. + Frames are decoded and validated before being passed to the + driver core. + This patch only adds the transport path. + IIO device registration is added separately.
Don't talk about a patch in here. Talk about what is supported then if you really want to add the other bits in later patches. Mostly this help is generic enough we don't need to modify it more than once in a series.
Quoted 12 lines
drivers/iio/opensensorfusion/osf_core.c new file +6
-0@@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/errno.h> +#include <linux/string.h> +#include <linux/types.h> +
drivers/iio/opensensorfusion/osf_core.c line 16
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c new file +10
-0+#include "osf_core.h" +#include "osf_protocol.h" + +#define OSF_RESERVED_MSG_FIRST 0x7f00 +#define OSF_RESERVED_MSG_LAST 0x7fff +#define OSF_VENDOR_PRIVATE_FIRST 0x8000 + +void osf_core_init (struct osf_device *osf,struct device *dev) +{ +memset (osf,0 ,sizeof (*osf));
*osf = (struct osf_device){
.dev = dev,
};is guaranteed to also clear all other fields (new C spec as well as the options the kernel has long been built with) so is how I would always do cases of zero then set stuff like this.
Quoted 9 lines
drivers/iio/opensensorfusion/osf_core.c new file +2
-0+ osf->dev = dev; +}drivers/iio/opensensorfusion/osf_serdev.c new file +0
-0
drivers/iio/opensensorfusion/osf_serdev.c line 96
Quoted 9 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +8
-0+ +static void osf_serdev_remove (struct serdev_device *serdev) +{ +struct osf_serdev *osf_uart =serdev_device_get_drvdata (serdev); + +serdev_device_close (serdev); +osf_stream_reset (&osf_uart->stream); +osf_core_unregister_iio (&osf_uart->osf);
My gut feeling is this should be first to tear down the device interfaces as soon as possible. They will have been initialized after the serdev was opened so should be unregistered before it is closed. If there is a reason for this specific order add a comment.
drivers/iio/opensensorfusion/osf_serdev.c line 113
Quoted 12 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +11
-0+} + +static struct serdev_device_driver osf_serdev_driver = { + .probe = osf_serdev_probe, + .remove = osf_serdev_remove, + .driver = { + .name ="open-sensor-fusion-uart" , + .of_match_table = osf_serdev_of_match, + }, +}; +
No blank line here as the macro is extremely tightly coupled with the structure and it is nice to have the visual cue.
Quoted 4 lines
drivers/iio/opensensorfusion/osf_serdev.c new file +4
-0+module_serdev_device_driver (osf_serdev_driver); + +MODULE_DESCRIPTION ("Open Sensor Fusion IIO driver" ); +MODULE_LICENSE ("GPL" );
RFC PATCH v5 6/6 iio: osf: register IIO devices from capabilities
In reply to Jinseob KimRegister IIO devices for supported Open Sensor Fusion capability entries and push received samples into IIO buffers when enabled.
--- drivers/iio/opensensorfusion/Kconfig | 11 +- drivers/iio/opensensorfusion/Makefile | 3 +- drivers/iio/opensensorfusion/osf_core.c | 253 ++++++++++++++++++++-- drivers/iio/opensensorfusion/osf_core.h | 52 +++++ drivers/iio/opensensorfusion/osf_iio.c | 275 ++++++++++++++++++++++++ drivers/iio/opensensorfusion/osf_iio.h | 22 ++ 6 files changed, 586 insertions(+), 30 deletions(-) create mode 100644 drivers/iio/opensensorfusion/osf_iio.c create mode 100644 drivers/iio/opensensorfusion/osf_iio.h
drivers/iio/opensensorfusion/Kconfig +5 -6
@@ -5,11 +5,10 @@ config OPEN_SENSOR_FUSION depends on IIO 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 OSF protocol frames over a serdev UART. - Frames are decoded and validated before being passed to the - driver core. - This patch only adds the transport path. - IIO device registration is added separately. + The driver receives OSF protocol frames over a serdev UART and + registers IIO devices for supported capability entries.
drivers/iio/opensensorfusion/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/opensensorfusion/osf_core.c +230 -23
307 lines not on this page · raw message
drivers/iio/opensensorfusion/osf_core.h +52 -0
69 lines not on this page · raw message
drivers/iio/opensensorfusion/osf_iio.c new file +275 -0
275 lines not on this page · raw message
drivers/iio/opensensorfusion/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, +unsigned int channel_count); +bool osf_iio_sensor_supported (u16 sensor_type,u16 channel_count); +const char *osf_iio_sensor_name (u16 sensor_type); + +#endif
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH RFC v5 6/6] iio: osf: register IIO devices from capabilities
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing synchronization between asynchronous IIO buffer push and buffer enable/disable operations…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing synchronization between asynchronous IIO buffer push and buffer enable/disable operations, leading to list corruption.
- [High] Uninitialized padding bytes in the IIO bounce buffer leak kernel heap memory to user-space.
--
commit e42cf74aa62314d9b92e389435a75cfcd58b0820
Author: Jinseob Kim <[email protected]>
iio: osf: register IIO devices from capabilities
Register IIO devices for supported Open Sensor Fusion capability entries and push received samples into IIO buffers when enabled.
Quoted 5 lines
drivers/iio/opensensorfusion/osf_iio.c new file +0
-0
[ ... ]
drivers/iio/opensensorfusion/osf_iio.c line 267
Quoted 11 lines
drivers/iio/opensensorfusion/osf_iio.c new file +11
-0+int osf_iio_push_sample (struct iio_dev *indio_dev,const s32 *values, +unsigned int channel_count) +{ +struct osf_iio_state *state =iio_priv (indio_dev); +s64 timestamp; + +if (channel_count != state->spec->channel_count) +return -EPROTO ; + +/* This is only a fast path; IIO rechecks buffer state while pushing. */ +if (!iio_buffer_enabled (indio_dev))
[Severity: High]
Is there a race condition between this asynchronous IIO buffer push and buffer enable/disable operations?
The IIO core's iio_push_to_buffers() function iterates over the buffer_list without locks, expecting the driver to guarantee no pushes occur concurrently with buffer state changes.
Since this driver doesn't implement IIO setup_ops (like predisable) to stop incoming frames, or use a software lock, could a concurrent write to the sysfs enable attribute invoke iio_disable_buffers() and modify the buffer_list while the UART RX path is traversing it, causing list corruption?
drivers/iio/opensensorfusion/osf_iio.c line 274
Quoted 7 lines
drivers/iio/opensensorfusion/osf_iio.c new file +7
-0+return 0 ; + + timestamp =iio_get_time_ns (indio_dev); + +return iio_push_to_buffers_with_ts_unaligned (indio_dev, values, + channel_count *sizeof (*values), + timestamp);
[Severity: High]
Does this leak uninitialized kernel heap memory to user-space through the bounce buffer padding?
The data payload size passed here is exactly the size of the channels without padding. The IIO core computes scan_bytes to include padding for the 64-bit timestamp to ensure alignment, and allocates the internal bounce buffer using devm_krealloc() without the __GFP_ZERO flag.
Because the IIO core only copies the exact data payload size and inserts the timestamp at the aligned offset, it leaves the intermediate padding bytes completely uninitialized. When this bounce buffer is pushed into the KFIFO, will it leak uninitialized memory with every sample?
Would it be safer to assemble the sample in a zero-initialized padded struct on the stack and use iio_push_to_buffers_with_timestamp() instead?
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH RFC v5 6/6] iio: osf: register IIO devices from capabilities
In reply to Jinseob KimQuoted 2 lines · Jinseob Kim
Register IIO devices for supported Open Sensor Fusion capability entries and push received samples into IIO buffers when enabled.
...
drivers/iio/opensensorfusion/Kconfig config OPEN_SENSOR_FUSION · line 14
Quoted 11 lines
drivers/iio/opensensorfusion/Kconfig +3
-6help - Build the Open Sensor Fusion UART receive path. + Build the Open Sensor Fusion UART IIO driver. - The driver receives OSF protocol frames over a serdev UART. - Frames are decoded and validated before being passed to the - driver core. - This patch only adds the transport path. - IIO device registration is added separately. + The driver receives OSF protocol frames over a serdev UART and + registers IIO devices for supported capability entries.
Can't you fix this in the initial patch?
...
drivers/iio/opensensorfusion/Makefile line 6
Quoted 5 lines
obj-$(CONFIG_OPEN_SENSOR_FUSION) += open-sensor-fusion.o
drivers/iio/opensensorfusion/Makefile +2
-1-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
Modify the original code the way that this will only have a + line.
...
drivers/iio/opensensorfusion/osf_core.c line 24
Quoted 5 lines
void
osf_core_unregister_iio(struct osf_device *osf){drivers/iio/opensensorfusion/osf_core.c +3
-0+unsigned int i; + +for (i =0 ; i < osf->iio_dev_count; i++)
For all for-loops where it's not going outside it
for (unsigned int i = 0; i < osf->iio_dev_count; i++)
Quoted 4 lines
drivers/iio/opensensorfusion/osf_core.c +4
-0+osf_iio_unregister_sensor (osf->iio_devs[i].indio_dev); + + osf->iio_dev_count =0 ; +}
...
Quoted 14 lines
drivers/iio/opensensorfusion/osf_core.c +3
-6- ret =osf_core_validate_sensor_sample (&frame); -break ; +return osf_core_handle_sensor_sample (osf, &frame);case OSF_MSG_DEVICE_STATUS : - ret =osf_core_validate_device_status (&frame); -break ; +return osf_core_handle_device_status (osf, &frame);case OSF_MSG_CAPABILITY_REPORT : - ret =osf_core_validate_capability_report (&frame); -break ; +return osf_core_handle_capability_report (osf, &frame);default :if (frame.message_type >=OSF_RESERVED_MSG_FIRST && frame.message_type <=OSF_RESERVED_MSG_LAST )
drivers/iio/opensensorfusion/osf_core.c int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len) · line 274
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c +4
-6- ret =0 ; - else if (frame.message_type >= OSF_VENDOR_PRIVATE_FIRST) - ret =0 ; - else - ret = -EOPNOTSUPP ; -break ; +return 0 ; +if (frame.message_type >=OSF_VENDOR_PRIVATE_FIRST ) +return 0 ; +return -EOPNOTSUPP ;
With my suggestion this can be done in the initial patch. So, this indeed needs a lot of work as for RFC :-)
I stop here.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH RFC v5 6/6] iio: osf: register IIO devices from capabilities
In reply to Jinseob KimQuoted 4 lines · On Tue, 16 Jun 2026 16:22:42 +0900 Jinseob Kim <[email protected]> wrote:
Register IIO devices for supported Open Sensor Fusion capability entries and push received samples into IIO buffers when enabled.
Signed-off-by Jinseob Kim <[email protected]>
Sashiko had a few comments. The last one on the unitilialized heap memory needs a new version of the fix from me.
Hopefully I'll get to that in the next few days,
https://sashiko.dev/#/patchset/20260529121005.1470-1-kimjinseob88%40gmail.com
The one about intermediate build issues (if correct) suggests you didn't ensure this series builds after each patch. Please make sure to do that to avoid breaking bisectability of the kernel.
Thanks,
Jonathan
Quoted 22 lines
--- drivers/iio/opensensorfusion/Kconfig | 11 +- drivers/iio/opensensorfusion/Makefile | 3 +- drivers/iio/opensensorfusion/osf_core.c | 253 ++++++++++++++++++++-- drivers/iio/opensensorfusion/osf_core.h | 52 +++++ drivers/iio/opensensorfusion/osf_iio.c | 275 ++++++++++++++++++++++++ drivers/iio/opensensorfusion/osf_iio.h | 22 ++ 6 files changed, 586 insertions(+), 30 deletions(-) create mode 100644 drivers/iio/opensensorfusion/osf_iio.c create mode 100644 drivers/iio/opensensorfusion/osf_iio.hdrivers/iio/opensensorfusion/Kconfig +2
-0@@ -5,11 +5,10 @@ config OPEN_SENSOR_FUSION depends on IIO depends on SERIAL_DEV_BUS select CRC32 + select IIO_BUFFER + select IIO_KFIFO_BUF help
drivers/iio/opensensorfusion/Kconfig config OPEN_SENSOR_FUSION · line 14
Quoted 10 lines
drivers/iio/opensensorfusion/Kconfig +3
-6- Build the Open Sensor Fusion UART receive path. + Build the Open Sensor Fusion UART IIO driver. - The driver receives OSF protocol frames over a serdev UART. - Frames are decoded and validated before being passed to the - driver core. - This patch only adds the transport path. - IIO device registration is added separately. + The driver receives OSF protocol frames over a serdev UART and + registers IIO devices for supported capability entries.
Avoid this churn. I wouldn't worry about it being a little forwards looking when added in the earlier patch and directly go to the final text.
Quoted 32 lines
drivers/iio/opensensorfusion/osf_core.c +24
-1-static int osf_core_validate_sensor_sample (const struct osf_frame *frame) +static int osf_core_register_capabilities (struct osf_device *osf, +const struct osf_capability_cache *cache) { +struct iio_dev *indio_dev; +unsigned int i; +int ret; + +if (osf->capability_cache.valid) +return 0 ; + +for (i =0 ; i < cache->capability_count; i++) { +if (!osf_iio_sensor_supported (cache->entries[i].sensor_type, + cache->entries[i].channel_count)) +continue ; + +if (osf_core_capability_is_duplicate (cache, i)) +return -EEXIST ; + } + +for (i =0 ; i < cache->capability_count; i++) { +if (!osf_iio_sensor_supported (cache->entries[i].sensor_type, + cache->entries[i].channel_count)) +continue ; + + ret = osf_iio_register_sensor(osf->dev, &cache->entries[i],
drivers/iio/opensensorfusion/osf_core.c line 119
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c +10
-0+ osf, &indio_dev); +if (ret) + goto err_unregister; + + osf->iio_devs[osf->iio_dev_count].sensor_type = + cache->entries[i].sensor_type; + osf->iio_devs[osf->iio_dev_count].sensor_index = + cache->entries[i].sensor_index; + osf->iio_devs[osf->iio_dev_count].indio_dev = indio_dev; + osf->iio_dev_count++;
Probably use a designated initializer for this one
ost->iio_dev[osf->iio_dev_count++] = (struct osf_iio_binding) {
.sensor_type = ...
};Not a problem if the lines are over 80 chars given this should be generally easier to read.
Quoted 16 lines
drivers/iio/opensensorfusion/osf_core.c +15
-0+ +static int osf_core_handle_sensor_sample (struct osf_device *osf, +const struct osf_frame *frame) +{ +struct osf_latest_sample *latest;struct osf_sensor_sample sample; +struct iio_dev *indio_dev; +s32 values[OSF_MAX_SAMPLE_CHANNELS ] = { }; +unsigned int i; +int ret; + + ret =osf_protocol_decode_sensor_sample (frame, &sample); +if (ret) +return ret; + +if (sample.channel_count >OSF_MAX_SAMPLE_CHANNELS )
drivers/iio/opensensorfusion/osf_core.c line 153
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c +8
-1+return -E2BIG ; + +for (i =0 ; i < sample.channel_count; i++) { + ret =osf_protocol_sensor_sample_value (&sample, i, &values[i]); +if (ret) +return ret; + } -return osf_protocol_decode_sensor_sample (frame, &sample); +mutex_lock (&osf->latest_lock);
This may well be better as a scoped_guard()
drivers/iio/opensensorfusion/osf_core.c line 157
Quoted 4 lines
drivers/iio/opensensorfusion/osf_core.c +4
-0+ latest =osf_core_find_latest_sample (osf, sample.sensor_type, + sample.sensor_index); +if (!latest) { +mutex_unlock (&osf->latest_lock);
scoped_guard() would allow you to return here without worrying about the manual unlock.
Quoted 43 lines
drivers/iio/opensensorfusion/osf_core.c +24
-7+return -E2BIG ; + } + +memcpy (latest->values, values,sizeof (values)); + latest->sensor_type = sample.sensor_type; + latest->sensor_index = sample.sensor_index; + latest->channel_count = sample.channel_count; + latest->sample_format = sample.sample_format; + latest->scale_nano = sample.scale_nano; + latest->sequence = frame->sequence; + latest->timestamp_us = frame->timestamp_us; + latest->valid = true; + osf->last_sequence = frame->sequence; +mutex_unlock (&osf->latest_lock); + + indio_dev =osf_core_find_iio_dev (osf, sample.sensor_type, + sample.sensor_index); +if (!indio_dev) +return 0 ; + +return osf_iio_push_sample (indio_dev, values, sample.channel_count); }@@ -73,27 +260,47 @@ int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len)switch (frame.message_type) {case OSF_MSG_SENSOR_SAMPLE : - ret =osf_core_validate_sensor_sample (&frame); -break ; +return osf_core_handle_sensor_sample (osf, &frame);case OSF_MSG_DEVICE_STATUS : - ret =osf_core_validate_device_status (&frame); -break ; +return osf_core_handle_device_status (osf, &frame);case OSF_MSG_CAPABILITY_REPORT : - ret =osf_core_validate_capability_report (&frame); -break ; +return osf_core_handle_capability_report (osf, &frame);default :if (frame.message_type >=OSF_RESERVED_MSG_FIRST && frame.message_type <=OSF_RESERVED_MSG_LAST ) - ret =0 ;
drivers/iio/opensensorfusion/osf_core.c int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len) · line 275
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c +4
-5-else if (frame.message_type >=OSF_VENDOR_PRIVATE_FIRST ) - ret =0 ; -else - ret = -EOPNOTSUPP ; -break ; +return 0 ; +if (frame.message_type >=OSF_VENDOR_PRIVATE_FIRST ) +return 0 ; +return -EOPNOTSUPP ; }
See if you can rework original code to reduce the churn here.
Quoted 4 lines
drivers/iio/opensensorfusion/osf_core.c +4
-0+} + +int osf_core_read_latest_sample(struct osf_device *osf, u16 sensor_type, + u16 sensor_index, unsigned int channel,
drivers/iio/opensensorfusion/osf_core.c int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len) · line 289
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c +10
-0+ s32 *value) +{ +const struct osf_latest_sample *latest; +unsigned int i; +int ret = -ENODATA ; + +if (!osf || !value) +return -EINVAL ; + +mutex_lock (&osf->latest_lock);
Looks like a good place to use guard(mutex)(&osf->latest_lock); Remember to include cleanup.h
drivers/iio/opensensorfusion/osf_core.c int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len) · line 300
Quoted 13 lines
drivers/iio/opensensorfusion/osf_core.c +10
-2+for (i =0 ; i < osf->latest_sample_count; i++) { + latest = &osf->latest_samples[i]; +if (latest->sensor_type != sensor_type || + latest->sensor_index != sensor_index) +continue ; + +if (!latest->valid || channel >= latest->channel_count) +break ; -if (!ret) - osf->last_sequence = frame.sequence; + *value = latest->values[channel]; + ret =0 ;
With guard, you can return directly here.
drivers/iio/opensensorfusion/osf_core.c int osf_core_receive_frame(struct osf_device *osf, const u8 *buf, size_t len) · line 303
Quoted 3 lines
drivers/iio/opensensorfusion/osf_core.c +3
-0+break ; + } +mutex_unlock (&osf->latest_lock);
This gets handled automatically on leaving scope
Then if you get here you can just do
return -ENODATA;
Quoted 10 lines
drivers/iio/opensensorfusion/osf_core.c +0
-0return ret; }drivers/iio/opensensorfusion/osf_iio.c new file +0
-0
drivers/iio/opensensorfusion/osf_iio.c line 141
Quoted 5 lines
drivers/iio/opensensorfusion/osf_iio.c new file +4
-0+ +bool osf_iio_sensor_supported (u16 sensor_type,u16 channel_count) +{ +return !!osf_iio_find_sensor_spec (sensor_type, channel_count);
The !! is getting used a lot less in modern kernel code. Linus Torvalds once pointed out how hard it is to read. Maybe != 0 is clearer and let the compiler do the optimization if it wants.
Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +10
-0+} + +const char *osf_iio_sensor_name (u16 sensor_type) +{ +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (osf_iio_sensor_specs); i++) { +if (osf_iio_sensor_specs[i].sensor_type == sensor_type) +return osf_iio_sensor_specs[i].name; + }
drivers/iio/opensensorfusion/osf_iio.c
Quoted 10 lines
drivers/iio/opensensorfusion/osf_iio.c new file +7
-0+ +return NULL ; +} +} + +int osf_iio_push_sample (struct iio_dev *indio_dev,const s32 *values, +unsigned int channel_count)
As you are comparing it with the reported number of channels from spec->channel count I would match type with that (u16 I think)
Quoted 17 lines
drivers/iio/opensensorfusion/osf_iio.c new file +17
-0+{ +struct osf_iio_state *state =iio_priv (indio_dev); +s64 timestamp; + +if (channel_count != state->spec->channel_count) +return -EPROTO ; + +/* This is only a fast path; IIO rechecks buffer state while pushing. */ +if (!iio_buffer_enabled (indio_dev)) +return 0 ; + + timestamp =iio_get_time_ns (indio_dev); + +return iio_push_to_buffers_with_ts_unaligned (indio_dev, values, + channel_count *sizeof (*values), + timestamp); +}