All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] eeprom: at24: check if the chip is functional in probe()
@ 2016-08-10 13:54 Bartosz Golaszewski
  2016-08-10 14:07 ` Wolfram Sang
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2016-08-10 13:54 UTC (permalink / raw)
  To: Wolfram Sang, linux-i2c, LKML; +Cc: Bartosz Golaszewski

The at24 driver doesn't check if the chip is functional in its probe
function. This leads to instantiating devices that are not physically
present. For example the cape EEPROMs for BeagleBone Black are defined
in the device tree at four addresses on i2c2, but normally only one of
them is present.

If the userspace doesn't know the location in advance, it will need to
check if reading the nvmem attributes fails to determine which EEPROM
is actually there.

Try to read a single byte in probe() and bail-out with -ENODEV if the
read fails.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/misc/eeprom/at24.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index 3cdf8e1..ed1e4eb 100644
--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -593,6 +593,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 	struct at24_data *at24;
 	int err;
 	unsigned i, num_addresses;
+	char c;
 
 	if (client->dev.platform_data) {
 		chip = *(struct at24_platform_data *)client->dev.platform_data;
@@ -780,6 +781,15 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 	if (chip.setup)
 		chip.setup(at24->nvmem, chip.context);
 
+	err = at24_read(at24, 0, &c, 1);
+	if (err) {
+		dev_err(&client->dev,
+			"error reading the test byte from EEPROM: %d\n", err);
+		nvmem_unregister(at24->nvmem);
+		err = -ENODEV;
+		goto err_clients;
+	}
+
 	return 0;
 
 err_clients:
-- 
2.7.4

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

* Re: [PATCH] eeprom: at24: check if the chip is functional in probe()
  2016-08-10 13:54 [PATCH] eeprom: at24: check if the chip is functional in probe() Bartosz Golaszewski
@ 2016-08-10 14:07 ` Wolfram Sang
  2016-08-11 15:09   ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2016-08-10 14:07 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: linux-i2c, LKML

[-- Attachment #1: Type: text/plain, Size: 1945 bytes --]

On Wed, Aug 10, 2016 at 03:54:17PM +0200, Bartosz Golaszewski wrote:
> The at24 driver doesn't check if the chip is functional in its probe
> function. This leads to instantiating devices that are not physically
> present. For example the cape EEPROMs for BeagleBone Black are defined
> in the device tree at four addresses on i2c2, but normally only one of
> them is present.
> 
> If the userspace doesn't know the location in advance, it will need to
> check if reading the nvmem attributes fails to determine which EEPROM
> is actually there.
> 
> Try to read a single byte in probe() and bail-out with -ENODEV if the
> read fails.

That's basically OK...

> 
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> ---
>  drivers/misc/eeprom/at24.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
> index 3cdf8e1..ed1e4eb 100644
> --- a/drivers/misc/eeprom/at24.c
> +++ b/drivers/misc/eeprom/at24.c
> @@ -593,6 +593,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
>  	struct at24_data *at24;
>  	int err;
>  	unsigned i, num_addresses;
> +	char c;

u8?

>  
>  	if (client->dev.platform_data) {
>  		chip = *(struct at24_platform_data *)client->dev.platform_data;
> @@ -780,6 +781,15 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
>  	if (chip.setup)
>  		chip.setup(at24->nvmem, chip.context);
>  
> +	err = at24_read(at24, 0, &c, 1);

Can't we do this before registering dummy clients and nvmem registration?

> +	if (err) {
> +		dev_err(&client->dev,
> +			"error reading the test byte from EEPROM: %d\n", err);

I don't think we should print an error in case of ENODEV.

> +		nvmem_unregister(at24->nvmem);
> +		err = -ENODEV;
> +		goto err_clients;
> +	}
> +
>  	return 0;
>  
>  err_clients:
> -- 
> 2.7.4
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH] eeprom: at24: check if the chip is functional in probe()
  2016-08-10 14:07 ` Wolfram Sang
@ 2016-08-11 15:09   ` Bartosz Golaszewski
  2016-08-11 15:55     ` Wolfram Sang
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2016-08-11 15:09 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c, LKML

2016-08-10 16:07 GMT+02:00 Wolfram Sang <wsa@the-dreams.de>:
> On Wed, Aug 10, 2016 at 03:54:17PM +0200, Bartosz Golaszewski wrote:
>> The at24 driver doesn't check if the chip is functional in its probe
>> function. This leads to instantiating devices that are not physically
>> present. For example the cape EEPROMs for BeagleBone Black are defined
>> in the device tree at four addresses on i2c2, but normally only one of
>> them is present.
>>
>> If the userspace doesn't know the location in advance, it will need to
>> check if reading the nvmem attributes fails to determine which EEPROM
>> is actually there.
>>
>> Try to read a single byte in probe() and bail-out with -ENODEV if the
>> read fails.
>
> That's basically OK...
>
>>
>> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>> ---
>>  drivers/misc/eeprom/at24.c | 10 ++++++++++
>>  1 file changed, 10 insertions(+)
>>
>> diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
>> index 3cdf8e1..ed1e4eb 100644
>> --- a/drivers/misc/eeprom/at24.c
>> +++ b/drivers/misc/eeprom/at24.c
>> @@ -593,6 +593,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
>>       struct at24_data *at24;
>>       int err;
>>       unsigned i, num_addresses;
>> +     char c;
>
> u8?
>
>>
>>       if (client->dev.platform_data) {
>>               chip = *(struct at24_platform_data *)client->dev.platform_data;
>> @@ -780,6 +781,15 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
>>       if (chip.setup)
>>               chip.setup(at24->nvmem, chip.context);
>>
>> +     err = at24_read(at24, 0, &c, 1);
>
> Can't we do this before registering dummy clients and nvmem registration?
>

It should be ok for nvmem, but I'm not sure about the clients:
at24_translate_offset() will return one of the registered client
structures and though it should generally work for the first byte (it
would always be at24->client[0]), it won't be "rock solid" anymore.

Best regards,
Bartosz Golaszewski

>> +     if (err) {
>> +             dev_err(&client->dev,
>> +                     "error reading the test byte from EEPROM: %d\n", err);
>
> I don't think we should print an error in case of ENODEV.
>
>> +             nvmem_unregister(at24->nvmem);
>> +             err = -ENODEV;
>> +             goto err_clients;
>> +     }
>> +
>>       return 0;
>>
>>  err_clients:
>> --
>> 2.7.4
>>

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

* Re: [PATCH] eeprom: at24: check if the chip is functional in probe()
  2016-08-11 15:09   ` Bartosz Golaszewski
@ 2016-08-11 15:55     ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2016-08-11 15:55 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: linux-i2c, LKML

[-- Attachment #1: Type: text/plain, Size: 429 bytes --]

> >> +     err = at24_read(at24, 0, &c, 1);
> >
> > Can't we do this before registering dummy clients and nvmem registration?
> >
> 
> It should be ok for nvmem, but I'm not sure about the clients:
> at24_translate_offset() will return one of the registered client
> structures and though it should generally work for the first byte (it
> would always be at24->client[0]), it won't be "rock solid" anymore.

Agreed.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

end of thread, other threads:[~2016-08-11 15:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-10 13:54 [PATCH] eeprom: at24: check if the chip is functional in probe() Bartosz Golaszewski
2016-08-10 14:07 ` Wolfram Sang
2016-08-11 15:09   ` Bartosz Golaszewski
2016-08-11 15:55     ` Wolfram Sang

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.