linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] eeprom: at24: check if the chip is functional in probe()
@ 2016-08-12 11:32 Bartosz Golaszewski
  2016-08-14 22:19 ` Wolfram Sang
  0 siblings, 1 reply; 2+ messages in thread
From: Bartosz Golaszewski @ 2016-08-12 11:32 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>
---
v1 -> v2:
- renamed c to test_byte
- changed the test_byte type to u8 (was char)
- moved the check above the nvmem registration
- moved the i2c_set_clientdata() call above both the nvmem
  registration and the test byte read
- removed the error message

 drivers/misc/eeprom/at24.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index 3cdf8e1..051b147 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;
+	u8 test_byte;
 
 	if (client->dev.platform_data) {
 		chip = *(struct at24_platform_data *)client->dev.platform_data;
@@ -743,6 +744,18 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 		}
 	}
 
+	i2c_set_clientdata(client, at24);
+
+	/*
+	 * Perform a one-byte test read to verify that the
+	 * chip is functional.
+	 */
+	err = at24_read(at24, 0, &test_byte, 1);
+	if (err) {
+		err = -ENODEV;
+		goto err_clients;
+	}
+
 	at24->nvmem_config.name = dev_name(&client->dev);
 	at24->nvmem_config.dev = &client->dev;
 	at24->nvmem_config.read_only = !writable;
@@ -764,8 +777,6 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 		goto err_clients;
 	}
 
-	i2c_set_clientdata(client, at24);
-
 	dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
 		chip.byte_len, client->name,
 		writable ? "writable" : "read-only", at24->write_max);
-- 
2.7.4

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

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

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

On Fri, Aug 12, 2016 at 01:32:57PM +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.
> 
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Applied to for-next, thanks!


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

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

end of thread, other threads:[~2016-08-14 22:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-12 11:32 [PATCH v2] eeprom: at24: check if the chip is functional in probe() Bartosz Golaszewski
2016-08-14 22:19 ` Wolfram Sang

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).