linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
@ 2020-04-24  9:04 Björn Ardö
  2020-04-24 10:03 ` Wolfram Sang
  2020-04-24 11:13 ` Wolfram Sang
  0 siblings, 2 replies; 9+ messages in thread
From: Björn Ardö @ 2020-04-24  9:04 UTC (permalink / raw)
  To: wsa; +Cc: linux-i2c, linux-kernel, patrick, kernel, Björn Ardö

If the slave eeprom has a "firmware-name" in devicetree, then
pre-load the data in the eeprom with this file. Otherwise we
init the eeprom with 0xFF.

Signed-off-by: Björn Ardö <bjorn.ardo@axis.com>
---
 drivers/i2c/i2c-slave-eeprom.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/drivers/i2c/i2c-slave-eeprom.c b/drivers/i2c/i2c-slave-eeprom.c
index cb415b10642f..c846c96c25c9 100644
--- a/drivers/i2c/i2c-slave-eeprom.c
+++ b/drivers/i2c/i2c-slave-eeprom.c
@@ -18,6 +18,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/firmware.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -120,6 +121,26 @@ static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob
 	return count;
 }
 
+static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
+					 unsigned int size)
+{
+	const struct firmware *fw;
+	const char *eeprom_data;
+	int error = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
+
+	if (!error) {
+		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
+						    eeprom->buffer, size);
+		if (ret)
+			return ret;
+		release_firmware(fw);
+	} else {
+		/* An empty eeprom typically has all bits set to 1 */
+		memset(eeprom->buffer, 0xFF, size);
+	}
+	return 0;
+}
+
 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
 	struct eeprom_data *eeprom;
@@ -138,6 +159,10 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
 	spin_lock_init(&eeprom->buffer_lock);
 	i2c_set_clientdata(client, eeprom);
 
+	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
+	if (ret)
+		return ret;
+
 	sysfs_bin_attr_init(&eeprom->bin);
 	eeprom->bin.attr.name = "slave-eeprom";
 	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
