// SPDX-License-Identifier: GPL-2.0
/*
* Software nodes for the firmware node framework.
*
* Copyright (C) 2018, Intel Corporation
* Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
*/
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/property.h>
#include <linux/slab.h>
#include "base.h"
struct swnode {
struct kobject kobj;
struct fwnode_handle fwnode;
const struct software_node *node;
int id;
/* hierarchy */
struct ida child_ids;
struct list_head entry;
struct list_head children;
struct swnode *parent;
unsigned int allocated:1;
unsigned int managed:1;
};
static DEFINE_IDA(swnode_root_ids);
static struct kset *swnode_kset;
#define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj)
static const struct fwnode_operations software_node_ops;
bool is_software_node(const struct fwnode_handle *fwnode)
{
return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops;
}
EXPORT_SYMBOL_GPL(is_software_node);
#define to_swnode(__fwnode) \
({ \
typeof(__fwnode) __to_swnode_fwnode = __fwnode; \
\
is_software_node(__to_swnode_fwnode) ? \
container_of(__to_swnode_fwnode, \
struct swnode, fwnode) : NULL; \
})
static inline struct swnode *dev_to_swnode(struct device *dev)
{
struct fwnode_handle *fwnode = dev_fwnode(dev);
if (!fwnode)
return NULL;
if (!is_software_node(fwnode))
fwnode = fwnode->secondary;
return to_swnode(fwnode);
}
static struct swnode *
software_node_to_swnode(const struct software_node *node)
{
struct swnode *swnode = NULL;
struct kobject *k;
if (!node)
return NULL;
spin_lock(&swnode_kset->list_lock);
list_for_each_entry(k, &swnode_kset->list, entry) {
swnode = kobj_to_swnode(k);
if (swnode->node == node)
break;
swnode = NULL;
}
spin_unlock(&swnode_kset->list_lock);
return swnode;
}
const struct software_node *to_software_node(const struct fwnode_handle *fwnode)
{
const struct swnode *swnode = to_swnode(fwnode);
return swnode ? swnode->node : NULL;
}
EXPORT_SYMBOL_GPL(to_software_node);
struct fwnode_handle *software_node_fwnode(const struct software_node *node)
{
struct swnode *swnode = software_node_to_swnode(node);
return swnode ? &swnode->fwnode : NULL;
}
EXPORT_SYMBOL_GPL(software_node_fwnode);
/* -------------------------------------------------------------------------- */
/* property_entry processing */
static const struct property_entry *
property_entry_get(const struct property_entry *prop, const char *name)
{
if (!prop)
return NULL;
for (; prop->name; prop++)
if (!strcmp(name, prop->name))
return prop;
return NULL;
}
static const void *property_get_pointer(const struct property_entry *prop)
{
if (!prop->length)
return NULL;
return prop->is_inline ? &prop->value : prop->pointer;
}
static const void *property_entry_find(const struct property_entry *props,
const char *propname, size_t length)
{
const struct property_entry *prop;
const void *pointer;
prop = property_entry_get(props, propname);
if (!prop)
return ERR_PTR(-EINVAL);
pointer = property_get_pointer(prop);
if (!pointer)
return ERR_PTR(-ENODATA);
if (length > prop->length)
return ERR_PTR(-EOVERFLOW);
return pointer;
}
static int
property_entry_count_elems_of_size(const struct property_entry *props,
const char *propname, size_t length)
{
const struct property_entry *prop;
prop = property_entry_get(props, propname