eeprom: at24: check if the chip is functional in probe()
Subsystems (2)
PATCH 1/1 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.
--- drivers/misc/eeprom/at24.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/misc/eeprom/at24.c +10 -0
@@ -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:
Signature · 1 line
2.7.4
Quoted 6 lines · 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.
Quoted 6 lines
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-ENODEVif the read fails.
That's basically OK...
Quoted 11 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- drivers/misc/eeprom/at24.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/misc/eeprom/at24.c +0
-0
drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 596
Quoted 4 lines
drivers/misc/eeprom/at24.c +1
-0@@ -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?
drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 784
Quoted 8 lines
drivers/misc/eeprom/at24.c +1
-0if (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?
drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 787
Quoted 3 lines
drivers/misc/eeprom/at24.c +3
-0+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.
Quoted 11 lines
drivers/misc/eeprom/at24.c +5
-0+nvmem_unregister (at24->nvmem); + err = -ENODEV ; + goto err_clients; + } +return 0 ; err_clients:Signature · 1 line
2.7.4
2016-08-10 16:07 GMT+02:00 Wolfram Sang <[email protected]>:
Quoted 41 lines · Bartosz Golaszewski
Quoted 6 lines
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.
Quoted 6 lines
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-ENODEVif the read fails.That's basically OK...
Quoted 11 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- drivers/misc/eeprom/at24.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/misc/eeprom/at24.c +0
-0drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 596
Quoted 4 lines
drivers/misc/eeprom/at24.c +1
-0@@ -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?
Quoted 7 lines
drivers/misc/eeprom/at24.c +0
-0if (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);
Quoted 4 lines
drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 784
Quoted 1 line
drivers/misc/eeprom/at24.c +1
-0+ 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
Quoted 17 lines
drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 787
Quoted 3 lines
drivers/misc/eeprom/at24.c +3
-0+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.
Quoted 11 lines
drivers/misc/eeprom/at24.c +5
-1+nvmem_unregister (at24->nvmem); + err = -ENODEV ; + goto err_clients; + } +return 0 ; err_clients: --2.7.4
Quoted 9 lines
Quoted 4 lines
Quoted 1 line
+ 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.