-- 
2.11.0


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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24  9:04 [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data Björn Ardö
@ 2020-04-24 10:03 ` Wolfram Sang
  2020-04-24 10:06   ` Bjorn Ardo
  2020-04-24 11:13 ` Wolfram Sang
  1 sibling, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2020-04-24 10:03 UTC (permalink / raw)
  To: Björn Ardö; +Cc: linux-i2c, linux-kernel, patrick, kernel

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

On Fri, Apr 24, 2020 at 11:04:43AM +0200, Björn Ardö wrote:
> If the slave eeprom has a "firmware-name" in devicetree, then
> pre-load the data in the eeprom with this file. Otherwise we
> init the eeprom with 0xFF.
> 
> Signed-off-by: Björn Ardö <bjorn.ardo@axis.com>

I like it a lot, thanks! Maybe we could add a SoB from Patrick for his
0xff-suggestion (but keeping you as the patch author).

Is this okay for everyone?

> ---
>  drivers/i2c/i2c-slave-eeprom.c | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/drivers/i2c/i2c-slave-eeprom.c b/drivers/i2c/i2c-slave-eeprom.c
> index cb415b10642f..c846c96c25c9 100644
> --- a/drivers/i2c/i2c-slave-eeprom.c
> +++ b/drivers/i2c/i2c-slave-eeprom.c
> @@ -18,6 +18,7 @@
>   */
>  
>  #include <linux/bitfield.h>
> +#include <linux/firmware.h>
>  #include <linux/i2c.h>
>  #include <linux/init.h>
>  #include <linux/module.h>
> @@ -120,6 +121,26 @@ static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob
>  	return count;
>  }
>  
> +static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
> +					 unsigned int size)
> +{
> +	const struct firmware *fw;
> +	const char *eeprom_data;
> +	int error = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
> +
> +	if (!error) {
> +		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
> +						    eeprom->buffer, size);
> +		if (ret)
> +			return ret;
> +		release_firmware(fw);
> +	} else {
> +		/* An empty eeprom typically has all bits set to 1 */
> +		memset(eeprom->buffer, 0xFF, size);
> +	}
> +	return 0;
> +}
> +
>  static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
>  {
>  	struct eeprom_data *eeprom;
> @@ -138,6 +159,10 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
>  	spin_lock_init(&eeprom->buffer_lock);
>  	i2c_set_clientdata(client, eeprom);
>  
> +	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
> +	if (ret)
> +		return ret;
> +
>  	sysfs_bin_attr_init(&eeprom->bin);
>  	eeprom->bin.attr.name = "slave-eeprom";
>  	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
> -- 
> 2.11.0
> 

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

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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24 10:03 ` Wolfram Sang
@ 2020-04-24 10:06   ` Bjorn Ardo
  2020-04-24 15:32     ` Patrick Williams
  0 siblings, 1 reply; 9+ messages in thread
From: Bjorn Ardo @ 2020-04-24 10:06 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c, linux-kernel, patrick, kernel


On 4/24/20 12:03 PM, Wolfram Sang wrote:
> On Fri, Apr 24, 2020 at 11:04:43AM +0200, Björn Ardö wrote:
>> If the slave eeprom has a "firmware-name" in devicetree, then
>> pre-load the data in the eeprom with this file. Otherwise we
>> init the eeprom with 0xFF.
>>
>> Signed-off-by: Björn Ardö <bjorn.ardo@axis.com>
> I like it a lot, thanks! Maybe we could add a SoB from Patrick for his
> 0xff-suggestion (but keeping you as the patch author).
>
> Is this okay for everyone?


OK for me!


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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24  9:04 [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data Björn Ardö
  2020-04-24 10:03 ` Wolfram Sang
@ 2020-04-24 11:13 ` Wolfram Sang
  2020-04-24 11:22   ` Bjorn Ardo
  1 sibling, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2020-04-24 11:13 UTC (permalink / raw)
  To: Björn Ardö; +Cc: linux-i2c, linux-kernel, patrick, kernel

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


On second look, two questions:

> +	if (!error) {
> +		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
> +						    eeprom->buffer, size);
> +		if (ret)
> +			return ret;

Aren't we leaking 'fw' here?

Also, do we need 'error' and 'ret'? Can't we reuse one of them?

> +		release_firmware(fw);


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

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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24 11:13 ` Wolfram Sang
@ 2020-04-24 11:22   ` Bjorn Ardo
  2020-04-24 11:27     ` Wolfram Sang
  0 siblings, 1 reply; 9+ messages in thread
From: Bjorn Ardo @ 2020-04-24 11:22 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c, linux-kernel, patrick, kernel

On 4/24/20 1:13 PM, Wolfram Sang wrote:

> On second look, two questions:
>
>> +	if (!error) {
>> +		int ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
>> +						    eeprom->buffer, size);
>> +		if (ret)
>> +			return ret;
> Aren't we leaking 'fw' here?


As I can see in drivers/base/firmware_loader/main.c in function 
_request_firmware, then the fw will be released internally if it returns 
an error value.


> Also, do we need 'error' and 'ret'? Can't we reuse one of them?


Yes, I can fix that.


/BA


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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24 11:22   ` Bjorn Ardo
@ 2020-04-24 11:27     ` Wolfram Sang
  2020-04-24 11:31       ` Bjorn Ardo
  0 siblings, 1 reply; 9+ messages in thread
From: Wolfram Sang @ 2020-04-24 11:27 UTC (permalink / raw)
  To: Bjorn Ardo; +Cc: linux-i2c, linux-kernel, patrick, kernel

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


> As I can see in drivers/base/firmware_loader/main.c in function
> _request_firmware, then the fw will be released internally if it returns an
> error value.

Ouch, of course!

> > Also, do we need 'error' and 'ret'? Can't we reuse one of them?
> 
> Yes, I can fix that.

Great, thanks!


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

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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24 11:27     ` Wolfram Sang
@ 2020-04-24 11:31       ` Bjorn Ardo
  0 siblings, 0 replies; 9+ messages in thread
From: Bjorn Ardo @ 2020-04-24 11:31 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c, linux-kernel, patrick, kernel

You got a new patch (ignor the first one, I forgot to update the version 
number).

On 4/24/20 1:27 PM, Wolfram Sang wrote:
>> As I can see in drivers/base/firmware_loader/main.c in function
>> _request_firmware, then the fw will be released internally if it returns an
>> error value.
> Ouch, of course!
>
>>> Also, do we need 'error' and 'ret'? Can't we reuse one of them?
>> Yes, I can fix that.
> Great, thanks!
>

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

* Re: [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
  2020-04-24 10:06   ` Bjorn Ardo
@ 2020-04-24 15:32     ` Patrick Williams
  0 siblings, 0 replies; 9+ messages in thread
From: Patrick Williams @ 2020-04-24 15:32 UTC (permalink / raw)
  To: Bjorn Ardo; +Cc: Wolfram Sang, linux-i2c, linux-kernel, kernel

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

On Fri, Apr 24, 2020 at 12:06:44PM +0200, Bjorn Ardo wrote:
> 
> On 4/24/20 12:03 PM, Wolfram Sang wrote:
> > On Fri, Apr 24, 2020 at 11:04:43AM +0200, Björn Ardö wrote:
> >> If the slave eeprom has a "firmware-name" in devicetree, then
> >> pre-load the data in the eeprom with this file. Otherwise we
> >> init the eeprom with 0xFF.
> >>
> >> Signed-off-by: Björn Ardö <bjorn.ardo@axis.com>
> > I like it a lot, thanks! Maybe we could add a SoB from Patrick for his
> > 0xff-suggestion (but keeping you as the patch author).
> >
> > Is this okay for everyone?
> 
> 
> OK for me!
> 

Fine by me, also.

-- 
Patrick Williams

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

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

* [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data
@ 2020-04-24 11:29 Björn Ardö
  0 siblings, 0 replies; 9+ messages in thread
From: Björn Ardö @ 2020-04-24 11:29 UTC (permalink / raw)
  To: wsa; +Cc: linux-i2c, linux-kernel, patrick, kernel, Björn Ardö

If the slave eeprom has a "firmware-name" in devicetree, then
pre-load the data in the eeprom with this file. Otherwise we
init the eeprom with 0xFF.

Signed-off-by: Björn Ardö <bjorn.ardo@axis.com>
---
 drivers/i2c/i2c-slave-eeprom.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/drivers/i2c/i2c-slave-eeprom.c b/drivers/i2c/i2c-slave-eeprom.c
index cb415b10642f..b425cefea92c 100644
--- a/drivers/i2c/i2c-slave-eeprom.c
+++ b/drivers/i2c/i2c-slave-eeprom.c
@@ -18,6 +18,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/firmware.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -120,6 +121,26 @@ static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kob
 	return count;
 }
 
+static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
+					 unsigned int size)
+{
+	const struct firmware *fw;
+	const char *eeprom_data;
+	int error = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
+
+	if (!error) {
+		error = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
+						  eeprom->buffer, size);
+		if (error)
+			return error;
+		release_firmware(fw);
+	} else {
+		/* An empty eeprom typically has all bits set to 1 */
+		memset(eeprom->buffer, 0xFF, size);
+	}
+	return 0;
+}
+
 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
 	struct eeprom_data *eeprom;
@@ -138,6 +159,10 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
 	spin_lock_init(&eeprom->buffer_lock);
 	i2c_set_clientdata(client, eeprom);
 
+	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
+	if (ret)
+		return ret;
+
 	sysfs_bin_attr_init(&eeprom->bin);
 	eeprom->bin.attr.name = "slave-eeprom";
 	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
-- 
2.11.0


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

end of thread, other threads:[~2020-04-24 15:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-24  9:04 [PATCHv2] i2c: slave-eeprom: Make it possible to pre-load eeprom data Björn Ardö
2020-04-24 10:03 ` Wolfram Sang
2020-04-24 10:06   ` Bjorn Ardo
2020-04-24 15:32     ` Patrick Williams
2020-04-24 11:13 ` Wolfram Sang
2020-04-24 11:22   ` Bjorn Ardo
2020-04-24 11:27     ` Wolfram Sang
2020-04-24 11:31       ` Bjorn Ardo
2020-04-24 11:29 Björn Ardö

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