Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include "of_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) /* true when node is initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) static int of_node_is_initialized(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 	return node && node->kobj.state_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) /* true when node is attached (i.e. present on sysfs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) int of_node_is_attached(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	return node && node->kobj.state_in_sysfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #ifndef CONFIG_OF_DYNAMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static void of_node_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	/* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #endif /* CONFIG_OF_DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct kobj_type of_node_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	.release = of_node_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 				struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 				loff_t offset, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct property *pp = container_of(bin_attr, struct property, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* always return newly allocated name, caller must free after use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static const char *safe_name(struct kobject *kobj, const char *orig_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	const char *name = orig_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	/* don't be a hero. After 16 tries give up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		sysfs_put(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		if (name != orig_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (name == orig_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		name = kstrdup(orig_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			kobject_name(kobj), name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) int __of_add_property_sysfs(struct device_node *np, struct property *pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/* Important: Don't leak passwords */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	bool secure = strncmp(pp->name, "security-", 9) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!IS_ENABLED(CONFIG_SYSFS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (!of_kset || !of_node_is_attached(np))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	sysfs_bin_attr_init(&pp->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	pp->attr.attr.name = safe_name(&np->kobj, pp->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	pp->attr.attr.mode = secure ? 0400 : 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	pp->attr.size = secure ? 0 : pp->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	pp->attr.read = of_node_property_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	WARN(rc, "error adding attribute %s to node %pOF\n", pp->name, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (!IS_ENABLED(CONFIG_SYSFS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	sysfs_remove_bin_file(&np->kobj, &prop->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	kfree(prop->attr.attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/* at early boot, bail here and defer setup to of_init() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (of_kset && of_node_is_attached(np))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		__of_sysfs_remove_bin_file(np, prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		struct property *oldprop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* At early boot, bail out and defer setup to of_init() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (!of_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (oldprop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		__of_sysfs_remove_bin_file(np, oldprop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	__of_add_property_sysfs(np, newprop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int __of_attach_node_sysfs(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct kobject *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct property *pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!IS_ENABLED(CONFIG_SYSFS) || !of_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	np->kobj.kset = of_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!np->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		/* Nodes without parents are new top level trees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		name = safe_name(&of_kset->kobj, "base");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		name = safe_name(&np->parent->kobj, kbasename(np->full_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		parent = &np->parent->kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	rc = kobject_add(&np->kobj, parent, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	for_each_property_of_node(np, pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		__of_add_property_sysfs(np, pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void __of_detach_node_sysfs(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct property *pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	BUG_ON(!of_node_is_initialized(np));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (!of_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/* only remove properties if on sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (of_node_is_attached(np)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		for_each_property_of_node(np, pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			__of_sysfs_remove_bin_file(np, pp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		kobject_del(&np->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }