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