All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephan Gerhold <stephan@gerhold.net>
To: Andi Shyti <andi@etezian.org>
Cc: Linux Input <linux-input@vger.kernel.org>,
	Seung-Woo Kim <sw0312.kim@samsung.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Joonyoung Shim <jy0922.shim@samsung.com>
Subject: Re: [PATCH v2 1/2] Input: mms114 - use smbus functions whenever possible
Date: Mon, 21 Oct 2019 11:34:23 +0200	[thread overview]
Message-ID: <20191021093423.GA1116@gerhold.net> (raw)
In-Reply-To: <20191020202856.20287-2-andi@etezian.org>

Hi Andi,

Thanks for working on these patches!

Not sure if you saw my comment regarding your patch [1],
so I'll just repeat it properly inline here:

[1]: https://patchwork.kernel.org/patch/11178515/#22927311

On Sun, Oct 20, 2019 at 11:28:55PM +0300, Andi Shyti wrote:
> The exchange of data to and from the mms114 touchscreen never
> exceeds 256 bytes. In the worst case it goes up to 80 bytes in
> the interrupt handler while reading the events.
> 

i2c_smbus_read_i2c_block_data() is actually limited to
I2C_SMBUS_BLOCK_MAX = 32.

> Thus it's not needed to make use of custom read/write functions
> for accessing the i2c. Replace, whenever possible, the use of
> custom functions with the more standard smbus ones.
> 
> It's not possible only in one case, in the mms114_set_active()
> function where the 'cache_mode_control' variable is updated
> according to the value in the register 'MMS114_MODE_CONTROL'
> register.
> 
> Signed-off-by: Andi Shyti <andi@etezian.org>
> Tested-by: Seung-Woo Kim <sw0312.kim@samsung.com>
> ---
>  drivers/input/touchscreen/mms114.c | 32 +++++++++++++++++++-----------
>  1 file changed, 20 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/input/touchscreen/mms114.c b/drivers/input/touchscreen/mms114.c
> index a5ab774da4cc..170dcb5312b9 100644
> --- a/drivers/input/touchscreen/mms114.c
> +++ b/drivers/input/touchscreen/mms114.c
> @@ -204,14 +204,15 @@ static irqreturn_t mms114_interrupt(int irq, void *dev_id)
>  	}
>  	mutex_unlock(&input_dev->mutex);
>  
> -	packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE);
> +	packet_size = i2c_smbus_read_byte_data(data->client,
> +					       MMS114_PACKET_SIZE);
>  	if (packet_size <= 0)
>  		goto out;
>  
>  	touch_size = packet_size / MMS114_PACKET_NUM;
>  
> -	error = __mms114_read_reg(data, MMS114_INFORMATION, packet_size,
> -			(u8 *)touch);
> +	error = i2c_smbus_read_i2c_block_data(data->client, MMS114_INFORMATION,
> +					      packet_size, (u8 *)touch);

... and here we try to read up to 80 bytes, as you mentioned.

i2c_smbus_read_i2c_block_data() will silently fall back to reading only
32 bytes. Therefore, if we try to read more than 32 bytes here we will
later read uninitialized data.

With this change, if you use more than 4 fingers you can easily trigger
a situation where one of the fingers gets "stuck", together with:
  mms114 4-0048: Wrong touch type (0)

So we still need the custom functions here, or maybe avoid the problem
by using regmap instead.

>  	if (error < 0)
>  		goto out;
>  
> @@ -251,7 +252,8 @@ static int mms114_get_version(struct mms114_data *data)
>  
>  	switch (data->type) {
>  	case TYPE_MMS152:
> -		error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf);
> +		error = i2c_smbus_read_i2c_block_data(data->client,
> +						      MMS152_FW_REV, 3, buf);
>  		if (error)

i2c_smbus_read_i2c_block_data() returns the number of read bytes,
therefore this check will always fail.

It should be: if (error < 0)

>  			return error;
>  
> @@ -265,7 +267,8 @@ static int mms114_get_version(struct mms114_data *data)
>  		break;
>  
>  	case TYPE_MMS114:
> -		error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
> +		error = i2c_smbus_read_i2c_block_data(data->client,
> +						      MMS114_TSP_REV, 6, buf);
>  		if (error)

As above.

>  			return error;
>  
> @@ -297,30 +300,35 @@ static int mms114_setup_regs(struct mms114_data *data)
>  
>  	val = (props->max_x >> 8) & 0xf;
>  	val |= ((props->max_y >> 8) & 0xf) << 4;
> -	error = mms114_write_reg(data, MMS114_XY_RESOLUTION_H, val);
> +	error = i2c_smbus_write_byte_data(data->client,
> +					  MMS114_XY_RESOLUTION_H, val);
>  	if (error < 0)
>  		return error;
>  
>  	val = props->max_x & 0xff;
> -	error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
> +	error = i2c_smbus_write_byte_data(data->client,
> +					  MMS114_X_RESOLUTION, val);
>  	if (error < 0)
>  		return error;
>  
>  	val = props->max_x & 0xff;
> -	error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
> +	error = i2c_smbus_write_byte_data(data->client,
> +					  MMS114_Y_RESOLUTION, val);
>  	if (error < 0)
>  		return error;
>  
>  	if (data->contact_threshold) {
> -		error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD,
> -				data->contact_threshold);
> +		error = i2c_smbus_write_byte_data(data->client,
> +						  MMS114_CONTACT_THRESHOLD,
> +						  data->contact_threshold);
>  		if (error < 0)
>  			return error;
>  	}
>  
>  	if (data->moving_threshold) {
> -		error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD,
> -				data->moving_threshold);
> +		error = i2c_smbus_write_byte_data(data->client,
> +						  MMS114_MOVING_THRESHOLD,
> +						  data->moving_threshold);
>  		if (error < 0)
>  			return error;
>  	}
> -- 
> 2.24.0.rc0
> 

  reply	other threads:[~2019-10-21  9:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-20 20:28 [PATCH v2 0/2] Use smbus functions to communicate through i2c Andi Shyti
2019-10-20 20:28 ` [PATCH v2 1/2] Input: mms114 - use smbus functions whenever possible Andi Shyti
2019-10-21  9:34   ` Stephan Gerhold [this message]
2019-10-21 15:41     ` Andi Shyti
2019-10-21 16:26       ` Stephan Gerhold
2019-10-21 16:39         ` Andi Shyti
2019-10-22  3:21           ` Dmitry Torokhov
2019-10-22 11:18             ` Andi Shyti
2019-11-18 13:32               ` Stephan Gerhold
2019-12-04 16:47                 ` Stephan Gerhold
2019-10-20 20:28 ` [PATCH v2 2/2] Input: mms114 - get read of custm i2c read/write functions Andi Shyti

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=20191021093423.GA1116@gerhold.net \
    --to=stephan@gerhold.net \
    --cc=andi@etezian.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jy0922.shim@samsung.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sw0312.kim@samsung.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.