1 From b12afa082622333816203f0fd2e93051de65de09 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 094/454] 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 .../devicetree/configfs-overlays.txt | 31 ++
48 drivers/of/Kconfig | 7 +
49 drivers/of/Makefile | 1 +
50 drivers/of/configfs.c | 311 ++++++++++++++++++
51 4 files changed, 350 insertions(+)
52 create mode 100644 Documentation/devicetree/configfs-overlays.txt
53 create mode 100644 drivers/of/configfs.c
56 +++ b/Documentation/devicetree/configfs-overlays.txt
58 +Howto use the configfs overlay interface.
60 +A device-tree configfs entry is created in /config/device-tree/overlays
61 +and and it is manipulated using standard file system I/O.
62 +Note that this is a debug level interface, for use by developers and
63 +not necessarily something accessed by normal users due to the
64 +security implications of having direct access to the kernel's device tree.
66 +* To create an overlay you mkdir the directory:
68 + # mkdir /config/device-tree/overlays/foo
70 +* Either you echo the overlay firmware file to the path property file.
72 + # echo foo.dtbo >/config/device-tree/overlays/foo/path
74 +* Or you cat the contents of the overlay to the dtbo file
76 + # cat foo.dtbo >/config/device-tree/overlays/foo/dtbo
78 +The overlay file will be applied, and devices will be created/destroyed
81 +To remove it simply rmdir the directory.
83 + # rmdir /config/device-tree/overlays/foo
85 +The rationalle of the dual interface (firmware & direct copy) is that each is
86 +better suited to different use patterns. The firmware interface is what's
87 +intended to be used by hardware managers in the kernel, while the copy interface
88 +make sense for developers (since it avoids problems with namespaces).
89 --- a/drivers/of/Kconfig
90 +++ b/drivers/of/Kconfig
91 @@ -112,4 +112,11 @@ config OF_OVERLAY
96 + bool "Device Tree Overlay ConfigFS interface"
100 + Enable a simple user-space driven DT overlay interface.
103 --- a/drivers/of/Makefile
104 +++ b/drivers/of/Makefile
106 # SPDX-License-Identifier: GPL-2.0
107 obj-y = base.o device.o platform.o property.o
108 +obj-$(CONFIG_OF_CONFIGFS) += configfs.o
109 obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
110 obj-$(CONFIG_OF_FLATTREE) += fdt.o
111 obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o
113 +++ b/drivers/of/configfs.c
116 + * Configfs entries for device-tree
118 + * Copyright (C) 2013 - Pantelis Antoniou <panto@antoniou-consulting.com>
120 + * This program is free software; you can redistribute it and/or
121 + * modify it under the terms of the GNU General Public License
122 + * as published by the Free Software Foundation; either version
123 + * 2 of the License, or (at your option) any later version.
125 +#include <linux/ctype.h>
126 +#include <linux/cpu.h>
127 +#include <linux/module.h>
128 +#include <linux/of.h>
129 +#include <linux/of_fdt.h>
130 +#include <linux/spinlock.h>
131 +#include <linux/slab.h>
132 +#include <linux/proc_fs.h>
133 +#include <linux/configfs.h>
134 +#include <linux/types.h>
135 +#include <linux/stat.h>
136 +#include <linux/limits.h>
137 +#include <linux/file.h>
138 +#include <linux/vmalloc.h>
139 +#include <linux/firmware.h>
140 +#include <linux/sizes.h>
142 +#include "of_private.h"
144 +struct cfs_overlay_item {
145 + struct config_item item;
147 + char path[PATH_MAX];
149 + const struct firmware *fw;
150 + struct device_node *overlay;
157 +static int create_overlay(struct cfs_overlay_item *overlay, void *blob)
161 + /* unflatten the tree */
162 + of_fdt_unflatten_tree(blob, NULL, &overlay->overlay);
163 + if (overlay->overlay == NULL) {
164 + pr_err("%s: failed to unflatten tree\n", __func__);
168 + pr_debug("%s: unflattened OK\n", __func__);
170 + /* mark it as detached */
171 + of_node_set_flag(overlay->overlay, OF_DETACHED);
173 + /* perform resolution */
174 + err = of_resolve_phandles(overlay->overlay);
176 + pr_err("%s: Failed to resolve tree\n", __func__);
179 + pr_debug("%s: resolved OK\n", __func__);
181 + err = of_overlay_create(overlay->overlay);
183 + pr_err("%s: Failed to create overlay (err=%d)\n",
187 + overlay->ov_id = err;
193 +static inline struct cfs_overlay_item *to_cfs_overlay_item(
194 + struct config_item *item)
196 + return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
199 +static ssize_t cfs_overlay_item_path_show(struct config_item *item,
202 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
203 + return sprintf(page, "%s\n", overlay->path);
206 +static ssize_t cfs_overlay_item_path_store(struct config_item *item,
207 + const char *page, size_t count)
209 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
210 + const char *p = page;
214 + /* if it's set do not allow changes */
215 + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
218 + /* copy to path buffer (and make sure it's always zero terminated */
219 + count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p);
220 + overlay->path[sizeof(overlay->path) - 1] = '\0';
222 + /* strip trailing newlines */
223 + s = overlay->path + strlen(overlay->path);
224 + while (s > overlay->path && *--s == '\n')
227 + pr_debug("%s: path is '%s'\n", __func__, overlay->path);
229 + err = request_firmware(&overlay->fw, overlay->path, NULL);
233 + err = create_overlay(overlay, (void *)overlay->fw->data);
241 + release_firmware(overlay->fw);
242 + overlay->fw = NULL;
244 + overlay->path[0] = '\0';
248 +static ssize_t cfs_overlay_item_status_show(struct config_item *item,
251 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
253 + return sprintf(page, "%s\n",
254 + overlay->ov_id >= 0 ? "applied" : "unapplied");
257 +CONFIGFS_ATTR(cfs_overlay_item_, path);
258 +CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
260 +static struct configfs_attribute *cfs_overlay_attrs[] = {
261 + &cfs_overlay_item_attr_path,
262 + &cfs_overlay_item_attr_status,
266 +ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
267 + void *buf, size_t max_count)
269 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
271 + pr_debug("%s: buf=%p max_count=%zu\n", __func__,
274 + if (overlay->dtbo == NULL)
277 + /* copy if buffer provided */
279 + /* the buffer must be large enough */
280 + if (overlay->dtbo_size > max_count)
283 + memcpy(buf, overlay->dtbo, overlay->dtbo_size);
286 + return overlay->dtbo_size;
289 +ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
290 + const void *buf, size_t count)
292 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
295 + /* if it's set do not allow changes */
296 + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
299 + /* copy the contents */
300 + overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
301 + if (overlay->dtbo == NULL)
304 + overlay->dtbo_size = count;
306 + err = create_overlay(overlay, overlay->dtbo);
313 + kfree(overlay->dtbo);
314 + overlay->dtbo = NULL;
315 + overlay->dtbo_size = 0;
320 +CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
322 +static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
323 + &cfs_overlay_item_attr_dtbo,
327 +static void cfs_overlay_release(struct config_item *item)
329 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
331 + if (overlay->ov_id >= 0)
332 + of_overlay_destroy(overlay->ov_id);
334 + release_firmware(overlay->fw);
335 + /* kfree with NULL is safe */
336 + kfree(overlay->dtbo);
340 +static struct configfs_item_operations cfs_overlay_item_ops = {
341 + .release = cfs_overlay_release,
344 +static struct config_item_type cfs_overlay_type = {
345 + .ct_item_ops = &cfs_overlay_item_ops,
346 + .ct_attrs = cfs_overlay_attrs,
347 + .ct_bin_attrs = cfs_overlay_bin_attrs,
348 + .ct_owner = THIS_MODULE,
351 +static struct config_item *cfs_overlay_group_make_item(
352 + struct config_group *group, const char *name)
354 + struct cfs_overlay_item *overlay;
356 + overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
358 + return ERR_PTR(-ENOMEM);
359 + overlay->ov_id = -1;
361 + config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
362 + return &overlay->item;
365 +static void cfs_overlay_group_drop_item(struct config_group *group,
366 + struct config_item *item)
368 + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
370 + config_item_put(&overlay->item);
373 +static struct configfs_group_operations overlays_ops = {
374 + .make_item = cfs_overlay_group_make_item,
375 + .drop_item = cfs_overlay_group_drop_item,
378 +static struct config_item_type overlays_type = {
379 + .ct_group_ops = &overlays_ops,
380 + .ct_owner = THIS_MODULE,
383 +static struct configfs_group_operations of_cfs_ops = {
384 + /* empty - we don't allow anything to be created */
387 +static struct config_item_type of_cfs_type = {
388 + .ct_group_ops = &of_cfs_ops,
389 + .ct_owner = THIS_MODULE,
392 +struct config_group of_cfs_overlay_group;
394 +static struct configfs_subsystem of_cfs_subsys = {
397 + .ci_namebuf = "device-tree",
398 + .ci_type = &of_cfs_type,
401 + .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
404 +static int __init of_cfs_init(void)
408 + pr_info("%s\n", __func__);
410 + config_group_init(&of_cfs_subsys.su_group);
411 + config_group_init_type_name(&of_cfs_overlay_group, "overlays",
413 + configfs_add_default_group(&of_cfs_overlay_group,
414 + &of_cfs_subsys.su_group);
416 + ret = configfs_register_subsystem(&of_cfs_subsys);
418 + pr_err("%s: failed to register subsys\n", __func__);
421 + pr_info("%s: OK\n", __func__);
425 +late_initcall(of_cfs_init);