diff mbox series

eeprom: at24: correctly set the size for at24mac402

Message ID 20171127210613.9140-1-brgl@bgdev.pl
State Accepted
Delegated to: Bartosz Golaszewski
Headers show
Series eeprom: at24: correctly set the size for at24mac402 | expand

Commit Message

Bartosz Golaszewski Nov. 27, 2017, 9:06 p.m. UTC
There's an ilog2() expansion in AT24_DEVICE_MAGIC() which rounds down
the actual size of EUI-48 byte array in at24mac402 eeproms to 4 from 6,
making it impossible to read it all.

Fix it by manually adjusting the value in probe().

This patch contains a temporary fix that is suitable for stable
branches. Eventually we'll probably remove the call to ilog2() while
converting the magic values to actual structs.

Cc: stable@vger.kernel.org
Fixes: 0b813658c115 ("eeprom: at24: add support for at24mac series")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 drivers/misc/eeprom/at24.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Andy Shevchenko Nov. 28, 2017, 10:25 a.m. UTC | #1
On Mon, Nov 27, 2017 at 11:06 PM, Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> There's an ilog2() expansion in AT24_DEVICE_MAGIC() which rounds down
> the actual size of EUI-48 byte array in at24mac402 eeproms to 4 from 6,
> making it impossible to read it all.
>
> Fix it by manually adjusting the value in probe().
>
> This patch contains a temporary fix that is suitable for stable
> branches. Eventually we'll probably remove the call to ilog2() while
> converting the magic values to actual structs.

> +       /*
> +        * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
> +        * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
> +        *
> +        * Eventually we'll get rid of the magic values altoghether in favor of
> +        * real structs, but for now just manually set the right size.
> +        */
> +       if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)

I didn't check, but do we need to test for len == 4 at all?

> +               chip.byte_len = 6;
Bartosz Golaszewski Nov. 28, 2017, 10:44 a.m. UTC | #2
2017-11-28 11:25 GMT+01:00 Andy Shevchenko <andy.shevchenko@gmail.com>:
> On Mon, Nov 27, 2017 at 11:06 PM, Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>> There's an ilog2() expansion in AT24_DEVICE_MAGIC() which rounds down
>> the actual size of EUI-48 byte array in at24mac402 eeproms to 4 from 6,
>> making it impossible to read it all.
>>
>> Fix it by manually adjusting the value in probe().
>>
>> This patch contains a temporary fix that is suitable for stable
>> branches. Eventually we'll probably remove the call to ilog2() while
>> converting the magic values to actual structs.
>
>> +       /*
>> +        * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
>> +        * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
>> +        *
>> +        * Eventually we'll get rid of the magic values altoghether in favor of
>> +        * real structs, but for now just manually set the right size.
>> +        */
>> +       if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
>
> I didn't check, but do we need to test for len == 4 at all?
>
>> +               chip.byte_len = 6;
>
> --
> With Best Regards,
> Andy Shevchenko

Yes, because the fix is only relevant for at24mac402 whose MAC byte
array is 6 bytes long, while the one in at24mac602 is 8 bytes and the
driver doesn't truncate the byte_len for it.

Thanks,
Bartosz
Andy Shevchenko Nov. 28, 2017, 12:09 p.m. UTC | #3
On Mon, Nov 27, 2017 at 11:06 PM, Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> There's an ilog2() expansion in AT24_DEVICE_MAGIC() which rounds down
> the actual size of EUI-48 byte array in at24mac402 eeproms to 4 from 6,
> making it impossible to read it all.
>
> Fix it by manually adjusting the value in probe().
>
> This patch contains a temporary fix that is suitable for stable
> branches. Eventually we'll probably remove the call to ilog2() while
> converting the magic values to actual structs.
>

Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> Cc: stable@vger.kernel.org
> Fixes: 0b813658c115 ("eeprom: at24: add support for at24mac series")
> Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
> ---
>  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 e0b4b36ef010..783244b485cc 100644
> --- a/drivers/misc/eeprom/at24.c
> +++ b/drivers/misc/eeprom/at24.c
> @@ -730,6 +730,16 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
>                 dev_warn(&client->dev,
>                         "page_size looks suspicious (no power of 2)!\n");
>
> +       /*
> +        * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
> +        * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
> +        *
> +        * Eventually we'll get rid of the magic values altoghether in favor of
> +        * real structs, but for now just manually set the right size.
> +        */
> +       if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
> +               chip.byte_len = 6;
> +
>         /* Use I2C operations unless we're stuck with SMBus extensions. */
>         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
>                 if (chip.flags & AT24_FLAG_ADDR16)
> --
> 2.15.0
>
Heiner Kallweit Nov. 28, 2017, 7:39 p.m. UTC | #4
Am 28.11.2017 um 13:09 schrieb Andy Shevchenko:
> On Mon, Nov 27, 2017 at 11:06 PM, Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>> There's an ilog2() expansion in AT24_DEVICE_MAGIC() which rounds down
>> the actual size of EUI-48 byte array in at24mac402 eeproms to 4 from 6,
>> making it impossible to read it all.
>>
>> Fix it by manually adjusting the value in probe().
>>
>> This patch contains a temporary fix that is suitable for stable
>> branches. Eventually we'll probably remove the call to ilog2() while
>> converting the magic values to actual structs.
>>
> 
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> 
Looks good also to me.

