1 From dc0f4681729e9bfe506ab8b15852363cb59bdaf5 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] OF: DT-Overlay configfs interface
6 This is a port of Pantelis Antoniou's v3 port that makes use of the
7 new upstreamed configfs support for binary attributes.
9 Original commit message:
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.
15 Please see Documentation/devicetree/configfs-overlays.txt for more info.
18 - Removed ifdef CONFIG_OF_OVERLAY (since for now it's required)
19 - Created a documentation entry
20 - Slight rewording in Kconfig
23 - of_resolve() -> of_resolve_phandles().
25 Originally-signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
26 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
28 DT configfs: Fix build errors on other platforms
30 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
32 DT configfs: fix build error
34 There is an error when compiling rpi-4.6.y branch:
35 CC drivers/of/configfs.o
36 drivers/of/configfs.c:291:21: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
37 .default_groups = of_cfs_def_groups,
39 drivers/of/configfs.c:291:21: note: (near initialization for 'of_cfs_subsys.su_group.default_groups.next')
41 The .default_groups is linked list since commit
42 1ae1602de028acaa42a0f6ff18d19756f8e825c6.
43 This commit uses configfs_add_default_group to fix this problem.
45 Signed-off-by: Slawomir Stepien <sst@poczta.fm>
47 configfs: New of_overlay API
49 .../devicetree/configfs-overlays.txt | 31 ++
50 drivers/of/Kconfig | 7 +
51 drivers/of/Makefile | 1 +
52 drivers/of/configfs.c | 310 ++++++++++++++++++
53 4 files changed, 349 insertions(+)
54 create mode 100644 Documentation/devicetree/configfs-overlays.txt
55 create mode 100644 drivers/of/configfs.c
58 +++ b/Documentation/devicetree/configfs-overlays.txt
60 +Howto use the configfs overlay interface.
62 +A device-tree configfs entry is created in /config/device-tree/overlays
63 +and and it is manipulated using standard file system I/O.
64 +Note that this is a debug level interface, for use by developers and
65 +not necessarily something accessed by normal users due to the
66 +security implications of having direct access to the kernel's device tree.
68 +* To create an overlay you mkdir the directory:
70 + # mkdir /config/device-tree/overlays/foo
72 +* Either you echo the overlay firmware file to the path property file.
74 + # echo foo.dtbo >/config/device-tree/overlays/foo/path
76 +* Or you cat the contents of the overlay to the dtbo file
78 + # cat foo.dtbo >/config/device-tree/overlays/foo/dtbo
80 +The overlay file will be applied, and devices will be created/destroyed
83 +To remove it simply rmdir the directory.
85 + # rmdir /config/device-tree/overlays/foo
87 +The rationalle of the dual interface (firmware & direct copy) is that each is
88 +better suited to different use patterns. The firmware interface is what's
89 +intended to be used by hardware managers in the kernel, while the copy interface
90 +make sense for developers (since it avoids problems with namespaces).
91 --- a/drivers/of/Kconfig
92 +++ b/drivers/of/Kconfig
93 @@ -108,4 +108,11 @@ config OF_DMA_DEFAULT_COHERENT
94 # arches should select this if DMA is coherent by default for OF devices
98 + bool "Device Tree Overlay ConfigFS interface"
102 + Enable a simple user-space driven DT overlay interface.
105 --- a/drivers/of/Makefile
106 +++ b/drivers/of/Makefile
108 # SPDX-License-Identifier: GPL-2.0
109 obj-y = base.o device.o platform.o property.o
110 obj-$(CONFIG_OF_KOBJ) += kobj.o
111 +obj-$(CONFIG_OF_CONFIGFS) += configfs.o
112 obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
113 obj-$(CONFIG_OF_FLATTREE) += fdt.o
114 obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o
116 +++ b/drivers/of/configfs.c
119 + * Configfs entries for device-tree
121 + * Copyright (C) 2013 - Pantelis Antoniou <panto@antoniou-consulting.com>
123 + * This program is free software; you can redistribute it and/or
124 + * modify it under the terms of the GNU General Public License
125 + * as published by the Free Software Foundation; either version
126 + * 2 of the License, or (at your option) any later version.
128 +#include <linux/ctype.h>
129 +#include <linux/cpu.h>
130 +#include <linux/module.h>
131 +#include <linux/of.h>
132 +#include <linux/of_fdt.h>
133 +#include <linux/spinlock.h>
134 +#include <linux/slab.h>
135 +#include <linux/proc_fs.h>
136 +#include <linux/configfs.h>
137 +#include <linux/types.h>
138 +#include <linux/stat.h>
139 +#include <linux/limits.h>
140 +#include <linux/file.h>
141 +#include <linux/vmalloc.h>
142 +#include <linux/firmware.h>
143 +#include <linux/sizes.h>
145 +#include "of_private.h"
147 +struct cfs_overlay_item {
148 + struct config_item item;
150 + char path[PATH_MAX];
152 + const struct firmware *fw;
153 + struct device_node *overlay;
160 +static int create_overlay(struct cfs_overlay_item *overlay, void *blob)
164 + /* unflatten the tree */
165 + of_fdt_unflatten_tree(blob, NULL, &overlay->overlay);
166 + if (overlay->overlay == NULL) {
167 + pr_err("%s: failed to unflatten tree\n", __func__);
171 + pr_debug("%s: unflattened OK\n", __func__);
173 + /* mark it as detached */
174 + of_node_set_flag(overlay->overlay, OF_DETACHED);
176 + /* perform resolution */
177 + err = of_resolve_phandles(overlay->overlay);
179 + pr_err("%s: Failed to resolve tree\n", __func__);
182 + pr_debug("%s: resolved OK\n", __func__);
184 + err = of_overlay_apply(overlay->overlay, &overlay->ov_id);
186 + pr_err("%s: Failed to create overlay (err=%d)\n",
195 +static inline struct cfs_overlay_item *to_cfs_overlay_item(
196 + struct config_item *item)
198 + return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
201 +static ssize_t cfs_overlay_item_path_show(struct config_item *item,
204 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
205 + return sprintf(page, "%s\n", overlay->path);
208 +static ssize_t cfs_overlay_item_path_store(struct config_item *item,
209 + const char *page, size_t count)
211 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
212 + const char *p = page;
216 + /* if it's set do not allow changes */
217 + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
220 + /* copy to path buffer (and make sure it's always zero terminated */
221 + count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p);
222 + overlay->path[sizeof(overlay->path) - 1] = '\0';
224 + /* strip trailing newlines */
225 + s = overlay->path + strlen(overlay->path);
226 + while (s > overlay->path && *--s == '\n')
229 + pr_debug("%s: path is '%s'\n", __func__, overlay->path);
231 + err = request_firmware(&overlay->fw, overlay->path, NULL);
235 + err = create_overlay(overlay, (void *)overlay->fw->data);
243 + release_firmware(overlay->fw);
244 + overlay->fw = NULL;
246 + overlay->path[0] = '\0';
250 +static ssize_t cfs_overlay_item_status_show(struct config_item *item,
253 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
255 + return sprintf(page, "%s\n",
256 + overlay->ov_id >= 0 ? "applied" : "unapplied");
259 +CONFIGFS_ATTR(cfs_overlay_item_, path);
260 +CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
262 +static struct configfs_attribute *cfs_overlay_attrs[] = {
263 + &cfs_overlay_item_attr_path,
264 + &cfs_overlay_item_attr_status,
268 +ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
269 + void *buf, size_t max_count)
271 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
273 + pr_debug("%s: buf=%p max_count=%zu\n", __func__,
276 + if (overlay->dtbo == NULL)
279 + /* copy if buffer provided */
281 + /* the buffer must be large enough */
282 + if (overlay->dtbo_size > max_count)
285 + memcpy(buf, overlay->dtbo, overlay->dtbo_size);
288 + return overlay->dtbo_size;
291 +ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
292 + const void *buf, size_t count)
294 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
297 + /* if it's set do not allow changes */
298 + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
301 + /* copy the contents */
302 + overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
303 + if (overlay->dtbo == NULL)
306 + overlay->dtbo_size = count;
308 + err = create_overlay(overlay, overlay->dtbo);
315 + kfree(overlay->dtbo);
316 + overlay->dtbo = NULL;
317 + overlay->dtbo_size = 0;
322 +CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
324 +static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
325 + &cfs_overlay_item_attr_dtbo,
329 +static void cfs_overlay_release(struct config_item *item)
331 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
333 + if (overlay->ov_id >= 0)
334 + of_overlay_remove(&overlay->ov_id);
336 + release_firmware(overlay->fw);
337 + /* kfree with NULL is safe */
338 + kfree(overlay->dtbo);
342 +static struct configfs_item_operations cfs_overlay_item_ops = {
343 + .release = cfs_overlay_release,
346 +static struct config_item_type cfs_overlay_type = {
347 + .ct_item_ops = &cfs_overlay_item_ops,
348 + .ct_attrs = cfs_overlay_attrs,
349 + .ct_bin_attrs = cfs_overlay_bin_attrs,
350 + .ct_owner = THIS_MODULE,
353 +static struct config_item *cfs_overlay_group_make_item(
354 + struct config_group *group, const char *name)
356 + struct cfs_overlay_item *overlay;
358 + overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
360 + return ERR_PTR(-ENOMEM);
361 + overlay->ov_id = -1;
363 + config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
364 + return &overlay->item;
367 +static void cfs_overlay_group_drop_item(struct config_group *group,
368 + struct config_item *item)
370 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
372 + config_item_put(&overlay->item);
375 +static struct configfs_group_operations overlays_ops = {
376 + .make_item = cfs_overlay_group_make_item,
377 + .drop_item = cfs_overlay_group_drop_item,
380 +static struct config_item_type overlays_type = {
381 + .ct_group_ops = &overlays_ops,
382 + .ct_owner = THIS_MODULE,
385 +static struct configfs_group_operations of_cfs_ops = {
386 + /* empty - we don't allow anything to be created */
389 +static struct config_item_type of_cfs_type = {
390 + .ct_group_ops = &of_cfs_ops,
391 + .ct_owner = THIS_MODULE,
394 +struct config_group of_cfs_overlay_group;
396 +static struct configfs_subsystem of_cfs_subsys = {
399 + .ci_namebuf = "device-tree",
400 + .ci_type = &of_cfs_type,
403 + .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
406 +static int __init of_cfs_init(void)
410 + pr_info("%s\n", __func__);
412 + config_group_init(&of_cfs_subsys.su_group);
413 + config_group_init_type_name(&of_cfs_overlay_group, "overlays",
415 + configfs_add_default_group(&of_cfs_overlay_group,
416 + &of_cfs_subsys.su_group);
418 + ret = configfs_register_subsystem(&of_cfs_subsys);
420 + pr_err("%s: failed to register subsys\n", __func__);
423 + pr_info("%s: OK\n", __func__);
427 +late_initcall(of_cfs_init);