Skip to contentSkip to conversation

iio: adc: mcp3422: fix locking scope

PATCH In review Angelo Compagnucci 3 participants 3 messages Aug 31, 2020 – Sep 1, 2020 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: adc: mcp3422: fix locking scope

From: Angelo Compagnucci <[email protected]>

Locking should be held for the entire reading sequence involving setting the channel, waiting for the channel switch and reading from the channel.
If not, reading from a channel can result mixing with the reading from another channel.

Fixes 07914c84ba30 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC")
---
 drivers/iio/adc/mcp3422.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/iio/adc/mcp3422.c +10 -6
index d86c0b5d80a3..f96f0cecbcde 100644
@@ -96,16 +96,12 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
 {
 	int ret;
 
-	mutex_lock(&adc->lock);
-
 	ret = i2c_master_send(adc->i2c, &newconfig, 1);
 	if (ret > 0) {
 		adc->config = newconfig;
 		ret = 0;
 	}
 
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
@@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 	u8 config;
 	u8 req_channel = channel->channel;
 
+	mutex_lock(&adc->lock);
+
 	if (req_channel != MCP3422_CHANNEL(adc->config)) {
 		config = adc->config;
 		config &= ~MCP3422_CHANNEL_MASK;
@@ -145,12 +143,18 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 		config &= ~MCP3422_PGA_MASK;
 		config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
 		ret = mcp3422_update_config(adc, config);
-		if (ret < 0)
+		if (ret < 0) {
+			mutex_unlock(&adc->lock);
 			return ret;
+		}
 		msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
 	}
 
-	return mcp3422_read(adc, value, &config);
+	ret = mcp3422_read(adc, value, &config);
+
+	mutex_unlock(&adc->lock);
+
+	return ret;
 }
 
 static int mcp3422_read_raw(struct iio_dev *iio,
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · On Mon, 31 Aug 2020 14:59:47 +0200 Angelo Compagnucci…

From: Angelo Compagnucci <[email protected]>

Locking should be held for the entire reading sequence involving setting the channel, waiting for the channel switch and reading from the channel.
If not, reading from a channel can result mixing with the reading from another channel.

Quoted 4 lines
Fixes 07914c84ba30 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC")

---

Hi Angelo

Change log?

Given I've just sent a pull request including v2, we are probably now in a mess if we actually need to use v3.

Jonathan

Quoted 54 lines
 drivers/iio/adc/mcp3422.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/iio/adc/mcp3422.c +10 -6
index d86c0b5d80a3..f96f0cecbcde 100644
@@ -96,16 +96,12 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
 {
 	int ret;
 
-	mutex_lock(&adc->lock);
-
 	ret = i2c_master_send(adc->i2c, &newconfig, 1);
 	if (ret > 0) {
 		adc->config = newconfig;
 		ret = 0;
 	}
 
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
@@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 	u8 config;
 	u8 req_channel = channel->channel;
 
+	mutex_lock(&adc->lock);
+
 	if (req_channel != MCP3422_CHANNEL(adc->config)) {
 		config = adc->config;
 		config &= ~MCP3422_CHANNEL_MASK;
@@ -145,12 +143,18 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 		config &= ~MCP3422_PGA_MASK;
 		config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
 		ret = mcp3422_update_config(adc, config);
-		if (ret < 0)
+		if (ret < 0) {
+			mutex_unlock(&adc->lock);
 			return ret;
+		}
 		msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
 	}
 
-	return mcp3422_read(adc, value, &config);
+	ret = mcp3422_read(adc, value, &config);
+
+	mutex_unlock(&adc->lock);
+
+	return ret;
 }
 
 static int mcp3422_read_raw(struct iio_dev *iio,
#2 lore raw
Quoted 15 lines · On Mon, 31 Aug 2020 15:10:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 8 lines · On Mon, 31 Aug 2020 14:59:47 +0200 Angelo Compagnucci…

From: Angelo Compagnucci <[email protected]>

Locking should be held for the entire reading sequence involving setting the channel, waiting for the channel switch and reading from the channel.
If not, reading from a channel can result mixing with the reading from another channel.

Quoted 4 lines
Fixes 07914c84ba30 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC")

---

Quoted 6 lines

Hi Angelo

Change log?

Given I've just sent a pull request including v2, we are probably now in a mess if we actually need to use v3.

Hi Angelo,

Just found the discussion with Julia.

Unfortunately that went to my work account and yesterday was a holiday in the UK so I missed it.

Could you spin a patch with the difference between v2 and v3 asap and add [email protected] to the cc list. If he prefers it that way I'll ack and Greg can pick it up directly.

Jonathan

Quoted 58 lines

Jonathan

Quoted 54 lines
 drivers/iio/adc/mcp3422.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/iio/adc/mcp3422.c +10 -6
index d86c0b5d80a3..f96f0cecbcde 100644
@@ -96,16 +96,12 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
 {
 	int ret;
 
-	mutex_lock(&adc->lock);
-
 	ret = i2c_master_send(adc->i2c, &newconfig, 1);
 	if (ret > 0) {
 		adc->config = newconfig;
 		ret = 0;
 	}
 
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
@@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 	u8 config;
 	u8 req_channel = channel->channel;
 
+	mutex_lock(&adc->lock);
+
 	if (req_channel != MCP3422_CHANNEL(adc->config)) {
 		config = adc->config;
 		config &= ~MCP3422_CHANNEL_MASK;
@@ -145,12 +143,18 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 		config &= ~MCP3422_PGA_MASK;
 		config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
 		ret = mcp3422_update_config(adc, config);
-		if (ret < 0)
+		if (ret < 0) {
+			mutex_unlock(&adc->lock);
 			return ret;
+		}
 		msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
 	}
 
-	return mcp3422_read(adc, value, &config);
+	ret = mcp3422_read(adc, value, &config);
+
+	mutex_unlock(&adc->lock);
+
+	return ret;
 }
 
 static int mcp3422_read_raw(struct iio_dev *iio,