>> Cc: stable@vger.kernel.org
>> Fixes: 0b813658c115 ("eeprom: at24: add support for at24mac series")
>> Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
>> ---
>>  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 e0b4b36ef010..783244b485cc 100644
>> --- a/drivers/misc/eeprom/at24.c
>> +++ b/drivers/misc/eeprom/at24.c
>> @@ -730,6 +730,16 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
>>                 dev_warn(&client->dev,
>>                         "page_size looks suspicious (no power of 2)!\n");
>>
>> +       /*
>> +        * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
>> +        * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
>> +        *
>> +        * Eventually we'll get rid of the magic values altoghether in favor of
>> +        * real structs, but for now just manually set the right size.
>> +        */
>> +       if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
>> +               chip.byte_len = 6;
>> +
>>         /* Use I2C operations unless we're stuck with SMBus extensions. */
>>         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
>>                 if (chip.flags & AT24_FLAG_ADDR16)
>> --
>> 2.15.0
>>
> 
> 
>
Claudiu Beznea Dec. 4, 2017, 12:36 p.m. UTC | #5
On 28.11.2017 12:44, Bartosz Golaszewski wrote:
> 2017-11-28 11:25 GMT+01:00 Andy Shevchenko <andy.shevchenko@gmail.com>:
>> On Mon, Nov 27, 2017 at 11:06 PM, Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>>> There's an ilog2() expansion in AT24_DEVICE_MAGIC() which rounds down
>>> the actual size of EUI-48 byte array in at24mac402 eeproms to 4 from 6,
>>> making it impossible to read it all.
>>>
>>> Fix it by manually adjusting the value in probe().
>>>
>>> This patch contains a temporary fix that is suitable for stable
>>> branches. Eventually we'll probably remove the call to ilog2() while
>>> converting the magic values to actual structs.
>>
>>> +       /*
>>> +        * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
>>> +        * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
>>> +        *
>>> +        * Eventually we'll get rid of the magic values altoghether in favor of
>>> +        * real structs, but for now just manually set the right size.
>>> +        */
>>> +       if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
>>
>> I didn't check, but do we need to test for len == 4 at all?
>>
>>> +               chip.byte_len = 6;
>>
>> --
>> With Best Regards,
>> Andy Shevchenko
> 
> Yes, because the fix is only relevant for at24mac402 whose MAC byte
> array is 6 bytes long, while the one in at24mac602 is 8 bytes and the
> driver doesn't truncate the byte_len for it.
From my point of view, I think all chips in at24_ids[] array should be checked
about restrictions regarding the size truncation (especially the ones from the
initial commit: "2b7a5056a0a7ff17d5d2 i2c: New-style EEPROM driver using device IDs"
which introduces AT24_DEVICE_MAGIC() macro)  and if none of them has such restrictions
we should get rid of the ilog2() restrictions.

Just my 2 cents...
Claudiu

> 
> Thanks,
> Bartosz
>
diff mbox series

Patch

diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index e0b4b36ef010..783244b485cc 100644
--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -730,6 +730,16 @@  static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 		dev_warn(&client->dev,
 			"page_size looks suspicious (no power of 2)!\n");
 
+	/*
+	 * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
+	 * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
+	 *
+	 * Eventually we'll get rid of the magic values altoghether in favor of
+	 * real structs, but for now just manually set the right size.
+	 */
+	if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
+		chip.byte_len = 6;
+
 	/* Use I2C operations unless we're stuck with SMBus extensions. */
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
 		if (chip.flags & AT24_FLAG_ADDR16)