* [PATCH] nvmem: core: Implement force_ro sysfs attribute
@ 2024-06-21 16:02 Marek Vasut
2024-06-22 6:49 ` Greg Kroah-Hartman
0 siblings, 1 reply; 2+ messages in thread
From: Marek Vasut @ 2024-06-21 16:02 UTC (permalink / raw)
To: linux-i2c
Cc: Marek Vasut, Srinivas Kandagatla, Bartosz Golaszewski,
Arnd Bergmann, Greg Kroah-Hartman
Implement "force_ro" sysfs attribute to allow users to set read-write
devices as read-only and back to read-write from userspace. The choice
of the name is based on MMC core 'force_ro' attribute.
This solves a situation where an AT24 I2C EEPROM with GPIO based nWP
signal may have to be occasionally updated. Such I2C EEPROM device is
usually set as read-only during most of the regular system operation,
but in case it has to be updated in a controlled manner, it could be
unlocked using this new "force_ro" sysfs attribute and then re-locked
again.
The "read-only" DT property and config->read_only configuration is
respected and is used to set default state of the device, read-only
or read-write, for devices which do implement .reg_write function.
For devices which do not implement .reg_write function, the device
is unconditionally read-only and the "force_ro" attribute is not
visible.
Signed-off-by: Marek Vasut <marex@denx.de>
---
Cc: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Cc: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-i2c@vger.kernel.org
---
drivers/nvmem/core.c | 44 +++++++++++++++++++++++++++++++++++++++
drivers/nvmem/internals.h | 1 +
2 files changed, 45 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index e1ec3b7200d7b..d263871b66dca 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -184,7 +184,30 @@ static ssize_t type_show(struct device *dev,
static DEVICE_ATTR_RO(type);
+static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+ return snprintf(buf, PAGE_SIZE, "%d\n", nvmem->read_only);
+}
+
+static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct nvmem_device *nvmem = to_nvmem_device(dev);
+ int ret = kstrtobool(buf, &nvmem->read_only);
+
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(force_ro);
+
static struct attribute *nvmem_attrs[] = {
+ &dev_attr_force_ro.attr,
&dev_attr_type.attr,
NULL,
};
@@ -299,6 +322,25 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
return nvmem_bin_attr_get_umode(nvmem);
}
+static umode_t nvmem_attr_is_visible(struct kobject *kobj,
+ struct attribute *attr, int i)
+{
+ struct device *dev = kobj_to_dev(kobj);
+ struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+ /*
+ * If the device has no .reg_write operation, do not allow
+ * configuration as read-write.
+ * If the device is set as read-only by configuration, it
+ * can be forced into read-write mode using the 'force_ro'
+ * attribute.
+ */
+ if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
+ return 0; /* Attribute not visible */
+
+ return attr->mode;
+}
+
static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
const char *id, int index);
@@ -355,6 +397,7 @@ static const struct attribute_group nvmem_bin_group = {
.bin_attrs = nvmem_bin_attributes,
.attrs = nvmem_attrs,
.is_bin_visible = nvmem_bin_attr_is_visible,
+ .is_visible = nvmem_attr_is_visible,
};
/* Cell attributes will be dynamically allocated */
@@ -971,6 +1014,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->read_only = device_property_present(config->dev, "read-only") ||
config->read_only || !nvmem->reg_write;
+ nvmem->default_read_only = nvmem->read_only;
#ifdef CONFIG_NVMEM_SYSFS
nvmem->dev.groups = nvmem_dev_groups;
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 18fed57270e5e..0667937ebb86b 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -16,6 +16,7 @@ struct nvmem_device {
int id;
struct kref refcnt;
size_t size;
+ bool default_read_only;
bool read_only;
bool root_only;
int flags;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] nvmem: core: Implement force_ro sysfs attribute
2024-06-21 16:02 [PATCH] nvmem: core: Implement force_ro sysfs attribute Marek Vasut
@ 2024-06-22 6:49 ` Greg Kroah-Hartman
0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2024-06-22 6:49 UTC (permalink / raw)
To: Marek Vasut
Cc: linux-i2c, Srinivas Kandagatla, Bartosz Golaszewski,
Arnd Bergmann
On Fri, Jun 21, 2024 at 06:02:14PM +0200, Marek Vasut wrote:
> Implement "force_ro" sysfs attribute to allow users to set read-write
> devices as read-only and back to read-write from userspace. The choice
> of the name is based on MMC core 'force_ro' attribute.
>
> This solves a situation where an AT24 I2C EEPROM with GPIO based nWP
> signal may have to be occasionally updated. Such I2C EEPROM device is
> usually set as read-only during most of the regular system operation,
> but in case it has to be updated in a controlled manner, it could be
> unlocked using this new "force_ro" sysfs attribute and then re-locked
> again.
>
> The "read-only" DT property and config->read_only configuration is
> respected and is used to set default state of the device, read-only
> or read-write, for devices which do implement .reg_write function.
> For devices which do not implement .reg_write function, the device
> is unconditionally read-only and the "force_ro" attribute is not
> visible.
>
> Signed-off-by: Marek Vasut <marex@denx.de>
> ---
> Cc: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
> Cc: Bartosz Golaszewski <brgl@bgdev.pl>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: linux-i2c@vger.kernel.org
> ---
> drivers/nvmem/core.c | 44 +++++++++++++++++++++++++++++++++++++++
> drivers/nvmem/internals.h | 1 +
> 2 files changed, 45 insertions(+)
No Documentation/ABI/ update for the new sysfs file?
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index e1ec3b7200d7b..d263871b66dca 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -184,7 +184,30 @@ static ssize_t type_show(struct device *dev,
>
> static DEVICE_ATTR_RO(type);
>
> +static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + struct nvmem_device *nvmem = to_nvmem_device(dev);
> +
> + return snprintf(buf, PAGE_SIZE, "%d\n", nvmem->read_only);
sysfs_emit() please.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-06-22 6:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-21 16:02 [PATCH] nvmem: core: Implement force_ro sysfs attribute Marek Vasut
2024-06-22 6:49 ` Greg Kroah-Hartman
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).