Skip to content

Commit 2bc96d4

Browse files
vamoiridgregkh
authored andcommitted
iio: chemical: bme680: Fix read/write ops to device by adding mutexes
[ Upstream commit 77641e5 ] 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: 1b3bd85 ("iio: chemical: Add support for Bosch BME680 sensor") Signed-off-by: Vasileios Amoiridis <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jonathan Cameron <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 5d86a29 commit 2bc96d4

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

drivers/iio/chemical/bme680_core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
#include <linux/acpi.h>
1212
#include <linux/bitfield.h>
13+
#include <linux/cleanup.h>
1314
#include <linux/delay.h>
1415
#include <linux/device.h>
1516
#include <linux/module.h>
@@ -52,6 +53,7 @@ struct bme680_calib {
5253
struct bme680_data {
5354
struct regmap *regmap;
5455
struct bme680_calib bme680;
56+
struct mutex lock; /* Protect multiple serial R/W ops to device. */
5557
u8 oversampling_temp;
5658
u8 oversampling_press;
5759
u8 oversampling_humid;
@@ -827,6 +829,8 @@ static int bme680_read_raw(struct iio_dev *indio_dev,
827829
{
828830
struct bme680_data *data = iio_priv(indio_dev);
829831

832+
guard(mutex)(&data->lock);
833+
830834
switch (mask) {
831835
case IIO_CHAN_INFO_PROCESSED:
832836
switch (chan->type) {
@@ -871,6 +875,8 @@ static int bme680_write_raw(struct iio_dev *indio_dev,
871875
{
872876
struct bme680_data *data = iio_priv(indio_dev);
873877

878+
guard(mutex)(&data->lock);
879+
874880
if (val2 != 0)
875881
return -EINVAL;
876882

@@ -967,6 +973,7 @@ int bme680_core_probe(struct device *dev, struct regmap *regmap,
967973
name = bme680_match_acpi_device(dev);
968974

969975
data = iio_priv(indio_dev);
976+
mutex_init(&data->lock);
970977
dev_set_drvdata(dev, indio_dev);
971978
data->regmap = regmap;
972979
indio_dev->name = name;

0 commit comments

Comments
 (0)