Re: [PATCH RFC v2 5/7] iio: osf: add UART serdev transport
From: Jonathan Cameron
Date: Thu May 28 2026 - 10:07:41 EST
On Sun, 24 May 2026 17:53:10 +0900
Jinseob Kim <kimjinseob88@xxxxxxxxx> wrote:
> Add the serdev receive path that feeds decoded OSF0 frames to the core.
>
> Signed-off-by: Jinseob Kim <kimjinseob88@xxxxxxxxx>
Various things inline.
> diff --git a/drivers/iio/opensensorfusion/Kconfig b/drivers/iio/opensensorfusion/Kconfig
> new file mode 100644
> index 000000000..360f25b4f
> --- /dev/null
> +++ b/drivers/iio/opensensorfusion/Kconfig
> @@ -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 OSF0 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.
Why is help text talking about a patch?
> diff --git a/drivers/iio/opensensorfusion/osf_core.c b/drivers/iio/opensensorfusion/osf_core.c
> new file mode 100644
> index 000000000..c867b3158
> --- /dev/null
> +++ b/drivers/iio/opensensorfusion/osf_core.c
> @@ -0,0 +1,107 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <linux/errno.h>
> +#include <linux/string.h>
> +#include <linux/types.h>
> +
> +#include "osf_core.h"
> +#include "osf_protocol.h"
> +
> +#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));
You zero them memory before passing to this. That seems like a sensible
pattern in which case this memset is unneeded.
> + osf->dev = dev;
> +}
> +
> +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;
If these can happen add a comment on why. If not remove them as overly cautious
checking.
> +
> + 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);
Perhaps check ret in each of these and return early if set. Then we only
do the shared path below on success.
> + 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;
Given there is nothing else to do on error, return -EOPNOTSUPP; perhaps.
> + break;
> + }
> +
> + if (!ret)
> + osf->last_sequence = frame.sequence;
> +
> + return ret;
> +}
> diff --git a/drivers/iio/opensensorfusion/osf_serdev.c b/drivers/iio/opensensorfusion/osf_serdev.c
> new file mode 100644
> index 000000000..f121089ed
> --- /dev/null
> +++ b/drivers/iio/opensensorfusion/osf_serdev.c
> +
> +static int osf_serdev_probe(struct serdev_device *serdev)
> +{
> + struct osf_serdev *osf_uart;
> + unsigned int baudrate;
> + int ret;
> +
> + osf_uart = devm_kzalloc(&serdev->dev, sizeof(*osf_uart), GFP_KERNEL);
> + if (!osf_uart)
> + return -ENOMEM;
> +
> + osf_uart->serdev = serdev;
> + osf_core_init(&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 = 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);
Given these don't match up with things in probe() please add some comments
to explain what they are undoing.
> +}