Linux IIO development
 help / color / mirror / Atom feed
From: Vasileios Amoiridis <vassilisamir@gmail.com>
To: jic23@kernel.org
Cc: dpfrey@gmail.com, himanshujha199640@gmail.com, lars@metafoo.de,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	mike.looijmans@topic.nl, vassilisamir@gmail.com
Subject: [PATCH v3 01/15] iio: chemical: bme680: Fix read/write ops to device by adding mutexes
Date: Mon, 10 Jun 2024 01:38:12 +0200	[thread overview]
Message-ID: <20240609233826.330516-2-vassilisamir@gmail.com> (raw)
In-Reply-To: <20240609233826.330516-1-vassilisamir@gmail.com>

Add mutexes in the {read/write}_raw() functions of the device to guard the
read/write of data from/to the device. This is necessary because for any
operation other than temperature, multiple reads need to take place from
the device. Even though regmap has a locking by itself, it won't protect us
from multiple applications trying to read at the same time temperature and
pressure since the pressure reading includes an internal temperature
reading and there is nothing to ensure that this temperature+pressure
reading will happen sequentially without any other operation interfering
in the meantime.

Fixes: 1b3bd8592780 ("iio: chemical: Add support for Bosch BME680 sensor")
Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
---
 drivers/iio/chemical/bme680_core.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 500f56834b01..a6bf689833da 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -10,6 +10,7 @@
  */
 #include <linux/acpi.h>
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/module.h>
@@ -52,6 +53,7 @@ struct bme680_calib {
 struct bme680_data {
 	struct regmap *regmap;
 	struct bme680_calib bme680;
+	struct mutex lock; /* Protect multiple serial R/W ops to device. */
 	u8 oversampling_temp;
 	u8 oversampling_press;
 	u8 oversampling_humid;
@@ -827,6 +829,8 @@ static int bme680_read_raw(struct iio_dev *indio_dev,
 {
 	struct bme680_data *data = iio_priv(indio_dev);
 
+	guard(mutex)(&data->lock);
+
 	switch (mask) {
 	case IIO_CHAN_INFO_PROCESSED:
 		switch (chan->type) {
@@ -871,6 +875,8 @@ static int bme680_write_raw(struct iio_dev *indio_dev,
 {
 	struct bme680_data *data = iio_priv(indio_dev);
 
+	guard(mutex)(&data->lock);
+
 	if (val2 != 0)
 		return -EINVAL;
 
@@ -967,6 +973,7 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
 		name = bme680_match_acpi_device(dev);
 
 	data = iio_priv(indio_dev);
+	mutex_init(&data->lock);
 	dev_set_drvdata(dev, indio_dev);
 	data->regmap = regmap;
 	indio_dev->name = name;
-- 
2.25.1


  reply	other threads:[~2024-06-09 23:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-09 23:38 [PATCH v3 00/15] iio: chemical: bme680: Driver cleanup Vasileios Amoiridis
2024-06-09 23:38 ` Vasileios Amoiridis [this message]
2024-06-09 23:38 ` [PATCH v3 02/15] iio: chemical: bme680: Fix typo in define Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 03/15] iio: chemical: bme680: Drop unnecessary casts and correct adc data types Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 04/15] iio: chemical: bme680: Remove remaining ACPI-only stuff Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 05/15] iio: chemical: bme680: Sort headers alphabetically Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 06/15] iio: chemical: bme680: Remove duplicate register read Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 07/15] iio: chemical: bme680: Use bulk reads for calibration data Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 08/15] iio: chemical: bme680: Allocate IIO device before chip initialization Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 09/15] iio: chemical: bme680: Add read buffers in read/write buffer union Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 10/15] iio: chemical: bme680: Make error checks consistent Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 11/15] iio: chemical: bme680: Modify startup procedure Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 12/15] iio: chemical: bme680: Move probe errors to dev_err_probe() Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 13/15] iio: chemical: bme680: Remove redundant gas configuration Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 14/15] iio: chemical: bme680: Move forced mode setup in ->read_raw() Vasileios Amoiridis
2024-06-09 23:38 ` [PATCH v3 15/15] iio: chemical: bme680: Refactorize reading functions Vasileios Amoiridis
2024-06-30 20:26 ` [PATCH v3 00/15] iio: chemical: bme680: Driver cleanup Vasileios Amoiridis
2024-07-01 12:44   ` Jonathan Cameron
2024-07-01 17:00     ` Vasileios Amoiridis
2024-07-07 16:41       ` Jonathan Cameron

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=20240609233826.330516-2-vassilisamir@gmail.com \
    --to=vassilisamir@gmail.com \
    --cc=dpfrey@gmail.com \
    --cc=himanshujha199640@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike.looijmans@topic.nl \
    /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