lantiq: fix broadcasts and vlans in two iface mode
[oweals/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 0090-OF-DT-Overlay-configfs-interface.patch
1 From ef4a8376af32bb6722e1ea933a9e1a83ebc8f175 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
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 DT configfs: Fix build errors on other platforms
29
30 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
31
32 DT configfs: fix build error
33
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,
38                      ^
39 drivers/of/configfs.c:291:21: note: (near initialization for 'of_cfs_subsys.su_group.default_groups.next')
40
41 The .default_groups is linked list since commit
42 1ae1602de028acaa42a0f6ff18d19756f8e825c6.
43 This commit uses configfs_add_default_group to fix this problem.
44
45 Signed-off-by: Slawomir Stepien <sst@poczta.fm>
46 ---
47  Documentation/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
54
55 --- /dev/null
56 +++ b/Documentation/devicetree/configfs-overlays.txt
57 @@ -0,0 +1,31 @@
58 +Howto use the configfs overlay interface.
59 +
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.
65 +
66 +* To create an overlay you mkdir the directory:
67 +
68 +       # mkdir /config/device-tree/overlays/foo
69 +
70 +* Either you echo the overlay firmware file to the path property file.
71 +
72 +       # echo foo.dtbo >/config/device-tree/overlays/foo/path
73 +
74 +* Or you cat the contents of the overlay to the dtbo file
75 +
76 +       # cat foo.dtbo >/config/device-tree/overlays/foo/dtbo
77 +
78 +The overlay file will be applied, and devices will be created/destroyed
79 +as required.
80 +
81 +To remove it simply rmdir the directory.
82 +
83 +       # rmdir /config/device-tree/overlays/foo
84 +
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
92  config OF_NUMA
93         bool
94  
95 +config OF_CONFIGFS
96 +       bool "Device Tree Overlay ConfigFS interface"
97 +       select CONFIGFS_FS
98 +       select OF_OVERLAY
99 +       help
100 +         Enable a simple user-space driven DT overlay interface.
101 +
102  endif # OF
103 --- a/drivers/of/Makefile
104 +++ b/drivers/of/Makefile
105 @@ -1,4 +1,5 @@
106  obj-y = base.o device.o platform.o
107 +obj-$(CONFIG_OF_CONFIGFS) += configfs.o
108  obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
109  obj-$(CONFIG_OF_FLATTREE) += fdt.o
110  obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o
111 --- /dev/null
112 +++ b/drivers/of/configfs.c
113 @@ -0,0 +1,311 @@
114 +/*
115 + * Configfs entries for device-tree
116 + *
117 + * Copyright (C) 2013 - Pantelis Antoniou <panto@antoniou-consulting.com>
118 + *
119 + * This program is free software; you can redistribute it and/or
120 + * modify it under the terms of the GNU General Public License
121 + * as published by the Free Software Foundation; either version
122 + * 2 of the License, or (at your option) any later version.
123 + */
124 +#include <linux/ctype.h>
125 +#include <linux/cpu.h>
126 +#include <linux/module.h>
127 +#include <linux/of.h>
128 +#include <linux/of_fdt.h>
129 +#include <linux/spinlock.h>
130 +#include <linux/slab.h>
131 +#include <linux/proc_fs.h>
132 +#include <linux/configfs.h>
133 +#include <linux/types.h>
134 +#include <linux/stat.h>
135 +#include <linux/limits.h>
136 +#include <linux/file.h>
137 +#include <linux/vmalloc.h>
138 +#include <linux/firmware.h>
139 +#include <linux/sizes.h>
140 +
141 +#include "of_private.h"
142 +
143 +struct cfs_overlay_item {
144 +       struct config_item      item;
145 +
146 +       char                    path[PATH_MAX];
147 +
148 +       const struct firmware   *fw;
149 +       struct device_node      *overlay;
150 +       int                     ov_id;
151 +
152 +       void                    *dtbo;
153 +       int                     dtbo_size;
154 +};
155 +
156 +static int create_overlay(struct cfs_overlay_item *overlay, void *blob)
157 +{
158 +       int err;
159 +
160 +       /* unflatten the tree */
161 +       of_fdt_unflatten_tree(blob, NULL, &overlay->overlay);
162 +       if (overlay->overlay == NULL) {
163 +               pr_err("%s: failed to unflatten tree\n", __func__);
164 +               err = -EINVAL;
165 +               goto out_err;
166 +       }
167 +       pr_debug("%s: unflattened OK\n", __func__);
168 +
169 +       /* mark it as detached */
170 +       of_node_set_flag(overlay->overlay, OF_DETACHED);
171 +
172 +       /* perform resolution */
173 +       err = of_resolve_phandles(overlay->overlay);
174 +       if (err != 0) {
175 +               pr_err("%s: Failed to resolve tree\n", __func__);
176 +               goto out_err;
177 +       }
178 +       pr_debug("%s: resolved OK\n", __func__);
179 +
180 +       err = of_overlay_create(overlay->overlay);
181 +       if (err < 0) {
182 +               pr_err("%s: Failed to create overlay (err=%d)\n",
183 +                               __func__, err);
184 +               goto out_err;
185 +       }
186 +       overlay->ov_id = err;
187 +
188 +out_err:
189 +       return err;
190 +}
191 +
192 +static inline struct cfs_overlay_item *to_cfs_overlay_item(
193 +               struct config_item *item)
194 +{
195 +       return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
196 +}
197 +
198 +static ssize_t cfs_overlay_item_path_show(struct config_item *item,
199 +               char *page)
200 +{
201 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
202 +       return sprintf(page, "%s\n", overlay->path);
203 +}
204 +
205 +static ssize_t cfs_overlay_item_path_store(struct config_item *item,
206 +               const char *page, size_t count)
207 +{
208 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
209 +       const char *p = page;
210 +       char *s;
211 +       int err;
212 +
213 +       /* if it's set do not allow changes */
214 +       if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
215 +               return -EPERM;
216 +
217 +       /* copy to path buffer (and make sure it's always zero terminated */
218 +       count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p);
219 +       overlay->path[sizeof(overlay->path) - 1] = '\0';
220 +
221 +       /* strip trailing newlines */
222 +       s = overlay->path + strlen(overlay->path);
223 +       while (s > overlay->path && *--s == '\n')
224 +               *s = '\0';
225 +
226 +       pr_debug("%s: path is '%s'\n", __func__, overlay->path);
227 +
228 +       err = request_firmware(&overlay->fw, overlay->path, NULL);
229 +       if (err != 0)
230 +               goto out_err;
231 +
232 +       err = create_overlay(overlay, (void *)overlay->fw->data);
233 +       if (err != 0)
234 +               goto out_err;
235 +
236 +       return count;
237 +
238 +out_err:
239 +
240 +       release_firmware(overlay->fw);
241 +       overlay->fw = NULL;
242 +
243 +       overlay->path[0] = '\0';
244 +       return err;
245 +}
246 +
247 +static ssize_t cfs_overlay_item_status_show(struct config_item *item,
248 +               char *page)
249 +{
250 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
251 +
252 +       return sprintf(page, "%s\n",
253 +                       overlay->ov_id >= 0 ? "applied" : "unapplied");
254 +}
255 +
256 +CONFIGFS_ATTR(cfs_overlay_item_, path);
257 +CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
258 +
259 +static struct configfs_attribute *cfs_overlay_attrs[] = {
260 +       &cfs_overlay_item_attr_path,
261 +       &cfs_overlay_item_attr_status,
262 +       NULL,
263 +};
264 +
265 +ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
266 +               void *buf, size_t max_count)
267 +{
268 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
269 +
270 +       pr_debug("%s: buf=%p max_count=%zu\n", __func__,
271 +                       buf, max_count);
272 +
273 +       if (overlay->dtbo == NULL)
274 +               return 0;
275 +
276 +       /* copy if buffer provided */
277 +       if (buf != NULL) {
278 +               /* the buffer must be large enough */
279 +               if (overlay->dtbo_size > max_count)
280 +                       return -ENOSPC;
281 +
282 +               memcpy(buf, overlay->dtbo, overlay->dtbo_size);
283 +       }
284 +
285 +       return overlay->dtbo_size;
286 +}
287 +
288 +ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
289 +               const void *buf, size_t count)
290 +{
291 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
292 +       int err;
293 +
294 +       /* if it's set do not allow changes */
295 +       if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
296 +               return -EPERM;
297 +
298 +       /* copy the contents */
299 +       overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
300 +       if (overlay->dtbo == NULL)
301 +               return -ENOMEM;
302 +
303 +       overlay->dtbo_size = count;
304 +
305 +       err = create_overlay(overlay, overlay->dtbo);
306 +       if (err != 0)
307 +               goto out_err;
308 +
309 +       return count;
310 +
311 +out_err:
312 +       kfree(overlay->dtbo);
313 +       overlay->dtbo = NULL;
314 +       overlay->dtbo_size = 0;
315 +
316 +       return err;
317 +}
318 +
319 +CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
320 +
321 +static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
322 +       &cfs_overlay_item_attr_dtbo,
323 +       NULL,
324 +};
325 +
326 +static void cfs_overlay_release(struct config_item *item)
327 +{
328 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
329 +
330 +       if (overlay->ov_id >= 0)
331 +               of_overlay_destroy(overlay->ov_id);
332 +       if (overlay->fw)
333 +               release_firmware(overlay->fw);
334 +       /* kfree with NULL is safe */
335 +       kfree(overlay->dtbo);
336 +       kfree(overlay);
337 +}
338 +
339 +static struct configfs_item_operations cfs_overlay_item_ops = {
340 +       .release        = cfs_overlay_release,
341 +};
342 +
343 +static struct config_item_type cfs_overlay_type = {
344 +       .ct_item_ops    = &cfs_overlay_item_ops,
345 +       .ct_attrs       = cfs_overlay_attrs,
346 +       .ct_bin_attrs   = cfs_overlay_bin_attrs,
347 +       .ct_owner       = THIS_MODULE,
348 +};
349 +
350 +static struct config_item *cfs_overlay_group_make_item(
351 +               struct config_group *group, const char *name)
352 +{
353 +       struct cfs_overlay_item *overlay;
354 +
355 +       overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
356 +       if (!overlay)
357 +               return ERR_PTR(-ENOMEM);
358 +       overlay->ov_id = -1;
359 +
360 +       config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
361 +       return &overlay->item;
362 +}
363 +
364 +static void cfs_overlay_group_drop_item(struct config_group *group,
365 +               struct config_item *item)
366 +{
367 +       struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
368 +
369 +       config_item_put(&overlay->item);
370 +}
371 +
372 +static struct configfs_group_operations overlays_ops = {
373 +       .make_item      = cfs_overlay_group_make_item,
374 +       .drop_item      = cfs_overlay_group_drop_item,
375 +};
376 +
377 +static struct config_item_type overlays_type = {
378 +       .ct_group_ops   = &overlays_ops,
379 +       .ct_owner       = THIS_MODULE,
380 +};
381 +
382 +static struct configfs_group_operations of_cfs_ops = {
383 +       /* empty - we don't allow anything to be created */
384 +};
385 +
386 +static struct config_item_type of_cfs_type = {
387 +       .ct_group_ops   = &of_cfs_ops,
388 +       .ct_owner       = THIS_MODULE,
389 +};
390 +
391 +struct config_group of_cfs_overlay_group;
392 +
393 +static struct configfs_subsystem of_cfs_subsys = {
394 +       .su_group = {
395 +               .cg_item = {
396 +                       .ci_namebuf = "device-tree",
397 +                       .ci_type = &of_cfs_type,
398 +               },
399 +       },
400 +       .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
401 +};
402 +
403 +static int __init of_cfs_init(void)
404 +{
405 +       int ret;
406 +
407 +       pr_info("%s\n", __func__);
408 +
409 +       config_group_init(&of_cfs_subsys.su_group);
410 +       config_group_init_type_name(&of_cfs_overlay_group, "overlays",
411 +                       &overlays_type);
412 +       configfs_add_default_group(&of_cfs_overlay_group,
413 +                       &of_cfs_subsys.su_group);
414 +
415 +       ret = configfs_register_subsystem(&of_cfs_subsys);
416 +       if (ret != 0) {
417 +               pr_err("%s: failed to register subsys\n", __func__);
418 +               goto out;
419 +       }
420 +       pr_info("%s: OK\n", __func__);
421 +out:
422 +       return ret;
423 +}
424 +late_initcall(of_cfs_init);