Skip to content

Commit 00f0ea7

Browse files
brglWolfram Sang
authored andcommitted
eeprom: at24: check if the chip is functional in probe()
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 <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 56025e7 commit 00f0ea7

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

drivers/misc/eeprom/at24.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
593593
struct at24_data *at24;
594594
int err;
595595
unsigned i, num_addresses;
596+
u8 test_byte;
596597

597598
if (client->dev.platform_data) {
598599
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)
743744
}
744745
}
745746

747+
i2c_set_clientdata(client, at24);
748+
749+
/*
750+
* Perform a one-byte test read to verify that the
751+
* chip is functional.
752+
*/
753+
err = at24_read(at24, 0, &test_byte, 1);
754+
if (err) {
755+
err = -ENODEV;
756+
goto err_clients;
757+
}
758+
746759
at24->nvmem_config.name = dev_name(&client->dev);
747760
at24->nvmem_config.dev = &client->dev;
748761
at24->nvmem_config.read_only = !writable;
@@ -764,8 +777,6 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
764777
goto err_clients;
765778
}
766779

767-
i2c_set_clientdata(client, at24);
768-
769780
dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
770781
chip.byte_len, client->name,
771782
writable ? "writable" : "read-only", at24->write_max);

0 commit comments

Comments
 (0)