Skip to content

Commit 9d7eb23

Browse files
Marek Vasutgregkh
authored andcommitted
nvmem: core: Implement force_ro sysfs attribute
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 <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 08c367e commit 9d7eb23

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Documentation/ABI/stable/sysfs-bus-nvmem

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
What: /sys/bus/nvmem/devices/.../force_ro
2+
Date: June 2024
3+
KernelVersion: 6.11
4+
Contact: Marek Vasut <[email protected]>
5+
Description:
6+
This read/write attribute allows users to set read-write
7+
devices as read-only and back to read-write from userspace.
8+
This can be used to unlock and relock write-protection of
9+
devices which are generally locked, except during sporadic
10+
programming operation.
11+
Read returns '0' or '1' for read-write or read-only modes
12+
respectively.
13+
Write parses one of 'YyTt1NnFf0', or [oO][NnFf] for "on"
14+
and "off", i.e. what kstrbool() supports.
15+
Note: This file is only present if CONFIG_NVMEM_SYSFS
16+
is enabled.
17+
118
What: /sys/bus/nvmem/devices/.../nvmem
219
Date: July 2015
320
KernelVersion: 4.2

drivers/nvmem/core.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,30 @@ static ssize_t type_show(struct device *dev,
184184

185185
static DEVICE_ATTR_RO(type);
186186

187+
static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
188+
char *buf)
189+
{
190+
struct nvmem_device *nvmem = to_nvmem_device(dev);
191+
192+
return sysfs_emit(buf, "%d\n", nvmem->read_only);
193+
}
194+
195+
static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
196+
const char *buf, size_t count)
197+
{
198+
struct nvmem_device *nvmem = to_nvmem_device(dev);
199+
int ret = kstrtobool(buf, &nvmem->read_only);
200+
201+
if (ret < 0)
202+
return ret;
203+
204+
return count;
205+
}
206+
207+
static DEVICE_ATTR_RW(force_ro);
208+
187209
static struct attribute *nvmem_attrs[] = {
210+
&dev_attr_force_ro.attr,
188211
&dev_attr_type.attr,
189212
NULL,
190213
};
@@ -285,6 +308,25 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
285308
return nvmem_bin_attr_get_umode(nvmem);
286309
}
287310

311+
static umode_t nvmem_attr_is_visible(struct kobject *kobj,
312+
struct attribute *attr, int i)
313+
{
314+
struct device *dev = kobj_to_dev(kobj);
315+
struct nvmem_device *nvmem = to_nvmem_device(dev);
316+
317+
/*
318+
* If the device has no .reg_write operation, do not allow
319+
* configuration as read-write.
320+
* If the device is set as read-only by configuration, it
321+
* can be forced into read-write mode using the 'force_ro'
322+
* attribute.
323+
*/
324+
if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
325+
return 0; /* Attribute not visible */
326+
327+
return attr->mode;
328+
}
329+
288330
static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
289331
const char *id, int index);
290332

@@ -341,6 +383,7 @@ static const struct attribute_group nvmem_bin_group = {
341383
.bin_attrs = nvmem_bin_attributes,
342384
.attrs = nvmem_attrs,
343385
.is_bin_visible = nvmem_bin_attr_is_visible,
386+
.is_visible = nvmem_attr_is_visible,
344387
};
345388

346389
static const struct attribute_group *nvmem_dev_groups[] = {

0 commit comments

Comments
 (0)