1 From 9745f13ee715e7f47ac11a31d0f75b9661b63344 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 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
37 +++ b/Documentation/devicetree/configfs-overlays.txt
39 +Howto use the configfs overlay interface.
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.
47 +* To create an overlay you mkdir the directory:
49 + # mkdir /config/device-tree/overlays/foo
51 +* Either you echo the overlay firmware file to the path property file.
53 + # echo foo.dtbo >/config/device-tree/overlays/foo/path
55 +* Or you cat the contents of the overlay to the dtbo file
57 + # cat foo.dtbo >/config/device-tree/overlays/foo/dtbo
59 +The overlay file will be applied, and devices will be created/destroyed
62 +To remove it simply rmdir the directory.
64 + # rmdir /config/device-tree/overlays/foo
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.
77 + bool "Device Tree Overlay ConfigFS interface"
81 + Enable a simple user-space driven DT overlay interface.
84 --- a/drivers/of/Makefile
85 +++ b/drivers/of/Makefile
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
93 +++ b/drivers/of/configfs.c
96 + * Configfs entries for device-tree
98 + * Copyright (C) 2013 - Pantelis Antoniou <panto@antoniou-consulting.com>
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.
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>
121 +#include "of_private.h"
123 +struct cfs_overlay_item {
124 + struct config_item item;
126 + char path[PATH_MAX];
128 + const struct firmware *fw;
129 + struct device_node *overlay;
136 +static int create_overlay(struct cfs_overlay_item *overlay, void *blob)
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__);
147 + pr_debug("%s: unflattened OK\n", __func__);
149 + /* mark it as detached */
150 + of_node_set_flag(overlay->overlay, OF_DETACHED);
152 + /* perform resolution */
153 + err = of_resolve_phandles(overlay->overlay);
155 + pr_err("%s: Failed to resolve tree\n", __func__);
158 + pr_debug("%s: resolved OK\n", __func__);
160 + err = of_overlay_create(overlay->overlay);
162 + pr_err("%s: Failed to create overlay (err=%d)\n",
166 + overlay->ov_id = err;
172 +static inline struct cfs_overlay_item *to_cfs_overlay_item(
173 + struct config_item *item)
175 + return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
178 +static ssize_t cfs_overlay_item_path_show(struct config_item *item,
181 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
182 + return sprintf(page, "%s\n", overlay->path);
185 +static ssize_t cfs_overlay_item_path_store(struct config_item *item,
186 + const char *page, size_t count)
188 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
189 + const char *p = page;
193 + /* if it's set do not allow changes */
194 + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
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';
201 + /* strip trailing newlines */
202 + s = overlay->path + strlen(overlay->path);
203 + while (s > overlay->path && *--s == '\n')
206 + pr_debug("%s: path is '%s'\n", __func__, overlay->path);
208 + err = request_firmware(&overlay->fw, overlay->path, NULL);
212 + err = create_overlay(overlay, (void *)overlay->fw->data);
220 + release_firmware(overlay->fw);
221 + overlay->fw = NULL;
223 + overlay->path[0] = '\0';
227 +static ssize_t cfs_overlay_item_status_show(struct config_item *item,
230 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
232 + return sprintf(page, "%s\n",
233 + overlay->ov_id >= 0 ? "applied" : "unapplied");
236 +CONFIGFS_ATTR(cfs_overlay_item_, path);
237 +CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
239 +static struct configfs_attribute *cfs_overlay_attrs[] = {
240 + &cfs_overlay_item_attr_path,
241 + &cfs_overlay_item_attr_status,
245 +ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
246 + void *buf, size_t max_count)
248 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
250 + pr_debug("%s: buf=%p max_count=%u\n", __func__,
253 + if (overlay->dtbo == NULL)
256 + /* copy if buffer provided */
258 + /* the buffer must be large enough */
259 + if (overlay->dtbo_size > max_count)
262 + memcpy(buf, overlay->dtbo, overlay->dtbo_size);
265 + return overlay->dtbo_size;
268 +ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
269 + const void *buf, size_t count)
271 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
274 + /* if it's set do not allow changes */
275 + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
278 + /* copy the contents */
279 + overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
280 + if (overlay->dtbo == NULL)
283 + overlay->dtbo_size = count;
285 + err = create_overlay(overlay, overlay->dtbo);
292 + kfree(overlay->dtbo);
293 + overlay->dtbo = NULL;
294 + overlay->dtbo_size = 0;
299 +CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
301 +static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
302 + &cfs_overlay_item_attr_dtbo,
306 +static void cfs_overlay_release(struct config_item *item)
308 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
310 + if (overlay->ov_id >= 0)
311 + of_overlay_destroy(overlay->ov_id);
313 + release_firmware(overlay->fw);
314 + /* kfree with NULL is safe */
315 + kfree(overlay->dtbo);
319 +static struct configfs_item_operations cfs_overlay_item_ops = {
320 + .release = cfs_overlay_release,
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,
330 +static struct config_item *cfs_overlay_group_make_item(
331 + struct config_group *group, const char *name)
333 + struct cfs_overlay_item *overlay;
335 + overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
337 + return ERR_PTR(-ENOMEM);
338 + overlay->ov_id = -1;
340 + config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
341 + return &overlay->item;
344 +static void cfs_overlay_group_drop_item(struct config_group *group,
345 + struct config_item *item)
347 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
349 + config_item_put(&overlay->item);
352 +static struct configfs_group_operations overlays_ops = {
353 + .make_item = cfs_overlay_group_make_item,
354 + .drop_item = cfs_overlay_group_drop_item,
357 +static struct config_item_type overlays_type = {
358 + .ct_group_ops = &overlays_ops,
359 + .ct_owner = THIS_MODULE,
362 +static struct configfs_group_operations of_cfs_ops = {
363 + /* empty - we don't allow anything to be created */
366 +static struct config_item_type of_cfs_type = {
367 + .ct_group_ops = &of_cfs_ops,
368 + .ct_owner = THIS_MODULE,
371 +struct config_group of_cfs_overlay_group;
373 +struct config_group *of_cfs_def_groups[] = {
374 + &of_cfs_overlay_group,
378 +static struct configfs_subsystem of_cfs_subsys = {
381 + .ci_namebuf = "device-tree",
382 + .ci_type = &of_cfs_type,
384 + .default_groups = of_cfs_def_groups,
386 + .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
389 +static int __init of_cfs_init(void)
393 + pr_info("%s\n", __func__);
395 + config_group_init(&of_cfs_subsys.su_group);
396 + config_group_init_type_name(&of_cfs_overlay_group, "overlays",
399 + ret = configfs_register_subsystem(&of_cfs_subsys);
401 + pr_err("%s: failed to register subsys\n", __func__);
404 + pr_info("%s: OK\n", __func__);
408 +late_initcall(of_cfs_init);