From: Jinseob Kim <kimjinseob88@gmail.com>
To: jic23@kernel.org, linux-iio@vger.kernel.org
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
linux-kernel@vger.kernel.org, rdunlap@infradead.org,
joshua.crofts1@gmail.com, u.kleine-koenig@baylibre.com,
julianbraha@gmail.com, robh@kernel.org, krzk+dt@kernel.org,
conor+dt@kernel.org, grondon@gmail.com,
devicetree@vger.kernel.org, corbet@lwn.net,
skhan@linuxfoundation.org, linux-doc@vger.kernel.org
Subject: [PATCH v8 4/5] iio: osf: add authenticated stream parser
Date: Thu, 20 Aug 2026 14:06:07 +0900 [thread overview]
Message-ID: <20260820050608.5440-5-kimjinseob88@gmail.com> (raw)
In-Reply-To: <20260820050608.5440-1-kimjinseob88@gmail.com>
Add a UART byte-stream parser for Open Sensor Fusion frames.
The parser searches for the OSF0 wire magic, keeps partial frames
buffered, checks header length and payload bounds, and passes complete
candidate frames to a registered frame callback.
Candidates rejected before authentication drop only the current head
byte before resynchronizing, so a corrupted unauthenticated payload length
cannot make the parser skip later valid frames. CRC-valid authenticated
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 authenticated outcomes and framing failures.
Signed-off-by: Jinseob Kim <kimjinseob88@gmail.com>
---
MAINTAINERS | 1 +
drivers/iio/opensensorfusion/osf_stream.c | 231 ++++++++++++++++++++++
drivers/iio/opensensorfusion/osf_stream.h | 53 +++++
3 files changed, 285 insertions(+)
create mode 100644 drivers/iio/opensensorfusion/osf_stream.c
create mode 100644 drivers/iio/opensensorfusion/osf_stream.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 76fa860a7b91..093b569de12e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -20299,6 +20299,7 @@ S: Maintained
F: Documentation/devicetree/bindings/iio/opensensorfusion,osf.yaml
F: Documentation/iio/open-sensor-fusion.rst
F: drivers/iio/opensensorfusion/osf_protocol.*
+F: drivers/iio/opensensorfusion/osf_stream.*
K: opensensorfusion
OPENCOMPUTE PTP CLOCK DRIVER
diff --git a/drivers/iio/opensensorfusion/osf_stream.c b/drivers/iio/opensensorfusion/osf_stream.c
new file mode 100644
index 000000000000..7ce45e631648
--- /dev/null
+++ b/drivers/iio/opensensorfusion/osf_stream.c
@@ -0,0 +1,231 @@
+// 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 authenticated;
+ * 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 authenticated frame. */
+ stream->stats.authenticated_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 authenticated 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_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;
+ stream->len = 0;
+ memset(&stream->stats, 0, sizeof(stream->stats));
+}
+
+void osf_stream_reset(struct osf_stream *stream)
+{
+ if (!stream)
+ return;
+
+ 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->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 authenticated
+ * 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;
+}
diff --git a/drivers/iio/opensensorfusion/osf_stream.h b/drivers/iio/opensensorfusion/osf_stream.h
new file mode 100644
index 000000000000..c13f4e3af18f
--- /dev/null
+++ b/drivers/iio/opensensorfusion/osf_stream.h
@@ -0,0 +1,53 @@
+/* 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
+
+/**
+ * enum osf_stream_frame_result - authenticated frame callback result
+ * @OSF_STREAM_FRAME_HANDLED: frame was processed successfully
+ * @OSF_STREAM_FRAME_IGNORED: frame was valid but unsupported or ignored
+ * @OSF_STREAM_FRAME_REJECTED: authenticated application processing failed
+ *
+ * A frame callback returns a negative errno only when a candidate could not
+ * be authenticated and the parser may perform one-byte resynchronization.
+ * Every nonnegative result must be one of these values. Such a result means
+ * the CRC-valid frame boundary is trusted, so the parser must consume the
+ * full frame.
+ */
+enum osf_stream_frame_result {
+ OSF_STREAM_FRAME_HANDLED,
+ OSF_STREAM_FRAME_IGNORED,
+ OSF_STREAM_FRAME_REJECTED,
+};
+
+struct osf_stream_stats {
+ u64 authenticated_frames;
+ u64 handled_frames;
+ u64 ignored_frames;
+ u64 rejected_frames;
+ u64 bad_magic_resyncs;
+ u64 bad_crc_frames;
+ u64 dropped_bytes;
+};
+
+struct osf_stream {
+ int (*receive_frame)(void *context, const u8 *buf, size_t len);
+ void *frame_context;
+ u8 buf[OSF_STREAM_MAX_FRAME_LEN];
+ size_t len;
+ struct osf_stream_stats stats;
+};
+
+void osf_stream_init(struct osf_stream *stream,
+ int (*receive_frame)(void *context, const u8 *buf,
+ size_t len),
+ void *frame_context);
+void osf_stream_reset(struct osf_stream *stream);
+int osf_stream_receive_bytes(struct osf_stream *stream,
+ const u8 *buf, size_t len);
+
+#endif
--
2.43.0
next prev parent reply other threads:[~2026-08-20 5:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-20 5:06 [PATCH v8 0/5] iio: add Open Sensor Fusion IIO driver Jinseob Kim
2026-08-20 5:06 ` [PATCH v8 1/5] dt-bindings: iio: add Open Sensor Fusion device Jinseob Kim
2026-08-20 5:06 ` [PATCH v8 2/5] Documentation: iio: add Open Sensor Fusion driver overview Jinseob Kim
2026-08-20 5:06 ` [PATCH v8 3/5] iio: osf: add protocol decoding Jinseob Kim
2026-08-20 5:06 ` Jinseob Kim [this message]
2026-08-20 5:06 ` [PATCH v8 5/5] iio: osf: add UART IIO driver Jinseob Kim
2026-08-20 5:27 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260820050608.5440-5-kimjinseob88@gmail.com \
--to=kimjinseob88@gmail.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=grondon@gmail.com \
--cc=jic23@kernel.org \
--cc=joshua.crofts1@gmail.com \
--cc=julianbraha@gmail.com \
--cc=krzk+dt@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=rdunlap@infradead.org \
--cc=robh@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=u.kleine-koenig@baylibre.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox