linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] iio: adc: mcp3422: fix locking scope
@ 2020-08-31 12:59 Angelo Compagnucci
  2020-08-31 14:10 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Angelo Compagnucci @ 2020-08-31 12:59 UTC (permalink / raw)
  To: linux-iio; +Cc: Angelo Compagnucci, Angelo Compagnucci

From: Angelo Compagnucci <angelo.compagnucci@gmail.com>

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")
Signed-off-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
 drivers/iio/adc/mcp3422.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
index d86c0b5d80a3..f96f0cecbcde 100644
--- a/drivers/iio/adc/mcp3422.c
+++ b/drivers/iio/adc/mcp3422.c
@@ -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.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] iio: adc: mcp3422: fix locking scope
  2020-08-31 12:59 [PATCH v3] iio: adc: mcp3422: fix locking scope Angelo Compagnucci
@ 2020-08-31 14:10 ` Jonathan Cameron
  2020-09-01  8:53   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2020-08-31 14:10 UTC (permalink / raw)
  To: Angelo Compagnucci; +Cc: linux-iio, Angelo Compagnucci

On Mon, 31 Aug 2020 14:59:47 +0200
Angelo Compagnucci <angelo.compagnucci@gmail.com> wrote:

> From: Angelo Compagnucci <angelo.compagnucci@gmail.com>
> 
> 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")
> Signed-off-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
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

>  drivers/iio/adc/mcp3422.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
> index d86c0b5d80a3..f96f0cecbcde 100644
> --- a/drivers/iio/adc/mcp3422.c
> +++ b/drivers/iio/adc/mcp3422.c
> @@ -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,


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] iio: adc: mcp3422: fix locking scope
  2020-08-31 14:10 ` Jonathan Cameron
@ 2020-09-01  8:53   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2020-09-01  8:53 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: Angelo Compagnucci, linux-iio, Angelo Compagnucci

On Mon, 31 Aug 2020 15:10:11 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> On Mon, 31 Aug 2020 14:59:47 +0200
> Angelo Compagnucci <angelo.compagnucci@gmail.com> wrote:
> 
> > From: Angelo Compagnucci <angelo.compagnucci@gmail.com>
> > 
> > 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")
> > Signed-off-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
> > Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> > ---  
> 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
gregkh@linuxfoundation.org to the cc list.  If he prefers it that way I'll ack
and Greg can pick it up directly.

Jonathan

> 
> Jonathan
> 
> >  drivers/iio/adc/mcp3422.c | 16 ++++++++++------
> >  1 file changed, 10 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
> > index d86c0b5d80a3..f96f0cecbcde 100644
> > --- a/drivers/iio/adc/mcp3422.c
> > +++ b/drivers/iio/adc/mcp3422.c
> > @@ -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,  
> 



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-01  8:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-08-31 12:59 [PATCH v3] iio: adc: mcp3422: fix locking scope Angelo Compagnucci
2020-08-31 14:10 ` Jonathan Cameron
2020-09-01  8:53   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).