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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Software nodes for the firmware node framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2018, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) struct swnode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	struct kobject kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct fwnode_handle fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	const struct software_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	/* hierarchy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct ida child_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct list_head entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct list_head children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct swnode *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int allocated:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static DEFINE_IDA(swnode_root_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static struct kset *swnode_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static const struct fwnode_operations software_node_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) bool is_software_node(const struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) EXPORT_SYMBOL_GPL(is_software_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define to_swnode(__fwnode)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	({								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		typeof(__fwnode) __to_swnode_fwnode = __fwnode;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		is_software_node(__to_swnode_fwnode) ?			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			container_of(__to_swnode_fwnode,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				     struct swnode, fwnode) : NULL;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static inline struct swnode *dev_to_swnode(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct fwnode_handle *fwnode = dev_fwnode(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (!fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (!is_software_node(fwnode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		fwnode = fwnode->secondary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static struct swnode *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) software_node_to_swnode(const struct software_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct swnode *swnode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct kobject *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	spin_lock(&swnode_kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	list_for_each_entry(k, &swnode_kset->list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		swnode = kobj_to_swnode(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (swnode->node == node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		swnode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	spin_unlock(&swnode_kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return swnode;
^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) const struct software_node *to_software_node(const struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	const struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return swnode ? swnode->node : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) EXPORT_SYMBOL_GPL(to_software_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) struct fwnode_handle *software_node_fwnode(const struct software_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct swnode *swnode = software_node_to_swnode(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return swnode ? &swnode->fwnode : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) EXPORT_SYMBOL_GPL(software_node_fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* -------------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* property_entry processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static const struct property_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) property_entry_get(const struct property_entry *prop, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	for (; prop->name; prop++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (!strcmp(name, prop->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			return prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static const void *property_get_pointer(const struct property_entry *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!prop->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return prop->is_inline ? &prop->value : prop->pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static const void *property_entry_find(const struct property_entry *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				       const char *propname, size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	const struct property_entry *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	const void *pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	prop = property_entry_get(props, propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	pointer = property_get_pointer(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (!pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return ERR_PTR(-ENODATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (length > prop->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return ERR_PTR(-EOVERFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) property_entry_count_elems_of_size(const struct property_entry *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				   const char *propname, size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	const struct property_entry *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	prop = property_entry_get(props, propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return prop->length / length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int property_entry_read_int_array(const struct property_entry *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 					 const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					 unsigned int elem_size, void *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 					 size_t nval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	const void *pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return property_entry_count_elems_of_size(props, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 							  elem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	length = nval * elem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	pointer = property_entry_find(props, name, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (IS_ERR(pointer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return PTR_ERR(pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	memcpy(val, pointer, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int property_entry_read_string_array(const struct property_entry *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 					    const char *propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 					    const char **strings, size_t nval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	const void *pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int array_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/* Find out the array length. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	array_len = property_entry_count_elems_of_size(props, propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 						       sizeof(const char *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (array_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return array_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	/* Return how many there are if strings is NULL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (!strings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return array_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	array_len = min_t(size_t, nval, array_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	length = array_len * sizeof(*strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	pointer = property_entry_find(props, propname, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (IS_ERR(pointer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return PTR_ERR(pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	memcpy(strings, pointer, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return array_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static void property_entry_free_data(const struct property_entry *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	const char * const *src_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	size_t i, nval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (p->type == DEV_PROP_STRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		src_str = property_get_pointer(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		nval = p->length / sizeof(*src_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		for (i = 0; i < nval; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			kfree(src_str[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (!p->is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		kfree(p->pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	kfree(p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static bool property_copy_string_array(const char **dst_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				       const char * const *src_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				       size_t nval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	for (i = 0; i < nval; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		dst_ptr[i] = kstrdup(src_ptr[i], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (!dst_ptr[i] && src_ptr[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				kfree(dst_ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int property_entry_copy_data(struct property_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				    const struct property_entry *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	const void *pointer = property_get_pointer(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	void *dst_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	size_t nval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * Properties with no data should not be marked as stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 * out of line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (!src->is_inline && !src->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 * Reference properties are never stored inline as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * they are too big.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (src->type == DEV_PROP_REF && src->is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (src->length <= sizeof(dst->value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		dst_ptr = &dst->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		dst->is_inline = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		dst_ptr = kmalloc(src->length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (!dst_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		dst->pointer = dst_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (src->type == DEV_PROP_STRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		nval = src->length / sizeof(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (!property_copy_string_array(dst_ptr, pointer, nval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			if (!dst->is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				kfree(dst->pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		memcpy(dst_ptr, pointer, src->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	dst->length = src->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	dst->type = src->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	dst->name = kstrdup(src->name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (!dst->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		property_entry_free_data(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * property_entries_dup - duplicate array of properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * @properties: array of properties to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * This function creates a deep copy of the given NULL-terminated array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * of property entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct property_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) property_entries_dup(const struct property_entry *properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct property_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	int i, n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (!properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	while (properties[n].name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	for (i = 0; i < n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		ret = property_entry_copy_data(&p[i], &properties[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				property_entry_free_data(&p[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) EXPORT_SYMBOL_GPL(property_entries_dup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * property_entries_free - free previously allocated array of properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * @properties: array of properties to destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * This function frees given NULL-terminated array of property entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * along with their data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) void property_entries_free(const struct property_entry *properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	const struct property_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (!properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	for (p = properties; p->name; p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		property_entry_free_data(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	kfree(properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) EXPORT_SYMBOL_GPL(property_entries_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* -------------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* fwnode operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	kobject_get(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	return &swnode->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static void software_node_put(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	kobject_put(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static bool software_node_property_present(const struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 					   const char *propname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	return !!property_entry_get(swnode->node->properties, propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static int software_node_read_int_array(const struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 					const char *propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 					unsigned int elem_size, void *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 					size_t nval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return property_entry_read_int_array(swnode->node->properties, propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 					     elem_size, val, nval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static int software_node_read_string_array(const struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 					   const char *propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 					   const char **val, size_t nval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return property_entry_read_string_array(swnode->node->properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 						propname, val, nval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) software_node_get_name(const struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	const struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (!swnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		return "(null)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return kobject_name(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) software_node_get_name_prefix(const struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct fwnode_handle *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	const char *prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	parent = fwnode_get_parent(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		return "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	/* Figure out the prefix from the parents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	while (is_software_node(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		parent = fwnode_get_next_parent(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	prefix = fwnode_get_name_prefix(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	fwnode_handle_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	/* Guess something if prefix was NULL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return prefix ?: "/";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static struct fwnode_handle *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) software_node_get_parent(const struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (!swnode || !swnode->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	return fwnode_handle_get(&swnode->parent->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static struct fwnode_handle *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) software_node_get_next_child(const struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			     struct fwnode_handle *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct swnode *p = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	struct swnode *c = to_swnode(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (!p || list_empty(&p->children) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	    (c && list_is_last(&c->entry, &p->children))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		c = list_next_entry(c, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		c = list_first_entry(&p->children, struct swnode, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	return fwnode_handle_get(&c->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static struct fwnode_handle *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) software_node_get_named_child_node(const struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				   const char *childname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	struct swnode *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	if (!swnode || list_empty(&swnode->children))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	list_for_each_entry(child, &swnode->children, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		if (!strcmp(childname, kobject_name(&child->kobj))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			kobject_get(&child->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			return &child->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) software_node_get_reference_args(const struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 				 const char *propname, const char *nargs_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 				 unsigned int nargs, unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 				 struct fwnode_reference_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	const struct software_node_ref_args *ref_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	const struct software_node_ref_args *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	const struct property_entry *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	struct fwnode_handle *refnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	u32 nargs_prop_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (!swnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	prop = property_entry_get(swnode->node->properties, propname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (prop->type != DEV_PROP_REF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	 * We expect that references are never stored inline, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	 * single ones, as they are too big.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	if (prop->is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (index * sizeof(*ref) >= prop->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	ref_array = prop->pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	ref = &ref_array[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	refnode = software_node_fwnode(ref->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (!refnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (nargs_prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		error = property_entry_read_int_array(ref->node->properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 						      nargs_prop, sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 						      &nargs_prop_val, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		nargs = nargs_prop_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (nargs > NR_FWNODE_REFERENCE_ARGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	args->fwnode = software_node_get(refnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	args->nargs = nargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	for (i = 0; i < nargs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		args->args[i] = ref->args[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static const struct fwnode_operations software_node_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	.get = software_node_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	.put = software_node_put,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	.property_present = software_node_property_present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	.property_read_int_array = software_node_read_int_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	.property_read_string_array = software_node_read_string_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	.get_name = software_node_get_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	.get_name_prefix = software_node_get_name_prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	.get_parent = software_node_get_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	.get_next_child_node = software_node_get_next_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	.get_named_child_node = software_node_get_named_child_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	.get_reference_args = software_node_get_reference_args
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /* -------------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)  * software_node_find_by_name - Find software node by name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)  * @parent: Parent of the software node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)  * @name: Name of the software node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)  * The function will find a node that is child of @parent and that is named
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)  * @name. If no node is found, the function returns NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)  * NOTE: you will need to drop the reference with fwnode_handle_put() after use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) const struct software_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) software_node_find_by_name(const struct software_node *parent, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	struct swnode *swnode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	struct kobject *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	spin_lock(&swnode_kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	list_for_each_entry(k, &swnode_kset->list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		swnode = kobj_to_swnode(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		if (parent == swnode->node->parent && swnode->node->name &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		    !strcmp(name, swnode->node->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 			kobject_get(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		swnode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	spin_unlock(&swnode_kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	return swnode ? swnode->node : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) EXPORT_SYMBOL_GPL(software_node_find_by_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) software_node_register_properties(struct software_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 				  const struct property_entry *properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	struct property_entry *props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	props = property_entries_dup(properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	if (IS_ERR(props))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		return PTR_ERR(props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	node->properties = props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) static void software_node_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	struct swnode *swnode = kobj_to_swnode(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	if (swnode->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		ida_simple_remove(&swnode->parent->child_ids, swnode->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		list_del(&swnode->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		ida_simple_remove(&swnode_root_ids, swnode->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	if (swnode->allocated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		property_entries_free(swnode->node->properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		kfree(swnode->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	ida_destroy(&swnode->child_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	kfree(swnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static struct kobj_type software_node_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	.release = software_node_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	.sysfs_ops = &kobj_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static struct fwnode_handle *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) swnode_register(const struct software_node *node, struct swnode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		unsigned int allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	struct swnode *swnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	swnode = kzalloc(sizeof(*swnode), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	if (!swnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	ret = ida_simple_get(parent ? &parent->child_ids : &swnode_root_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			     0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		kfree(swnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	swnode->id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	swnode->node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	swnode->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	swnode->allocated = allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	swnode->kobj.kset = swnode_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	fwnode_init(&swnode->fwnode, &software_node_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	ida_init(&swnode->child_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	INIT_LIST_HEAD(&swnode->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	INIT_LIST_HEAD(&swnode->children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	if (node->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 					   parent ? &parent->kobj : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 					   "%s", node->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 					   parent ? &parent->kobj : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 					   "node%d", swnode->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		kobject_put(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		list_add_tail(&swnode->entry, &parent->children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	kobject_uevent(&swnode->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	return &swnode->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	if (allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		property_entries_free(node->properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)  * software_node_register_nodes - Register an array of software nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)  * @nodes: Zero terminated array of software nodes to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)  * Register multiple software nodes at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) int software_node_register_nodes(const struct software_node *nodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	for (i = 0; nodes[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		ret = software_node_register(&nodes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			software_node_unregister_nodes(nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) EXPORT_SYMBOL_GPL(software_node_register_nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)  * software_node_unregister_nodes - Unregister an array of software nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)  * @nodes: Zero terminated array of software nodes to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)  * Unregister multiple software nodes at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)  * NOTE: Be careful using this call if the nodes had parent pointers set up in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)  * them before registering.  If so, it is wiser to remove the nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)  * individually, in the correct order (child before parent) instead of relying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)  * on the sequential order of the list of nodes in the array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) void software_node_unregister_nodes(const struct software_node *nodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	for (i = 0; nodes[i].name; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		software_node_unregister(&nodes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) EXPORT_SYMBOL_GPL(software_node_unregister_nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)  * software_node_register_node_group - Register a group of software nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)  * @node_group: NULL terminated array of software node pointers to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)  * Register multiple software nodes at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) int software_node_register_node_group(const struct software_node **node_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	if (!node_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	for (i = 0; node_group[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		ret = software_node_register(node_group[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 			software_node_unregister_node_group(node_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) EXPORT_SYMBOL_GPL(software_node_register_node_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)  * software_node_unregister_node_group - Unregister a group of software nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)  * @node_group: NULL terminated array of software node pointers to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)  * Unregister multiple software nodes at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) void software_node_unregister_node_group(const struct software_node **node_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (!node_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	for (i = 0; node_group[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 		software_node_unregister(node_group[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) EXPORT_SYMBOL_GPL(software_node_unregister_node_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)  * software_node_register - Register static software node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)  * @node: The software node to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) int software_node_register(const struct software_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	struct swnode *parent = software_node_to_swnode(node->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	if (software_node_to_swnode(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 		return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	if (node->parent && !parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) EXPORT_SYMBOL_GPL(software_node_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)  * software_node_unregister - Unregister static software node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)  * @node: The software node to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) void software_node_unregister(const struct software_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	struct swnode *swnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	swnode = software_node_to_swnode(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	if (swnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 		fwnode_remove_software_node(&swnode->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) EXPORT_SYMBOL_GPL(software_node_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) struct fwnode_handle *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) fwnode_create_software_node(const struct property_entry *properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 			    const struct fwnode_handle *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	struct software_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	struct swnode *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 		if (IS_ERR(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 			return ERR_CAST(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		if (!is_software_node(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 		p = to_swnode(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	node = kzalloc(sizeof(*node), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	ret = software_node_register_properties(node, properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 		kfree(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	node->parent = p ? p->node : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	return swnode_register(node, p, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) EXPORT_SYMBOL_GPL(fwnode_create_software_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) void fwnode_remove_software_node(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	struct swnode *swnode = to_swnode(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	if (!swnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	kobject_put(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)  * device_add_software_node - Assign software node to a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)  * @dev: The device the software node is meant for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)  * @node: The software node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)  * This function will make @node the secondary firmware node pointer of @dev. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)  * @dev has no primary node, then @node will become the primary node. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)  * function will register @node automatically if it wasn't already registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) int device_add_software_node(struct device *dev, const struct software_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	struct swnode *swnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	/* Only one software node per device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	if (dev_to_swnode(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	swnode = software_node_to_swnode(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	if (swnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 		kobject_get(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 		ret = software_node_register(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 		swnode = software_node_to_swnode(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	set_secondary_fwnode(dev, &swnode->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	 * If the device has been fully registered by the time this function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	 * called, software_node_notify() must be called separately so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	 * symlinks get created and the reference count of the node is kept in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	 * balance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	if (device_is_registered(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 		software_node_notify(dev, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) EXPORT_SYMBOL_GPL(device_add_software_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)  * device_remove_software_node - Remove device's software node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)  * @dev: The device with the software node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)  * This function will unregister the software node of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) void device_remove_software_node(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	struct swnode *swnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	swnode = dev_to_swnode(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	if (!swnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	if (device_is_registered(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 		software_node_notify(dev, KOBJ_REMOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	set_secondary_fwnode(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	kobject_put(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) EXPORT_SYMBOL_GPL(device_remove_software_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) int software_node_notify(struct device *dev, unsigned long action)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	struct swnode *swnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 	swnode = dev_to_swnode(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	if (!swnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	case KOBJ_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 		ret = sysfs_create_link(&dev->kobj, &swnode->kobj, "software_node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 		ret = sysfs_create_link(&swnode->kobj, &dev->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 					dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 			sysfs_remove_link(&dev->kobj, "software_node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 		kobject_get(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) 	case KOBJ_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 		sysfs_remove_link(&swnode->kobj, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 		sysfs_remove_link(&dev->kobj, "software_node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) 		kobject_put(&swnode->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) static int __init software_node_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) 	swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) 	if (!swnode_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) postcore_initcall(software_node_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) static void __exit software_node_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) 	ida_destroy(&swnode_root_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) 	kset_unregister(swnode_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) __exitcall(software_node_exit);