* [PATCH v2 0/2] iio: chemical: Add driver and bindings for acd10
@ 2026-12-02 12:23 Akhilesh Patil
2026-12-02 12:24 ` [PATCH v2 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
2026-12-02 12:25 ` [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
0 siblings, 2 replies; 4+ messages in thread
From: Akhilesh Patil @ 2026-12-02 12:23 UTC (permalink / raw)
To: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
joshua.crofts1, maxwell, u.kleine-koenig
Cc: linux-iio, devicetree, linux-kernel, akhileshpatilvnit, akhilesh
This patch series adds support for Aosong ASAIR acd10 CO2 (Carbon
dioxide) sensor driver in IIO subsystem.
Patch 1: Adds bindings for this hardware
Patch 2: Adds driver code for acd10 under drivers/iio/chemical
Why new driver under iio/chemical ?
I have checked existing chemical drivers from iio/chemical.
Many of the drivers are for other vendors and are I2C based with
different register layout. They do not match with serial interface of
acd10 co2 sensor. ags02ma is the same vendor, but it is an I2C based
with a different protocol. There are few drivers with serial
interface - scd30, pms7003, sps30, and mhz19b. however their framing,
command positions, padding, checksum calculation, supported commands, reply etc,
all differ from acd10. Given the above differences from existing drivers,
it is my judgement to add a new driver for it.
Quick overview of acd10:
- This is CO2 sensor based on NDIR technology.
- Can measure CO2 concentration up to 5000ppm
- Supports serial interface over UART with 1200 baud rate.
Datasheet: https://www.es.co.th/Schemetic/PDF/ACD10.PDF
Protocol:
acd10 defines 4 types of UART frames - read co2, get software version,
get instrument number and set single point calibration.
Frame format: fixed header, length of data, command code, Actual data,
checksum. Response from the sensor follows same format.
Total length of each tx and rx frame for 4 commands is fixed and defined in
the datasheet. Driver uses this information to write and read from
sensor for each command over serdev interface.
Testing:
Driver is tested on Texas Instruments am62x sk board with sensor
connected on UART5 on 40pin IO expander. Additional 3.3 to 5v
bidirectional logic translator is used to interface with 3.3v IOs of
am62x. Values are read using iio sysfs interface. Also I have blown into
the sensor to observe sensor output increase due to CO2 in my exhale.
Waveforms for read co2, get sw rev and get ins num commands are
validated on logic analyzer.
Slight deviation from datasheet:
For command get software version, datasheet mentions frame length of 16
bytes including D11 reserved, however it is observed that sensor sends
15 bytes with length field 0xb (11 bytes of data), but actual data is 10
bytes. Hence, this is handled in the driver.
Scope of this series:
Driver supports reading co2, getting sw version and instrument number(serialnumber).
As sensor serial protocol is fairly simple, I have intentionally kept receiver
implementation simple. Single point calibration and preheat implementation is left for
future contributions and are maked as TODO items in the driver file.
Changes in v2:
- Fixed possibility of buffer overflow in acd10_receive_buf() by doing bound checks.
- Added regulator support in driver.
- Added missing MODULE_DEVICE_TABLE() for loadable kernel modules.
- Dropped device.h from include files.
- Initialized all enum acd10_cmds members explicitly.
- Used __must_hold markings for acd10_serdev_transfer() mutex usage.
- Protected functions acd10_get_sw_rev() and acd10_get_instrument_num() with mutex lock.
- Defined and used ACD10_FRAME_START_IDX and ACD10_FRAME_FIXED_CODE_IDX wherever applicable.
- Removed obvious comments from acd10_serdev_transfer().
- Used dev_dbg() instead of dev_info() wherever applicable to avoid more bloated kernel logs.
- Mapped instrument number to standard IIO ABI serialnumber and added sysfs attribute code to export it.
- Used guard() instead of scoped_guard() inside acd10_read_raw()
- Arranged _probe function's local variables as per reverse xmas tree convention.
- Used sizeof(*drv_state) instead of sizeof(struct acd10_state) inside _probe function.
- Used dev_err_probe() to handle err during probe wherever applicable.
- Clean up of devm_iio_device_register() call from _probe function.
- Added section "Why new driver" in cover letter of the patch series.
- Link to v1: https://lore.kernel.org/linux-iio/cover.1795326744.git.akhilesh.patil@linux.dev/
Akhilesh Patil (2):
dt-bindings: iio: chemical: Add aosong acd10
iio: chemical: acd10: Add driver for ACD10 CO2 sensor
.../bindings/iio/chemical/aosong,acd10.yaml | 34 ++
MAINTAINERS | 7 +
drivers/iio/chemical/Kconfig | 10 +
drivers/iio/chemical/Makefile | 3 +
drivers/iio/chemical/acd10.c | 389 ++++++++++++++++++
5 files changed, 443 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
create mode 100644 drivers/iio/chemical/acd10.c
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2] dt-bindings: iio: chemical: Add aosong acd10
2026-12-02 12:23 [PATCH v2 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
@ 2026-12-02 12:24 ` Akhilesh Patil
2026-12-02 12:25 ` [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
1 sibling, 0 replies; 4+ messages in thread
From: Akhilesh Patil @ 2026-12-02 12:24 UTC (permalink / raw)
To: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
joshua.crofts1, maxwell, u.kleine-koenig
Cc: linux-iio, devicetree, linux-kernel, akhileshpatilvnit, akhilesh
Add bindings for acd10 CO2 sensor. This sensor has UART serial interface
with 1200 baudrate and can measure CO2 concentrations in the range of 400
to 5000 ppm.
Signed-off-by: Akhilesh Patil <akhilesh.patil@linux.dev>
---
.../bindings/iio/chemical/aosong,acd10.yaml | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
diff --git a/Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml b/Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
new file mode 100644
index 000000000000..bd6bad01550f
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/chemical/aosong,acd10.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: aosong acd10 carbon dioxide sensor
+
+maintainers:
+ - Akhilesh Patil <akhilesh.patil@linux.dev>
+
+description:
+ aosong acd10 is NDIR CO2 sensor with measurement range of 400 to 5000 ppm.
+ It provides serial interface over UART communication with fixed baud rate of 1200.
+
+properties:
+ compatible:
+ const: aosong,acd10
+
+ vcc-supply: true
+
+required:
+ - compatible
+
+additionalProperties: false
+
+examples:
+ - |
+ serial {
+ co2-sensor {
+ compatible = "aosong,acd10";
+ };
+ };
+...
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor
2026-12-02 12:23 [PATCH v2 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
2026-12-02 12:24 ` [PATCH v2 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
@ 2026-12-02 12:25 ` Akhilesh Patil
2026-12-02 12:33 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Akhilesh Patil @ 2026-12-02 12:25 UTC (permalink / raw)
To: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
joshua.crofts1, maxwell, u.kleine-koenig
Cc: linux-iio, devicetree, linux-kernel, akhileshpatilvnit, akhilesh
Add driver for Aosong ASAIR ACD10 carbon dioxide sensor.
Use serdev interface to communicate with sensor over UART.
Implement read co2, get software revision, get instrument number
commands.
Tested on TI am62x sk board on UART5 with 1200 baud rate.
Signed-off-by: Akhilesh Patil <akhilesh.patil@linux.dev>
---
MAINTAINERS | 7 +
drivers/iio/chemical/Kconfig | 10 +
drivers/iio/chemical/Makefile | 3 +
drivers/iio/chemical/acd10.c | 389 ++++++++++++++++++++++++++++++++++
4 files changed, 409 insertions(+)
create mode 100644 drivers/iio/chemical/acd10.c
diff --git a/MAINTAINERS b/MAINTAINERS
index e8f06145fb54..a5d70cd11dda 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3743,6 +3743,13 @@ S: Supported
W: http://www.akm.com/
F: drivers/iio/magnetometer/ak8974.c
+AOSONG ACD10 CARBON DIOXIDE SENSOR DRIVER
+M: Akhilesh Patil <akhilesh.patil@linux.dev>
+L: linux-iio@vger.kernel.org
+S: Maintained
+F: Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
+F: drivers/iio/chemical/acd10.c
+
AOSONG AGS02MA TVOC SENSOR DRIVER
M: Anshul Dalal <anshulusr@gmail.com>
L: linux-iio@vger.kernel.org
diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig
index b22afa1f6d59..2bbf49135997 100644
--- a/drivers/iio/chemical/Kconfig
+++ b/drivers/iio/chemical/Kconfig
@@ -5,6 +5,16 @@
menu "Chemical Sensors"
+config ACD10
+ tristate "Aosong ACD10 CO2 gas sensor"
+ depends on SERIAL_DEV_BUS
+ help
+ Say Y here to build Aosong ASAIR ACD10 CO2 (carbon dioxide)
+ sensor driver.
+
+ To compile this ACD10 driver as a loadable kernel module,
+ choose M here: the module will be called acd10
+
config AOSONG_AGS02MA
tristate "Aosong AGS02MA TVOC sensor driver"
depends on I2C
diff --git a/drivers/iio/chemical/Makefile b/drivers/iio/chemical/Makefile
index 2287a00a6b75..2187f596728c 100644
--- a/drivers/iio/chemical/Makefile
+++ b/drivers/iio/chemical/Makefile
@@ -3,7 +3,10 @@
# Makefile for IIO chemical sensors
#
+CONTEXT_ANALYSIS_acd10.o := y
+
# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ACD10) += acd10.o
obj-$(CONFIG_AOSONG_AGS02MA) += ags02ma.o
obj-$(CONFIG_ATLAS_PH_SENSOR) += atlas-sensor.o
obj-$(CONFIG_ATLAS_EZO_SENSOR) += atlas-ezo-sensor.o
diff --git a/drivers/iio/chemical/acd10.c b/drivers/iio/chemical/acd10.c
new file mode 100644
index 000000000000..a77dde636ea7
--- /dev/null
+++ b/drivers/iio/chemical/acd10.c
@@ -0,0 +1,389 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ACD10 CO2 gas sensor driver
+ *
+ * Copyright (c) 2026 Akhilesh Patil <akhilesh.patil@linux.dev>
+ *
+ * Datasheet:
+ * https://www.es.co.th/Schemetic/PDF/ACD10.PDF
+ *
+ * TODO additional features:
+ * 1) single-point calibration feature.
+ * 2) Sensor preheat wait feature.
+ */
+
+#include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/completion.h>
+#include <linux/dev_printk.h>
+#include <linux/errno.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/types.h>
+#include <linux/jiffies.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/regulator/consumer.h>
+#include <linux/serdev.h>
+#include <linux/string.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+
+#define ACD10_SERIAL_BAUDRATE 1200
+#define ACD10_SERDEV_TIMEOUT msecs_to_jiffies(500)
+#define ACD10_FRAME_START 0xFE
+#define ACD10_FRAME_FIXED_CODE 0xA6
+#define ACD10_FRAME_START_IDX 0U
+#define ACD10_FRAME_FIXED_CODE_IDX 1U
+#define ACD10_FRAME_DATA_LEN_IDX 2U
+#define ACD10_FRAME_CMD_CODE_IDX 3U
+#define ACD10_FRAME_DATA_START_IDX 4U
+#define ACD10_FRAME_DATA_LEN_MAX 11U
+
+struct acd10_state {
+ struct serdev_device *serdev;
+
+ /* Lock to acquire during CMD/RESP sequence */
+ struct mutex lock;
+ struct completion buf_ready;
+
+ /* Single buffer to hold tx and rx data */
+ u8 buf[16];
+ u8 buf_idx;
+ u8 rx_expected_len;
+
+ u8 sw_rev[16];
+ u8 inst_num[16];
+};
+
+enum acd10_cmds {
+ ACD10_CMD_READ_CO2 = 0,
+ ACD10_CMD_CAL = 1,
+ ACD10_CMD_READ_SWREV = 2,
+ ACD10_CMD_READ_INS_NUM = 3,
+};
+
+struct acd10_cmd_info {
+ u8 cmd_code;
+ u8 tx_frame_len;
+ u8 tx_data_len;
+ u8 rx_frame_len;
+};
+
+static const struct acd10_cmd_info cmd_info[] = {
+ [ACD10_CMD_READ_CO2] = {
+ .cmd_code = 0x01,
+ .tx_frame_len = 5,
+ .tx_data_len = 0,
+ .rx_frame_len = 9,
+ },
+
+ [ACD10_CMD_CAL] = {
+ .cmd_code = 0x03,
+ .tx_frame_len = 7,
+ .tx_data_len = 2,
+ .rx_frame_len = 5,
+ },
+
+ [ACD10_CMD_READ_SWREV] = {
+ .cmd_code = 0x1E,
+ .tx_frame_len = 5,
+ .tx_data_len = 0,
+ /*
+ * Note: datasheet says sw rev response is D1-D11 with D11 reserved.
+ * However, sensor actually sends only D1-D10 (10 bytes).
+ * Hence, rx_frame_len is 15 and not 16.
+ */
+ .rx_frame_len = 15,
+ },
+
+ [ACD10_CMD_READ_INS_NUM] = {
+ .cmd_code = 0x1F,
+ .tx_frame_len = 5,
+ .tx_data_len = 0,
+ .rx_frame_len = 15,
+ }
+};
+
+static u8 acd10_get_checksum(u8 *buf, u8 len)
+{
+ u8 checksum = 0;
+
+ for (u8 i = ACD10_FRAME_FIXED_CODE_IDX; i < len - 1; i += 1)
+ checksum += buf[i];
+
+ return checksum;
+}
+
+/*
+ * Do transfer based on command and fill drv_state->buf with received data.
+ * Caller of this function should use mutex drv_state->lock.
+ */
+static int acd10_serdev_transfer(struct acd10_state *drv_state, enum acd10_cmds cmd)
+ __must_hold(&drv_state->lock)
+{
+ struct serdev_device *serdev = drv_state->serdev;
+ struct device *dev = &serdev->dev;
+ int ret;
+
+ /* Prepare transmit buffer along with checksum and send over serial */
+ drv_state->buf[ACD10_FRAME_START_IDX] = ACD10_FRAME_START;
+ drv_state->buf[ACD10_FRAME_FIXED_CODE_IDX] = ACD10_FRAME_FIXED_CODE;
+ drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] = cmd_info[cmd].tx_data_len;
+ drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] = cmd_info[cmd].cmd_code;
+ drv_state->buf[cmd_info[cmd].tx_frame_len - 1] =
+ acd10_get_checksum(drv_state->buf, cmd_info[cmd].tx_frame_len);
+ drv_state->buf_idx = 0;
+ drv_state->rx_expected_len = cmd_info[cmd].rx_frame_len;
+ reinit_completion(&drv_state->buf_ready);
+
+ ret = serdev_device_write(serdev, drv_state->buf, cmd_info[cmd].tx_frame_len,
+ ACD10_SERDEV_TIMEOUT);
+ if (ret < 0)
+ return ret;
+ if (ret != cmd_info[cmd].tx_frame_len)
+ return -EIO;
+
+ /* Wait till we receive expected number of bytes for sent cmd */
+ ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
+ ACD10_SERDEV_TIMEOUT);
+ if (ret < 0)
+ return ret;
+ if (!ret)
+ return -ETIMEDOUT;
+
+ if (drv_state->buf[ACD10_FRAME_START_IDX] != ACD10_FRAME_START ||
+ drv_state->buf[ACD10_FRAME_FIXED_CODE_IDX] != ACD10_FRAME_FIXED_CODE) {
+ dev_err(dev, "RX frame invalid\n");
+ return -EIO;
+ }
+
+ if (drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] > ACD10_FRAME_DATA_LEN_MAX)
+ return -EIO;
+
+ /* Validate command echo in the frame received from the sensor */
+ if (drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] != cmd_info[cmd].cmd_code)
+ return -EIO;
+
+ if (drv_state->buf[cmd_info[cmd].rx_frame_len - 1] !=
+ acd10_get_checksum(drv_state->buf, cmd_info[cmd].rx_frame_len)) {
+ dev_err(dev, "Checksum error\n");
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static int acd10_get_sw_rev(struct iio_dev *indio_dev)
+{
+ struct acd10_state *drv_state = iio_priv(indio_dev);
+ struct device *dev = &indio_dev->dev;
+ int ret;
+
+ guard(mutex)(&drv_state->lock);
+
+ ret = acd10_serdev_transfer(drv_state, ACD10_CMD_READ_SWREV);
+ if (ret)
+ return ret;
+
+ memcpy(drv_state->sw_rev, &drv_state->buf[ACD10_FRAME_DATA_START_IDX], 10);
+ drv_state->sw_rev[10] = '\0';
+
+ dev_dbg(dev, "Software revision: %s\n", drv_state->sw_rev);
+
+ return 0;
+}
+
+static int acd10_get_instrument_num(struct iio_dev *indio_dev)
+{
+ struct acd10_state *drv_state = iio_priv(indio_dev);
+ struct device *dev = &indio_dev->dev;
+ int ret;
+
+ guard(mutex)(&drv_state->lock);
+
+ ret = acd10_serdev_transfer(drv_state, ACD10_CMD_READ_INS_NUM);
+ if (ret)
+ return ret;
+
+ memcpy(drv_state->inst_num, &drv_state->buf[ACD10_FRAME_DATA_START_IDX], 10);
+ drv_state->inst_num[10] = '\0';
+
+ dev_dbg(dev, "Instrument number: %s\n", drv_state->inst_num);
+
+ return 0;
+}
+
+static size_t acd10_receive_buf(struct serdev_device *serdev,
+ const u8 *data, size_t len)
+{
+ struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
+ struct acd10_state *drv_state = iio_priv(indio_dev);
+ size_t consume = len;
+ int i = 0;
+
+ while (drv_state->rx_expected_len && consume > 0 &&
+ drv_state->buf_idx < sizeof(drv_state->buf)) {
+ drv_state->buf[drv_state->buf_idx++] = data[i++];
+ consume -= 1;
+ drv_state->rx_expected_len -= 1;
+ }
+
+ if (drv_state->rx_expected_len == 0) {
+ drv_state->buf_idx = 0;
+ complete(&drv_state->buf_ready);
+ }
+
+ return len;
+}
+
+static int acd10_read_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct acd10_state *drv_state = iio_priv(indio_dev);
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW: {
+ int ret;
+
+ guard(mutex)(&drv_state->lock);
+
+ ret = acd10_serdev_transfer(drv_state, ACD10_CMD_READ_CO2);
+ if (ret)
+ return ret;
+
+ *val = get_unaligned_be16(&drv_state->buf[ACD10_FRAME_DATA_START_IDX]);
+
+ return IIO_VAL_INT;
+ }
+
+ case IIO_CHAN_INFO_SCALE:
+ *val = 0;
+ *val2 = 100;
+ return IIO_VAL_INT_PLUS_MICRO;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static ssize_t serialnumber_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct acd10_state *drv_state = iio_priv(dev_to_iio_dev(dev));
+
+ return sysfs_emit(buf, "%s\n", drv_state->inst_num);
+}
+
+static IIO_DEVICE_ATTR_RO(serialnumber, 0);
+
+static struct attribute *acd10_attrs[] = {
+ &iio_dev_attr_serialnumber.dev_attr.attr,
+ NULL,
+};
+
+static const struct attribute_group acd10_attrs_group = {
+ .attrs = acd10_attrs,
+};
+
+static const struct iio_info acd10_info = {
+ .attrs = &acd10_attrs_group,
+ .read_raw = &acd10_read_raw,
+};
+
+static const struct iio_chan_spec acd10_channels[] = {
+ {
+ .type = IIO_CONCENTRATION,
+ .channel2 = IIO_MOD_CO2,
+ .modified = 1,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ },
+};
+
+static const struct serdev_device_ops acd10_ser_ops = {
+ .receive_buf = &acd10_receive_buf,
+ .write_wakeup = serdev_device_write_wakeup,
+};
+
+static int acd10_probe(struct serdev_device *serdev)
+{
+ struct device *dev = &serdev->dev;
+ struct acd10_state *drv_state;
+ struct iio_dev *indio_dev;
+ unsigned int baudrate;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*drv_state));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ indio_dev->name = "acd10";
+ indio_dev->channels = acd10_channels;
+ indio_dev->num_channels = ARRAY_SIZE(acd10_channels);
+ indio_dev->info = &acd10_info;
+
+ serdev_device_set_drvdata(serdev, indio_dev);
+
+ drv_state = iio_priv(indio_dev);
+ drv_state->serdev = serdev;
+
+ init_completion(&drv_state->buf_ready);
+ ret = devm_mutex_init(dev, &drv_state->lock);
+ if (ret)
+ return ret;
+
+ ret = devm_regulator_get_enable(dev, "vcc");
+ if (ret)
+ return ret;
+
+ serdev_device_set_client_ops(serdev, &acd10_ser_ops);
+ ret = devm_serdev_device_open(dev, serdev);
+ if (ret)
+ return dev_err_probe(dev, ret, "ACD10: serial device open failed\n");
+
+ baudrate = serdev_device_set_baudrate(serdev, ACD10_SERIAL_BAUDRATE);
+ dev_dbg(dev, "baud rate set to: %u\n", baudrate);
+ if (baudrate != ACD10_SERIAL_BAUDRATE)
+ return dev_err_probe(dev, -EIO, "baud rate expected: %u, got: %u\n",
+ ACD10_SERIAL_BAUDRATE, baudrate);
+
+ serdev_device_set_flow_control(serdev, false);
+ ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
+ if (ret)
+ return ret;
+
+ ret = acd10_get_sw_rev(indio_dev);
+ if (ret)
+ return ret;
+
+ ret = acd10_get_instrument_num(indio_dev);
+ if (ret)
+ return ret;
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct of_device_id acd10_of_match_table[] = {
+ { .compatible = "aosong,acd10", },
+ { }
+};
+MODULE_DEVICE_TABLE(of, acd10_of_match_table);
+
+static struct serdev_device_driver acd10_driver = {
+ .driver = {
+ .name = "acd10",
+ .of_match_table = acd10_of_match_table,
+ },
+ .probe = acd10_probe,
+};
+module_serdev_device_driver(acd10_driver);
+
+MODULE_AUTHOR("Akhilesh Patil");
+MODULE_DESCRIPTION("ACD10 CO2 gas sensor driver over serdev interface");
+MODULE_LICENSE("GPL");
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor
2026-12-02 12:25 ` [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
@ 2026-12-02 12:33 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-02 12:33 UTC (permalink / raw)
To: Akhilesh Patil; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] iio: chemical: acd10: shared TX/RX buffer race in
acd10_serdev_transfer()
--
commit 0d5d850b993452ce98f539b196f7b69e4ad1b6e8
Author: Akhilesh Patil <akhilesh.patil@linux.dev>
iio: chemical: acd10: Add driver for ACD10 CO2 sensor
This commit adds an IIO driver for the Aosong ASAIR ACD10 CO2 sensor
using the serdev UART interface.
> diff --git a/drivers/iio/chemical/acd10.c b/drivers/iio/chemical/acd10.c
> new file mode 100644
> index 0000000000000..a77dde636ea74
> --- /dev/null
> +++ b/drivers/iio/chemical/acd10.c
[ ... ]
> +static int acd10_serdev_transfer(struct acd10_state *drv_state, enum acd10_cmds cmd)
> + __must_hold(&drv_state->lock)
> +{
> + struct serdev_device *serdev = drv_state->serdev;
> + struct device *dev = &serdev->dev;
> + int ret;
> +
> + /* Prepare transmit buffer along with checksum and send over serial */
> + drv_state->buf[ACD10_FRAME_START_IDX] = ACD10_FRAME_START;
> + drv_state->buf[ACD10_FRAME_FIXED_CODE_IDX] = ACD10_FRAME_FIXED_CODE;
> + drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] = cmd_info[cmd].tx_data_len;
> + drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] = cmd_info[cmd].cmd_code;
> + drv_state->buf[cmd_info[cmd].tx_frame_len - 1] =
> + acd10_get_checksum(drv_state->buf, cmd_info[cmd].tx_frame_len);
> + drv_state->buf_idx = 0;
> + drv_state->rx_expected_len = cmd_info[cmd].rx_frame_len;
> + reinit_completion(&drv_state->buf_ready);
> +
> + ret = serdev_device_write(serdev, drv_state->buf, cmd_info[cmd].tx_frame_len,
> + ACD10_SERDEV_TIMEOUT);
> + if (ret < 0)
> + return ret;
> + if (ret != cmd_info[cmd].tx_frame_len)
> + return -EIO;
> +
> + /* Wait till we receive expected number of bytes for sent cmd */
> + ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
> + ACD10_SERDEV_TIMEOUT);
> + if (ret < 0)
> + return ret;
> + if (!ret)
> + return -ETIMEDOUT;
[ ... ]
> +static size_t acd10_receive_buf(struct serdev_device *serdev,
> + const u8 *data, size_t len)
> +{
> + struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
> + struct acd10_state *drv_state = iio_priv(indio_dev);
> + size_t consume = len;
> + int i = 0;
> +
> + while (drv_state->rx_expected_len && consume > 0 &&
> + drv_state->buf_idx < sizeof(drv_state->buf)) {
> + drv_state->buf[drv_state->buf_idx++] = data[i++];
> + consume -= 1;
> + drv_state->rx_expected_len -= 1;
> + }
> +
> + if (drv_state->rx_expected_len == 0) {
> + drv_state->buf_idx = 0;
> + complete(&drv_state->buf_ready);
> + }
> +
> + return len;
> +}
[Severity: Medium]
Can an interrupted or timed-out transfer leave drv_state->rx_expected_len
non-zero and allow acd10_receive_buf() to race on the shared drv_state->buf
during the next transfer?
At 1200 baud, a 9-byte response takes around 75 ms to arrive. If
wait_for_completion_interruptible_timeout() is interrupted by a signal
(or times out) while the response is in flight, acd10_serdev_transfer()
returns without resetting drv_state->rx_expected_len:
CPU0 kworker (tty flip buffer)
acd10_serdev_transfer()
rx_expected_len = 9
serdev_device_write(...)
wait_for_completion...()
-> -ERESTARTSYS
acd10_serdev_transfer()
buf[0] = ACD10_FRAME_START
...
acd10_receive_buf()
buf[buf_idx++] = data[i++]
buf[4] = acd10_get_checksum(buf)
serdev_device_write(buf) <-- sends corrupted TX frame
Because acd10_receive_buf() does not take drv_state->lock and shares
drv_state->buf with the transmit path, late bytes from the previous
response overwrite the outgoing frame or shift the receive buffer of subsequent
transfers out of alignment.
In addition, in acd10_receive_buf(), if an unexpected byte arrives when
drv_state->rx_expected_len is already 0, the while loop does not run, but
the if (drv_state->rx_expected_len == 0) check still calls
complete(&drv_state->buf_ready). And in acd10_serdev_transfer(),
drv_state->rx_expected_len is armed before reinit_completion() and
serdev_device_write(), so a completion that fires before
reinit_completion() is lost.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796211342.git.akhilesh.patil@linux.dev?part=2
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 12:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:23 [PATCH v2 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
2026-12-02 12:24 ` [PATCH v2 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
2026-12-02 12:25 ` [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
2026-12-02 12:33 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox