ac7dda1bdf42980613fd29180375c28594dcaa48
[openwrt/staging/xback.git] /
1 From 9d7eb234ac7a56b88aea8a52ed81553a730fe25c Mon Sep 17 00:00:00 2001
2 From: Marek Vasut <marex@denx.de>
3 Date: Fri, 5 Jul 2024 08:48:52 +0100
4 Subject: [PATCH] nvmem: core: Implement force_ro sysfs attribute
5
6 Implement "force_ro" sysfs attribute to allow users to set read-write
7 devices as read-only and back to read-write from userspace. The choice
8 of the name is based on MMC core 'force_ro' attribute.
9
10 This solves a situation where an AT24 I2C EEPROM with GPIO based nWP
11 signal may have to be occasionally updated. Such I2C EEPROM device is
12 usually set as read-only during most of the regular system operation,
13 but in case it has to be updated in a controlled manner, it could be
14 unlocked using this new "force_ro" sysfs attribute and then re-locked
15 again.
16
17 The "read-only" DT property and config->read_only configuration is
18 respected and is used to set default state of the device, read-only
19 or read-write, for devices which do implement .reg_write function.
20 For devices which do not implement .reg_write function, the device
21 is unconditionally read-only and the "force_ro" attribute is not
22 visible.
23
24 Signed-off-by: Marek Vasut <marex@denx.de>
25 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
26 Link: https://lore.kernel.org/r/20240705074852.423202-16-srinivas.kandagatla@linaro.org
27 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
28 ---
29 Documentation/ABI/stable/sysfs-bus-nvmem | 17 ++++++++++
30 drivers/nvmem/core.c | 43 ++++++++++++++++++++++++
31 2 files changed, 60 insertions(+)
32
33 --- a/Documentation/ABI/stable/sysfs-bus-nvmem
34 +++ b/Documentation/ABI/stable/sysfs-bus-nvmem
35 @@ -1,3 +1,20 @@
36 +What: /sys/bus/nvmem/devices/.../force_ro
37 +Date: June 2024
38 +KernelVersion: 6.11
39 +Contact: Marek Vasut <marex@denx.de>
40 +Description:
41 + This read/write attribute allows users to set read-write
42 + devices as read-only and back to read-write from userspace.
43 + This can be used to unlock and relock write-protection of
44 + devices which are generally locked, except during sporadic
45 + programming operation.
46 + Read returns '0' or '1' for read-write or read-only modes
47 + respectively.
48 + Write parses one of 'YyTt1NnFf0', or [oO][NnFf] for "on"
49 + and "off", i.e. what kstrbool() supports.
50 + Note: This file is only present if CONFIG_NVMEM_SYSFS
51 + is enabled.
52 +
53 What: /sys/bus/nvmem/devices/.../nvmem
54 Date: July 2015
55 KernelVersion: 4.2
56 --- a/drivers/nvmem/core.c
57 +++ b/drivers/nvmem/core.c
58 @@ -185,7 +185,30 @@ static ssize_t type_show(struct device *
59
60 static DEVICE_ATTR_RO(type);
61
62 +static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
63 + char *buf)
64 +{
65 + struct nvmem_device *nvmem = to_nvmem_device(dev);
66 +
67 + return sysfs_emit(buf, "%d\n", nvmem->read_only);
68 +}
69 +
70 +static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
71 + const char *buf, size_t count)
72 +{
73 + struct nvmem_device *nvmem = to_nvmem_device(dev);
74 + int ret = kstrtobool(buf, &nvmem->read_only);
75 +
76 + if (ret < 0)
77 + return ret;
78 +
79 + return count;
80 +}
81 +
82 +static DEVICE_ATTR_RW(force_ro);
83 +
84 static struct attribute *nvmem_attrs[] = {
85 + &dev_attr_force_ro.attr,
86 &dev_attr_type.attr,
87 NULL,
88 };
89 @@ -286,6 +309,25 @@ static umode_t nvmem_bin_attr_is_visible
90 return nvmem_bin_attr_get_umode(nvmem);
91 }
92
93 +static umode_t nvmem_attr_is_visible(struct kobject *kobj,
94 + struct attribute *attr, int i)
95 +{
96 + struct device *dev = kobj_to_dev(kobj);
97 + struct nvmem_device *nvmem = to_nvmem_device(dev);
98 +
99 + /*
100 + * If the device has no .reg_write operation, do not allow
101 + * configuration as read-write.
102 + * If the device is set as read-only by configuration, it
103 + * can be forced into read-write mode using the 'force_ro'
104 + * attribute.
105 + */
106 + if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
107 + return 0; /* Attribute not visible */
108 +
109 + return attr->mode;
110 +}
111 +
112 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
113 const char *id, int index);
114
115 @@ -342,6 +384,7 @@ static const struct attribute_group nvme
116 .bin_attrs = nvmem_bin_attributes,
117 .attrs = nvmem_attrs,
118 .is_bin_visible = nvmem_bin_attr_is_visible,
119 + .is_visible = nvmem_attr_is_visible,
120 };
121
122 static const struct attribute_group *nvmem_dev_groups[] = {