brcm2708: update linux 4.4 patches to latest version
[lede.git] / target / linux / brcm2708 / patches-4.4 / 0178-OF-DT-Overlay-configfs-interface.patch
1 From 46852dc03df7e271cedafb6405ab3b47fa57fc1f Mon Sep 17 00:00:00 2001
2 From: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
3 Date: Wed, 3 Dec 2014 13:23:28 +0200
4 Subject: [PATCH 178/304] OF: DT-Overlay configfs interface
5
6 This is a port of Pantelis Antoniou's v3 port that makes use of the
7 new upstreamed configfs support for binary attributes.
8
9 Original commit message:
10
11 Add a runtime interface to using configfs for generic device tree overlay
12 usage. With it its possible to use device tree overlays without having
13 to use a per-platform overlay manager.
14
15 Please see Documentation/devicetree/configfs-overlays.txt for more info.
16
17 Changes since v2:
18 - Removed ifdef CONFIG_OF_OVERLAY (since for now it's required)
19 - Created a documentation entry
20 - Slight rewording in Kconfig
21
22 Changes since v1:
23 - of_resolve() -> of_resolve_phandles().
24
25 Originally-signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
26 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
27 ---
28  Documentation/devicetree/configfs-overlays.txt |  31 +++
29  drivers/of/Kconfig                             |   7 +
30  drivers/of/Makefile                            |   1 +
31  drivers/of/configfs.c                          | 314 +++++++++++++++++++++++++
32  4 files changed, 353 insertions(+)
33  create mode 100644 Documentation/devicetree/configfs-overlays.txt
34  create mode 100644 drivers/of/configfs.c
35
36 --- /dev/null
37 +++ b/Documentation/devicetree/configfs-overlays.txt
38 @@ -0,0 +1,31 @@
39 +Howto use the configfs overlay interface.
40 +
41 +A device-tree configfs entry is created in /config/device-tree/overlays
42 +and and it is manipulated using standard file system I/O.
43 +Note that this is a debug level interface, for use by developers and
44 +not necessarily something accessed by normal users due to the
45 +security implications of having direct access to the kernel's device tree.
46 +
47 +* To create an overlay you mkdir the directory:
48 +
49 +       # mkdir /config/device-tree/overlays/foo
50 +
51 +* Either you echo the overlay firmware file to the path property file.
52 +
53 +       # echo foo.dtbo >/config/device-tree/overlays/foo/path
54 +
55 +* Or you cat the contents of the overlay to the dtbo file
56 +
57 +       # cat foo.dtbo >/config/device-tree/overlays/foo/dtbo
58 +
59 +The overlay file will be applied, and devices will be created/destroyed
60 +as required.
61 +
62 +To remove it simply rmdir the directory.
63 +
64 +       # rmdir /config/device-tree/overlays/foo
65 +
66 +The rationalle of the dual interface (firmware & direct copy) is that each is
67 +better suited to different use patterns. The firmware interface is what's
68 +intended to be used by hardware managers in the kernel, while the copy interface
69 +make sense for developers (since it avoids problems with namespaces).
70 --- a/drivers/of/Kconfig
71 +++ b/drivers/of/Kconfig
72 @@ -112,4 +112,11 @@ config OF_OVERLAY
73           While this option is selected automatically when needed, you can
74           enable it manually to improve device tree unit test coverage.
75  
76 +config OF_CONFIGFS
77 +       bool "Device Tree Overlay ConfigFS interface"
78 +       select CONFIGFS_FS
79 +       select OF_OVERLAY
80 +       help
81 +         Enable a simple user-space driven DT overlay interface.
82 +
83  endif # OF
84 --- a/drivers/of/Makefile
85 +++ b/drivers/of/Makefile
86 @@ -1,4 +1,5 @@
87  obj-y = base.o device.o platform.o
88 +obj-$(CONFIG_OF_CONFIGFS) += configfs.o
89  obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
90  obj-$(CONFIG_OF_FLATTREE) += fdt.o
91  obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o
92 --- /dev/null
93 +++ b/drivers/of/configfs.c
94 @@ -0,0 +1,314 @@
95 +/*
96 + * Configfs entries for device-tree
97 + *
98 + * Copyright (C) 2013 - Pantelis Antoniou <panto@antoniou-consulting.com>
99 + *
100 + * This program is free software; you can redistribute it and/or
101 + * modify it under the terms of the GNU General Public License
102 + * as published by the Free Software Foundation; either version
103 + * 2 of the License, or (at your option) any later version.
104 + */
105 +#include <linux/ctype.h>
106 +#include <linux/cpu.h>
107 +#include <linux/module.h>
108 +#include <linux/of.h>
109 +#include <linux/of_fdt.h>
110 +#include <linux/spinlock.h>
111 +#include <linux/slab.h>
112 +#include <linux/proc_fs.h>
113 +#include <linux/configfs.h>
114 +#include <linux/types.h>
115 +#include <linux/stat.h>
116 +#include <linux/limits.h>
117 +#include <linux/file.h>
118 +#include <linux/vmalloc.h>
119 +#include <linux/firmware.h>
120 +
121 +#include "of_private.h"
122 +
123 +struct cfs_overlay_item {
124 +       struct config_item      item;
125 +
126 +       char                    path[PATH_MAX];
127 +
128 +       const struct firmware   *fw;
129 +       struct device_node      *overlay;
130 +       int                     ov_id;
131 +
132 +       void                    *dtbo;
133 +       int                     dtbo_size;
134 +};
135 +
136 +static int create_overlay(struct cfs_overlay_item *overlay, void *blob)
137 +{
138 +       int err;
139 +
140 +       /* unflatten the tree */
141 +       of_fdt_unflatten_tree(blob, &overlay->overlay);
142 +       if (overlay->overlay == NULL) {
143 +               pr_err("%s: failed to unflatten tree\n", __func__);
144 +               err = -EINVAL;
145 +               goto out_err;
146 +       }
147 +       pr_debug("%s: unflattened OK\n", __func__);
148 +
149 +       /* mark it as detached */
150 +       of_node_set_flag(overlay->overlay, OF_DETACHED);
151 +
152 +       /* perform resolution */
153 +       err = of_resolve_phandles(overlay->overlay);
154 +       if (err != 0) {
155 +               pr_err("%s: Failed to resolve tree\n", __func__);
156 +               goto out_err;
157 +       }
158 +       pr_debug("%s: resolved OK\n", __func__);
159 +
160 +       err = of_overlay_create(overlay->overlay);
161 +       if (err < 0) {
162 +               pr_err("%s: Failed to create overlay (err=%d)\n",
163 +                               __func__, err);
164 +               goto out_err;
165 +       }
166 +       overlay->ov_id = err;
167 +
168 +out_err:
169 +       return err;
170 +}
171 +
172 +static inline struct cfs_overlay_item *to_cfs_overlay_item(
173 +               struct config_item *item)
174 +{
175 +       return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
176 +}
177 +
178 +static ssize_t cfs_overlay_item_path_show(struct config_item *item,
179 +               char *page)
180 +{
181 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
182 +       return sprintf(page, "%s\n", overlay->path);
183 +}
184 +
185 +static ssize_t cfs_overlay_item_path_store(struct config_item *item,
186 +               const char *page, size_t count)
187 +{
188 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
189 +       const char *p = page;
190 +       char *s;
191 +       int err;
192 +
193 +       /* if it's set do not allow changes */
194 +       if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
195 +               return -EPERM;
196 +
197 +       /* copy to path buffer (and make sure it's always zero terminated */
198 +       count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p);
199 +       overlay->path[sizeof(overlay->path) - 1] = '\0';
200 +
201 +       /* strip trailing newlines */
202 +       s = overlay->path + strlen(overlay->path);
203 +       while (s > overlay->path && *--s == '\n')
204 +               *s = '\0';
205 +
206 +       pr_debug("%s: path is '%s'\n", __func__, overlay->path);
207 +
208 +       err = request_firmware(&overlay->fw, overlay->path, NULL);
209 +       if (err != 0)
210 +               goto out_err;
211 +
212 +       err = create_overlay(overlay, (void *)overlay->fw->data);
213 +       if (err != 0)
214 +               goto out_err;
215 +
216 +       return count;
217 +
218 +out_err:
219 +
220 +       release_firmware(overlay->fw);
221 +       overlay->fw = NULL;
222 +
223 +       overlay->path[0] = '\0';
224 +       return err;
225 +}
226 +
227 +static ssize_t cfs_overlay_item_status_show(struct config_item *item,
228 +               char *page)
229 +{
230 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
231 +
232 +       return sprintf(page, "%s\n",
233 +                       overlay->ov_id >= 0 ? "applied" : "unapplied");
234 +}
235 +
236 +CONFIGFS_ATTR(cfs_overlay_item_, path);
237 +CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
238 +
239 +static struct configfs_attribute *cfs_overlay_attrs[] = {
240 +       &cfs_overlay_item_attr_path,
241 +       &cfs_overlay_item_attr_status,
242 +       NULL,
243 +};
244 +
245 +ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
246 +               void *buf, size_t max_count)
247 +{
248 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
249 +
250 +       pr_debug("%s: buf=%p max_count=%u\n", __func__,
251 +                       buf, max_count);
252 +
253 +       if (overlay->dtbo == NULL)
254 +               return 0;
255 +
256 +       /* copy if buffer provided */
257 +       if (buf != NULL) {
258 +               /* the buffer must be large enough */
259 +               if (overlay->dtbo_size > max_count)
260 +                       return -ENOSPC;
261 +
262 +               memcpy(buf, overlay->dtbo, overlay->dtbo_size);
263 +       }
264 +
265 +       return overlay->dtbo_size;
266 +}
267 +
268 +ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
269 +               const void *buf, size_t count)
270 +{
271 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
272 +       int err;
273 +
274 +       /* if it's set do not allow changes */
275 +       if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
276 +               return -EPERM;
277 +
278 +       /* copy the contents */
279 +       overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
280 +       if (overlay->dtbo == NULL)
281 +               return -ENOMEM;
282 +
283 +       overlay->dtbo_size = count;
284 +
285 +       err = create_overlay(overlay, overlay->dtbo);
286 +       if (err != 0)
287 +               goto out_err;
288 +
289 +       return count;
290 +
291 +out_err:
292 +       kfree(overlay->dtbo);
293 +       overlay->dtbo = NULL;
294 +       overlay->dtbo_size = 0;
295 +
296 +       return err;
297 +}
298 +
299 +CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
300 +
301 +static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
302 +       &cfs_overlay_item_attr_dtbo,
303 +       NULL,
304 +};
305 +
306 +static void cfs_overlay_release(struct config_item *item)
307 +{
308 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
309 +
310 +       if (overlay->ov_id >= 0)
311 +               of_overlay_destroy(overlay->ov_id);
312 +       if (overlay->fw)
313 +               release_firmware(overlay->fw);
314 +       /* kfree with NULL is safe */
315 +       kfree(overlay->dtbo);
316 +       kfree(overlay);
317 +}
318 +
319 +static struct configfs_item_operations cfs_overlay_item_ops = {
320 +       .release        = cfs_overlay_release,
321 +};
322 +
323 +static struct config_item_type cfs_overlay_type = {
324 +       .ct_item_ops    = &cfs_overlay_item_ops,
325 +       .ct_attrs       = cfs_overlay_attrs,
326 +       .ct_bin_attrs   = cfs_overlay_bin_attrs,
327 +       .ct_owner       = THIS_MODULE,
328 +};
329 +
330 +static struct config_item *cfs_overlay_group_make_item(
331 +               struct config_group *group, const char *name)
332 +{
333 +       struct cfs_overlay_item *overlay;
334 +
335 +       overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
336 +       if (!overlay)
337 +               return ERR_PTR(-ENOMEM);
338 +       overlay->ov_id = -1;
339 +
340 +       config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
341 +       return &overlay->item;
342 +}
343 +
344 +static void cfs_overlay_group_drop_item(struct config_group *group,
345 +               struct config_item *item)
346 +{
347 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
348 +
349 +       config_item_put(&overlay->item);
350 +}
351 +
352 +static struct configfs_group_operations overlays_ops = {
353 +       .make_item      = cfs_overlay_group_make_item,
354 +       .drop_item      = cfs_overlay_group_drop_item,
355 +};
356 +
357 +static struct config_item_type overlays_type = {
358 +       .ct_group_ops   = &overlays_ops,
359 +       .ct_owner       = THIS_MODULE,
360 +};
361 +
362 +static struct configfs_group_operations of_cfs_ops = {
363 +       /* empty - we don't allow anything to be created */
364 +};
365 +
366 +static struct config_item_type of_cfs_type = {
367 +       .ct_group_ops   = &of_cfs_ops,
368 +       .ct_owner       = THIS_MODULE,
369 +};
370 +
371 +struct config_group of_cfs_overlay_group;
372 +
373 +struct config_group *of_cfs_def_groups[] = {
374 +       &of_cfs_overlay_group,
375 +       NULL
376 +};
377 +
378 +static struct configfs_subsystem of_cfs_subsys = {
379 +       .su_group = {
380 +               .cg_item = {
381 +                       .ci_namebuf = "device-tree",
382 +                       .ci_type = &of_cfs_type,
383 +               },
384 +               .default_groups = of_cfs_def_groups,
385 +       },
386 +       .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
387 +};
388 +
389 +static int __init of_cfs_init(void)
390 +{
391 +       int ret;
392 +
393 +       pr_info("%s\n", __func__);
394 +
395 +       config_group_init(&of_cfs_subsys.su_group);
396 +       config_group_init_type_name(&of_cfs_overlay_group, "overlays",
397 +                       &overlays_type);
398 +
399 +       ret = configfs_register_subsystem(&of_cfs_subsys);
400 +       if (ret != 0) {
401 +               pr_err("%s: failed to register subsys\n", __func__);
402 +               goto out;
403 +       }
404 +       pr_info("%s: OK\n", __func__);
405 +out:
406 +       return ret;
407 +}
408 +late_initcall(of_cfs_init);