generic: 6.1: backport qca808x LED support patch
[openwrt/openwrt.git] / target / linux / generic / backport-6.1 / 819-v6.8-0006-nvmem-core-Expose-cells-through-sysfs.patch
1 From 0331c611949fffdf486652450901a4dc52bc5cca Mon Sep 17 00:00:00 2001
2 From: Miquel Raynal <miquel.raynal@bootlin.com>
3 Date: Fri, 15 Dec 2023 11:15:34 +0000
4 Subject: [PATCH] nvmem: core: Expose cells through sysfs
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 The binary content of nvmem devices is available to the user so in the
10 easiest cases, finding the content of a cell is rather easy as it is
11 just a matter of looking at a known and fixed offset. However, nvmem
12 layouts have been recently introduced to cope with more advanced
13 situations, where the offset and size of the cells is not known in
14 advance or is dynamic. When using layouts, more advanced parsers are
15 used by the kernel in order to give direct access to the content of each
16 cell, regardless of its position/size in the underlying
17 device. Unfortunately, these information are not accessible by users,
18 unless by fully re-implementing the parser logic in userland.
19
20 Let's expose the cells and their content through sysfs to avoid these
21 situations. Of course the relevant NVMEM sysfs Kconfig option must be
22 enabled for this support to be available.
23
24 Not all nvmem devices expose cells. Indeed, the .bin_attrs attribute
25 group member will be filled at runtime only when relevant and will
26 remain empty otherwise. In this case, as the cells attribute group will
27 be empty, it will not lead to any additional folder/file creation.
28
29 Exposed cells are read-only. There is, in practice, everything in the
30 core to support a write path, but as I don't see any need for that, I
31 prefer to keep the interface simple (and probably safer). The interface
32 is documented as being in the "testing" state which means we can later
33 add a write attribute if though relevant.
34
35 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
36 Tested-by: Rafał Miłecki <rafal@milecki.pl>
37 Tested-by: Chen-Yu Tsai <wenst@chromium.org>
38 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
39 Link: https://lore.kernel.org/r/20231215111536.316972-9-srinivas.kandagatla@linaro.org
40 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
41 ---
42 drivers/nvmem/core.c | 135 +++++++++++++++++++++++++++++++++++++-
43 drivers/nvmem/internals.h | 1 +
44 2 files changed, 135 insertions(+), 1 deletion(-)
45
46 --- a/drivers/nvmem/core.c
47 +++ b/drivers/nvmem/core.c
48 @@ -300,6 +300,43 @@ static umode_t nvmem_bin_attr_is_visible
49 return nvmem_bin_attr_get_umode(nvmem);
50 }
51
52 +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
53 + const char *id, int index);
54 +
55 +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
56 + struct bin_attribute *attr, char *buf,
57 + loff_t pos, size_t count)
58 +{
59 + struct nvmem_cell_entry *entry;
60 + struct nvmem_cell *cell = NULL;
61 + size_t cell_sz, read_len;
62 + void *content;
63 +
64 + entry = attr->private;
65 + cell = nvmem_create_cell(entry, entry->name, 0);
66 + if (IS_ERR(cell))
67 + return PTR_ERR(cell);
68 +
69 + if (!cell)
70 + return -EINVAL;
71 +
72 + content = nvmem_cell_read(cell, &cell_sz);
73 + if (IS_ERR(content)) {
74 + read_len = PTR_ERR(content);
75 + goto destroy_cell;
76 + }
77 +
78 + read_len = min_t(unsigned int, cell_sz - pos, count);
79 + memcpy(buf, content + pos, read_len);
80 + kfree(content);
81 +
82 +destroy_cell:
83 + kfree_const(cell->id);
84 + kfree(cell);
85 +
86 + return read_len;
87 +}
88 +
89 /* default read/write permissions */
90 static struct bin_attribute bin_attr_rw_nvmem = {
91 .attr = {
92 @@ -321,11 +358,21 @@ static const struct attribute_group nvme
93 .is_bin_visible = nvmem_bin_attr_is_visible,
94 };
95
96 +/* Cell attributes will be dynamically allocated */
97 +static struct attribute_group nvmem_cells_group = {
98 + .name = "cells",
99 +};
100 +
101 static const struct attribute_group *nvmem_dev_groups[] = {
102 &nvmem_bin_group,
103 NULL,
104 };
105
106 +static const struct attribute_group *nvmem_cells_groups[] = {
107 + &nvmem_cells_group,
108 + NULL,
109 +};
110 +
111 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
112 .attr = {
113 .name = "eeprom",
114 @@ -381,6 +428,68 @@ static void nvmem_sysfs_remove_compat(st
115 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
116 }
117
118 +static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
119 +{
120 + struct bin_attribute **cells_attrs, *attrs;
121 + struct nvmem_cell_entry *entry;
122 + unsigned int ncells = 0, i = 0;
123 + int ret = 0;
124 +
125 + mutex_lock(&nvmem_mutex);
126 +
127 + if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) {
128 + nvmem_cells_group.bin_attrs = NULL;
129 + goto unlock_mutex;
130 + }
131 +
132 + /* Allocate an array of attributes with a sentinel */
133 + ncells = list_count_nodes(&nvmem->cells);
134 + cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1,
135 + sizeof(struct bin_attribute *), GFP_KERNEL);
136 + if (!cells_attrs) {
137 + ret = -ENOMEM;
138 + goto unlock_mutex;
139 + }
140 +
141 + attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
142 + if (!attrs) {
143 + ret = -ENOMEM;
144 + goto unlock_mutex;
145 + }
146 +
147 + /* Initialize each attribute to take the name and size of the cell */
148 + list_for_each_entry(entry, &nvmem->cells, node) {
149 + sysfs_bin_attr_init(&attrs[i]);
150 + attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
151 + "%s@%x", entry->name,
152 + entry->offset);
153 + attrs[i].attr.mode = 0444;
154 + attrs[i].size = entry->bytes;
155 + attrs[i].read = &nvmem_cell_attr_read;
156 + attrs[i].private = entry;
157 + if (!attrs[i].attr.name) {
158 + ret = -ENOMEM;
159 + goto unlock_mutex;
160 + }
161 +
162 + cells_attrs[i] = &attrs[i];
163 + i++;
164 + }
165 +
166 + nvmem_cells_group.bin_attrs = cells_attrs;
167 +
168 + ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups);
169 + if (ret)
170 + goto unlock_mutex;
171 +
172 + nvmem->sysfs_cells_populated = true;
173 +
174 +unlock_mutex:
175 + mutex_unlock(&nvmem_mutex);
176 +
177 + return ret;
178 +}
179 +
180 #else /* CONFIG_NVMEM_SYSFS */
181
182 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
183 @@ -740,11 +849,25 @@ static int nvmem_add_cells_from_fixed_la
184
185 int nvmem_layout_register(struct nvmem_layout *layout)
186 {
187 + int ret;
188 +
189 if (!layout->add_cells)
190 return -EINVAL;
191
192 /* Populate the cells */
193 - return layout->add_cells(&layout->nvmem->dev, layout->nvmem);
194 + ret = layout->add_cells(&layout->nvmem->dev, layout->nvmem);
195 + if (ret)
196 + return ret;
197 +
198 +#ifdef CONFIG_NVMEM_SYSFS
199 + ret = nvmem_populate_sysfs_cells(layout->nvmem);
200 + if (ret) {
201 + nvmem_device_remove_all_cells(layout->nvmem);
202 + return ret;
203 + }
204 +#endif
205 +
206 + return 0;
207 }
208 EXPORT_SYMBOL_GPL(nvmem_layout_register);
209
210 @@ -903,10 +1026,20 @@ struct nvmem_device *nvmem_register(cons
211 if (rval)
212 goto err_remove_dev;
213
214 +#ifdef CONFIG_NVMEM_SYSFS
215 + rval = nvmem_populate_sysfs_cells(nvmem);
216 + if (rval)
217 + goto err_destroy_layout;
218 +#endif
219 +
220 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
221
222 return nvmem;
223
224 +#ifdef CONFIG_NVMEM_SYSFS
225 +err_destroy_layout:
226 + nvmem_destroy_layout(nvmem);
227 +#endif
228 err_remove_dev:
229 device_del(&nvmem->dev);
230 err_remove_cells:
231 --- a/drivers/nvmem/internals.h
232 +++ b/drivers/nvmem/internals.h
233 @@ -32,6 +32,7 @@ struct nvmem_device {
234 struct gpio_desc *wp_gpio;
235 struct nvmem_layout *layout;
236 void *priv;
237 + bool sysfs_cells_populated;
238 };
239
240 #if IS_ENABLED(CONFIG_OF)