1 // SPDX-License-Identifier: GPL-2.0
3 * Software nodes for the firmware node framework.
5 * Copyright (C) 2018, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
17 struct fwnode_handle fwnode;
18 const struct software_node *node;
22 struct list_head entry;
23 struct list_head children;
24 struct swnode *parent;
26 unsigned int allocated:1;
29 static DEFINE_IDA(swnode_root_ids);
30 static struct kset *swnode_kset;
32 #define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj)
34 static const struct fwnode_operations software_node_ops;
36 bool is_software_node(const struct fwnode_handle *fwnode)
38 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops;
40 EXPORT_SYMBOL_GPL(is_software_node);
42 #define to_swnode(__fwnode) \
44 typeof(__fwnode) __to_swnode_fwnode = __fwnode; \
46 is_software_node(__to_swnode_fwnode) ? \
47 container_of(__to_swnode_fwnode, \
48 struct swnode, fwnode) : NULL; \
51 static struct swnode *
52 software_node_to_swnode(const struct software_node *node)
54 struct swnode *swnode = NULL;
60 spin_lock(&swnode_kset->list_lock);
62 list_for_each_entry(k, &swnode_kset->list, entry) {
63 swnode = kobj_to_swnode(k);
64 if (swnode->node == node)
69 spin_unlock(&swnode_kset->list_lock);
74 const struct software_node *to_software_node(const struct fwnode_handle *fwnode)
76 const struct swnode *swnode = to_swnode(fwnode);
78 return swnode ? swnode->node : NULL;
80 EXPORT_SYMBOL_GPL(to_software_node);
82 struct fwnode_handle *software_node_fwnode(const struct software_node *node)
84 struct swnode *swnode = software_node_to_swnode(node);
86 return swnode ? &swnode->fwnode : NULL;
88 EXPORT_SYMBOL_GPL(software_node_fwnode);
90 /* -------------------------------------------------------------------------- */
91 /* property_entry processing */
93 static const struct property_entry *
94 property_entry_get(const struct property_entry *prop, const char *name)
99 for (; prop->name; prop++)
100 if (!strcmp(name, prop->name))
106 static const void *property_get_pointer(const struct property_entry *prop)
112 return prop->pointer;
117 static const void *property_entry_find(const struct property_entry *props,
118 const char *propname, size_t length)
120 const struct property_entry *prop;
123 prop = property_entry_get(props, propname);
125 return ERR_PTR(-EINVAL);
126 pointer = property_get_pointer(prop);
128 return ERR_PTR(-ENODATA);
129 if (length > prop->length)
130 return ERR_PTR(-EOVERFLOW);
135 property_entry_count_elems_of_size(const struct property_entry *props,
136 const char *propname, size_t length)
138 const struct property_entry *prop;
140 prop = property_entry_get(props, propname);
144 return prop->length / length;
147 static int property_entry_read_int_array(const struct property_entry *props,
149 unsigned int elem_size, void *val,
156 return property_entry_count_elems_of_size(props, name,
159 if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
162 length = nval * elem_size;
164 pointer = property_entry_find(props, name, length);
166 return PTR_ERR(pointer);
168 memcpy(val, pointer, length);
172 static int property_entry_read_string_array(const struct property_entry *props,
173 const char *propname,
174 const char **strings, size_t nval)
180 /* Find out the array length. */
181 array_len = property_entry_count_elems_of_size(props, propname,
182 sizeof(const char *));
186 /* Return how many there are if strings is NULL. */
190 array_len = min_t(size_t, nval, array_len);
191 length = array_len * sizeof(*strings);
193 pointer = property_entry_find(props, propname, length);
195 return PTR_ERR(pointer);
197 memcpy(strings, pointer, length);
202 static void property_entry_free_data(const struct property_entry *p)
204 const void *pointer = property_get_pointer(p);
205 const char * const *src_str;
209 if (p->type == DEV_PROP_STRING && p->pointer) {
210 src_str = p->pointer;
211 nval = p->length / sizeof(const char *);
212 for (i = 0; i < nval; i++)
216 } else if (p->type == DEV_PROP_STRING) {
222 static const char * const *
223 property_copy_string_array(const struct property_entry *src)
226 const char * const *src_str = src->pointer;
227 size_t nval = src->length / sizeof(*d);
230 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
234 for (i = 0; i < nval; i++) {
235 d[i] = kstrdup(src_str[i], GFP_KERNEL);
236 if (!d[i] && src_str[i]) {
247 static int property_entry_copy_data(struct property_entry *dst,
248 const struct property_entry *src)
250 const void *pointer = property_get_pointer(src);
257 if (src->type == DEV_PROP_STRING) {
258 new = property_copy_string_array(src);
262 new = kmemdup(pointer, src->length, GFP_KERNEL);
267 dst->is_array = true;
269 } else if (src->type == DEV_PROP_STRING) {
270 new = kstrdup(src->value.str, GFP_KERNEL);
271 if (!new && src->value.str)
274 dst->value.str = new;
276 dst->value = src->value;
279 dst->length = src->length;
280 dst->type = src->type;
281 dst->name = kstrdup(src->name, GFP_KERNEL);
288 property_entry_free_data(dst);
293 * property_entries_dup - duplicate array of properties
294 * @properties: array of properties to copy
296 * This function creates a deep copy of the given NULL-terminated array
297 * of property entries.
299 struct property_entry *
300 property_entries_dup(const struct property_entry *properties)
302 struct property_entry *p;
309 while (properties[n].name)
312 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
314 return ERR_PTR(-ENOMEM);
316 for (i = 0; i < n; i++) {
317 ret = property_entry_copy_data(&p[i], &properties[i]);
320 property_entry_free_data(&p[i]);
328 EXPORT_SYMBOL_GPL(property_entries_dup);
331 * property_entries_free - free previously allocated array of properties
332 * @properties: array of properties to destroy
334 * This function frees given NULL-terminated array of property entries,
335 * along with their data.
337 void property_entries_free(const struct property_entry *properties)
339 const struct property_entry *p;
344 for (p = properties; p->name; p++)
345 property_entry_free_data(p);
349 EXPORT_SYMBOL_GPL(property_entries_free);
351 /* -------------------------------------------------------------------------- */
352 /* fwnode operations */
354 static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
356 struct swnode *swnode = to_swnode(fwnode);
358 kobject_get(&swnode->kobj);
360 return &swnode->fwnode;
363 static void software_node_put(struct fwnode_handle *fwnode)
365 struct swnode *swnode = to_swnode(fwnode);
367 kobject_put(&swnode->kobj);
370 static bool software_node_property_present(const struct fwnode_handle *fwnode,
371 const char *propname)
373 struct swnode *swnode = to_swnode(fwnode);
375 return !!property_entry_get(swnode->node->properties, propname);
378 static int software_node_read_int_array(const struct fwnode_handle *fwnode,
379 const char *propname,
380 unsigned int elem_size, void *val,
383 struct swnode *swnode = to_swnode(fwnode);
385 return property_entry_read_int_array(swnode->node->properties, propname,
386 elem_size, val, nval);
389 static int software_node_read_string_array(const struct fwnode_handle *fwnode,
390 const char *propname,
391 const char **val, size_t nval)
393 struct swnode *swnode = to_swnode(fwnode);
395 return property_entry_read_string_array(swnode->node->properties,
396 propname, val, nval);
400 software_node_get_name(const struct fwnode_handle *fwnode)
402 const struct swnode *swnode = to_swnode(fwnode);
407 return kobject_name(&swnode->kobj);
411 software_node_get_name_prefix(const struct fwnode_handle *fwnode)
413 struct fwnode_handle *parent;
416 parent = fwnode_get_parent(fwnode);
420 /* Figure out the prefix from the parents. */
421 while (is_software_node(parent))
422 parent = fwnode_get_next_parent(parent);
424 prefix = fwnode_get_name_prefix(parent);
425 fwnode_handle_put(parent);
427 /* Guess something if prefix was NULL. */
428 return prefix ?: "/";
431 static struct fwnode_handle *
432 software_node_get_parent(const struct fwnode_handle *fwnode)
434 struct swnode *swnode = to_swnode(fwnode);
436 if (!swnode || !swnode->parent)
439 return fwnode_handle_get(&swnode->parent->fwnode);
442 static struct fwnode_handle *
443 software_node_get_next_child(const struct fwnode_handle *fwnode,
444 struct fwnode_handle *child)
446 struct swnode *p = to_swnode(fwnode);
447 struct swnode *c = to_swnode(child);
449 if (!p || list_empty(&p->children) ||
450 (c && list_is_last(&c->entry, &p->children)))
454 c = list_next_entry(c, entry);
456 c = list_first_entry(&p->children, struct swnode, entry);
460 static struct fwnode_handle *
461 software_node_get_named_child_node(const struct fwnode_handle *fwnode,
462 const char *childname)
464 struct swnode *swnode = to_swnode(fwnode);
465 struct swnode *child;
467 if (!swnode || list_empty(&swnode->children))
470 list_for_each_entry(child, &swnode->children, entry) {
471 if (!strcmp(childname, kobject_name(&child->kobj))) {
472 kobject_get(&child->kobj);
473 return &child->fwnode;
480 software_node_get_reference_args(const struct fwnode_handle *fwnode,
481 const char *propname, const char *nargs_prop,
482 unsigned int nargs, unsigned int index,
483 struct fwnode_reference_args *args)
485 struct swnode *swnode = to_swnode(fwnode);
486 const struct software_node_reference *ref;
487 const struct property_entry *prop;
488 struct fwnode_handle *refnode;
491 if (!swnode || !swnode->node->references)
494 for (ref = swnode->node->references; ref->name; ref++)
495 if (!strcmp(ref->name, propname))
498 if (!ref->name || index > (ref->nrefs - 1))
501 refnode = software_node_fwnode(ref->refs[index].node);
506 prop = property_entry_get(swnode->node->properties, nargs_prop);
510 nargs = prop->value.u32_data;
513 if (nargs > NR_FWNODE_REFERENCE_ARGS)
516 args->fwnode = software_node_get(refnode);
519 for (i = 0; i < nargs; i++)
520 args->args[i] = ref->refs[index].args[i];
525 static const struct fwnode_operations software_node_ops = {
526 .get = software_node_get,
527 .put = software_node_put,
528 .property_present = software_node_property_present,
529 .property_read_int_array = software_node_read_int_array,
530 .property_read_string_array = software_node_read_string_array,
531 .get_name = software_node_get_name,
532 .get_name_prefix = software_node_get_name_prefix,
533 .get_parent = software_node_get_parent,
534 .get_next_child_node = software_node_get_next_child,
535 .get_named_child_node = software_node_get_named_child_node,
536 .get_reference_args = software_node_get_reference_args
539 /* -------------------------------------------------------------------------- */
542 * software_node_find_by_name - Find software node by name
543 * @parent: Parent of the software node
544 * @name: Name of the software node
546 * The function will find a node that is child of @parent and that is named
547 * @name. If no node is found, the function returns NULL.
549 * NOTE: you will need to drop the reference with fwnode_handle_put() after use.
551 const struct software_node *
552 software_node_find_by_name(const struct software_node *parent, const char *name)
554 struct swnode *swnode = NULL;
560 spin_lock(&swnode_kset->list_lock);
562 list_for_each_entry(k, &swnode_kset->list, entry) {
563 swnode = kobj_to_swnode(k);
564 if (parent == swnode->node->parent && swnode->node->name &&
565 !strcmp(name, swnode->node->name)) {
566 kobject_get(&swnode->kobj);
572 spin_unlock(&swnode_kset->list_lock);
574 return swnode ? swnode->node : NULL;
576 EXPORT_SYMBOL_GPL(software_node_find_by_name);
579 software_node_register_properties(struct software_node *node,
580 const struct property_entry *properties)
582 struct property_entry *props;
584 props = property_entries_dup(properties);
586 return PTR_ERR(props);
588 node->properties = props;
593 static void software_node_release(struct kobject *kobj)
595 struct swnode *swnode = kobj_to_swnode(kobj);
597 if (swnode->allocated) {
598 property_entries_free(swnode->node->properties);
601 ida_destroy(&swnode->child_ids);
605 static struct kobj_type software_node_type = {
606 .release = software_node_release,
607 .sysfs_ops = &kobj_sysfs_ops,
610 static struct fwnode_handle *
611 swnode_register(const struct software_node *node, struct swnode *parent,
612 unsigned int allocated)
614 struct swnode *swnode;
617 swnode = kzalloc(sizeof(*swnode), GFP_KERNEL);
623 ret = ida_simple_get(parent ? &parent->child_ids : &swnode_root_ids,
632 swnode->parent = parent;
633 swnode->allocated = allocated;
634 swnode->kobj.kset = swnode_kset;
635 swnode->fwnode.ops = &software_node_ops;
637 ida_init(&swnode->child_ids);
638 INIT_LIST_HEAD(&swnode->entry);
639 INIT_LIST_HEAD(&swnode->children);
642 ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
643 parent ? &parent->kobj : NULL,
646 ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
647 parent ? &parent->kobj : NULL,
648 "node%d", swnode->id);
650 kobject_put(&swnode->kobj);
655 list_add_tail(&swnode->entry, &parent->children);
657 kobject_uevent(&swnode->kobj, KOBJ_ADD);
658 return &swnode->fwnode;
662 property_entries_free(node->properties);
667 * software_node_register_nodes - Register an array of software nodes
668 * @nodes: Zero terminated array of software nodes to be registered
670 * Register multiple software nodes at once.
672 int software_node_register_nodes(const struct software_node *nodes)
677 for (i = 0; nodes[i].name; i++) {
678 ret = software_node_register(&nodes[i]);
680 software_node_unregister_nodes(nodes);
687 EXPORT_SYMBOL_GPL(software_node_register_nodes);
690 * software_node_unregister_nodes - Unregister an array of software nodes
691 * @nodes: Zero terminated array of software nodes to be unregistered
693 * Unregister multiple software nodes at once.
695 void software_node_unregister_nodes(const struct software_node *nodes)
697 struct swnode *swnode;
700 for (i = 0; nodes[i].name; i++) {
701 swnode = software_node_to_swnode(&nodes[i]);
703 fwnode_remove_software_node(&swnode->fwnode);
706 EXPORT_SYMBOL_GPL(software_node_unregister_nodes);
709 * software_node_register - Register static software node
710 * @node: The software node to be registered
712 int software_node_register(const struct software_node *node)
714 struct swnode *parent = software_node_to_swnode(node->parent);
716 if (software_node_to_swnode(node))
719 return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0));
721 EXPORT_SYMBOL_GPL(software_node_register);
723 struct fwnode_handle *
724 fwnode_create_software_node(const struct property_entry *properties,
725 const struct fwnode_handle *parent)
727 struct software_node *node;
728 struct swnode *p = NULL;
733 return ERR_CAST(parent);
734 if (!is_software_node(parent))
735 return ERR_PTR(-EINVAL);
736 p = to_swnode(parent);
739 node = kzalloc(sizeof(*node), GFP_KERNEL);
741 return ERR_PTR(-ENOMEM);
743 ret = software_node_register_properties(node, properties);
749 node->parent = p ? p->node : NULL;
751 return swnode_register(node, p, 1);
753 EXPORT_SYMBOL_GPL(fwnode_create_software_node);
755 void fwnode_remove_software_node(struct fwnode_handle *fwnode)
757 struct swnode *swnode = to_swnode(fwnode);
762 if (swnode->parent) {
763 ida_simple_remove(&swnode->parent->child_ids, swnode->id);
764 list_del(&swnode->entry);
766 ida_simple_remove(&swnode_root_ids, swnode->id);
769 kobject_put(&swnode->kobj);
771 EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
773 int software_node_notify(struct device *dev, unsigned long action)
775 struct fwnode_handle *fwnode = dev_fwnode(dev);
776 struct swnode *swnode;
782 if (!is_software_node(fwnode))
783 fwnode = fwnode->secondary;
784 if (!is_software_node(fwnode))
787 swnode = to_swnode(fwnode);
791 ret = sysfs_create_link(&dev->kobj, &swnode->kobj,
796 ret = sysfs_create_link(&swnode->kobj, &dev->kobj,
799 sysfs_remove_link(&dev->kobj, "software_node");
802 kobject_get(&swnode->kobj);
805 sysfs_remove_link(&swnode->kobj, dev_name(dev));
806 sysfs_remove_link(&dev->kobj, "software_node");
807 kobject_put(&swnode->kobj);
816 static int __init software_node_init(void)
818 swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj);
823 postcore_initcall(software_node_init);
825 static void __exit software_node_exit(void)
827 ida_destroy(&swnode_root_ids);
828 kset_unregister(swnode_kset);
830 __exitcall(software_node_exit);