Release 4.12 drivers/base/component.c
  
  
  
/*
 * Componentized device handling.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This is work in progress.  We gather up the component devices into a list,
 * and bind them when instructed.  At the moment, we're specific to the DRM
 * subsystem, and only handles one master device, but this doesn't have to be
 * the case.
 */
#include <linux/component.h>
#include <linux/device.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
struct component;
struct component_match_array {
	
void *data;
	
int (*compare)(struct device *, void *);
	
void (*release)(struct device *, void *);
	
struct component *component;
	
bool duplicate;
};
struct component_match {
	
size_t alloc;
	
size_t num;
	
struct component_match_array *compare;
};
struct master {
	
struct list_head node;
	
bool bound;
	
const struct component_master_ops *ops;
	
struct device *dev;
	
struct component_match *match;
};
struct component {
	
struct list_head node;
	
struct master *master;
	
bool bound;
	
const struct component_ops *ops;
	
struct device *dev;
};
static DEFINE_MUTEX(component_mutex);
static LIST_HEAD(component_list);
static LIST_HEAD(masters);
static struct master *__master_find(struct device *dev,
	const struct component_master_ops *ops)
{
	struct master *m;
	list_for_each_entry(m, &masters, node)
		if (m->dev == dev && (!ops || m->ops == ops))
			return m;
	return NULL;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 57 | 100.00% | 1 | 100.00% | 
| Total | 57 | 100.00% | 1 | 100.00% | 
static struct component *find_component(struct master *master,
	int (*compare)(struct device *, void *), void *compare_data)
{
	struct component *c;
	list_for_each_entry(c, &component_list, node) {
		if (c->master && c->master != master)
			continue;
		if (compare(c->dev, compare_data))
			return c;
	}
	return NULL;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 76 | 100.00% | 3 | 100.00% | 
| Total | 76 | 100.00% | 3 | 100.00% | 
static int find_components(struct master *master)
{
	struct component_match *match = master->match;
	size_t i;
	int ret = 0;
	/*
         * Scan the array of match functions and attach
         * any components which are found to this master.
         */
	for (i = 0; i < match->num; i++) {
		struct component_match_array *mc = &match->compare[i];
		struct component *c;
		dev_dbg(master->dev, "Looking for component %zu\n", i);
		if (match->compare[i].component)
			continue;
		c = find_component(master, mc->compare, mc->data);
		if (!c) {
			ret = -ENXIO;
			break;
		}
		dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
		/* Attach this component to the master */
		match->compare[i].duplicate = !!c->master;
		match->compare[i].component = c;
		c->master = master;
	}
	return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 173 | 100.00% | 3 | 100.00% | 
| Total | 173 | 100.00% | 3 | 100.00% | 
/* Detach component from associated master */
static void remove_component(struct master *master, struct component *c)
{
	size_t i;
	/* Detach the component from this master. */
	for (i = 0; i < master->match->num; i++)
		if (master->match->compare[i].component == c)
			master->match->compare[i].component = NULL;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 65 | 100.00% | 2 | 100.00% | 
| Total | 65 | 100.00% | 2 | 100.00% | 
/*
 * Try to bring up a master.  If component is NULL, we're interested in
 * this master, otherwise it's a component which must be present to try
 * and bring up the master.
 *
 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
 */
static int try_to_bring_up_master(struct master *master,
	struct component *component)
{
	int ret;
	dev_dbg(master->dev, "trying to bring up master\n");
	if (find_components(master)) {
		dev_dbg(master->dev, "master has incomplete components\n");
		return 0;
	}
	if (component && component->master != master) {
		dev_dbg(master->dev, "master is not for this component (%s)\n",
			dev_name(component->dev));
		return 0;
	}
	if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
		return -ENOMEM;
	/* Found all components */
	ret = master->ops->bind(master->dev);
	if (ret < 0) {
		devres_release_group(master->dev, NULL);
		dev_info(master->dev, "master bind failed: %d\n", ret);
		return ret;
	}
	master->bound = true;
	return 1;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 152 | 100.00% | 5 | 100.00% | 
| Total | 152 | 100.00% | 5 | 100.00% | 
static int try_to_bring_up_masters(struct component *component)
{
	struct master *m;
	int ret = 0;
	list_for_each_entry(m, &masters, node) {
		if (!m->bound) {
			ret = try_to_bring_up_master(m, component);
			if (ret != 0)
				break;
		}
	}
	return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 59 | 100.00% | 2 | 100.00% | 
| Total | 59 | 100.00% | 2 | 100.00% | 
static void take_down_master(struct master *master)
{
	if (master->bound) {
		master->ops->unbind(master->dev);
		devres_release_group(master->dev, NULL);
		master->bound = false;
	}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 45 | 100.00% | 2 | 100.00% | 
| Total | 45 | 100.00% | 2 | 100.00% | 
static void component_match_release(struct device *master,
	struct component_match *match)
{
	unsigned int i;
	for (i = 0; i < match->num; i++) {
		struct component_match_array *mc = &match->compare[i];
		if (mc->release)
			mc->release(master, mc->data);
	}
	kfree(match->compare);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 74 | 100.00% | 3 | 100.00% | 
| Total | 74 | 100.00% | 3 | 100.00% | 
static void devm_component_match_release(struct device *dev, void *res)
{
	component_match_release(dev, res);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 22 | 100.00% | 2 | 100.00% | 
| Total | 22 | 100.00% | 2 | 100.00% | 
static int component_match_realloc(struct device *dev,
	struct component_match *match, size_t num)
{
	struct component_match_array *new;
	if (match->alloc == num)
		return 0;
	new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
	if (!new)
		return -ENOMEM;
	if (match->compare) {
		memcpy(new, match->compare, sizeof(*new) *
					    min(match->num, num));
		kfree(match->compare);
	}
	match->compare = new;
	match->alloc = num;
	return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 113 | 100.00% | 3 | 100.00% | 
| Total | 113 | 100.00% | 3 | 100.00% | 
/*
 * Add a component to be matched, with a release function.
 *
 * The match array is first created or extended if necessary.
 */
void component_match_add_release(struct device *master,
	struct component_match **matchptr,
	void (*release)(struct device *, void *),
	int (*compare)(struct device *, void *), void *compare_data)
{
	struct component_match *match = *matchptr;
	if (IS_ERR(match))
		return;
	if (!match) {
		match = devres_alloc(devm_component_match_release,
				     sizeof(*match), GFP_KERNEL);
		if (!match) {
			*matchptr = ERR_PTR(-ENOMEM);
			return;
		}
		devres_add(master, match);
		*matchptr = match;
	}
	if (match->num == match->alloc) {
		size_t new_size = match->alloc + 16;
		int ret;
		ret = component_match_realloc(master, match, new_size);
		if (ret) {
			*matchptr = ERR_PTR(ret);
			return;
		}
	}
	match->compare[match->num].compare = compare;
	match->compare[match->num].release = release;
	match->compare[match->num].data = compare_data;
	match->compare[match->num].component = NULL;
	match->num++;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 222 | 100.00% | 4 | 100.00% | 
| Total | 222 | 100.00% | 4 | 100.00% | 
EXPORT_SYMBOL(component_match_add_release);
static void free_master(struct master *master)
{
	struct component_match *match = master->match;
	int i;
	list_del(&master->node);
	if (match) {
		for (i = 0; i < match->num; i++) {
			struct component *c = match->compare[i].component;
			if (c)
				c->master = NULL;
		}
	}
	kfree(master);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Jon Medhurst (Tixy) | 83 | 100.00% | 1 | 100.00% | 
| Total | 83 | 100.00% | 1 | 100.00% | 
int component_master_add_with_match(struct device *dev,
	const struct component_master_ops *ops,
	struct component_match *match)
{
	struct master *master;
	int ret;
	/* Reallocate the match array for its true size */
	ret = component_match_realloc(dev, match, match->num);
	if (ret)
		return ret;
	master = kzalloc(sizeof(*master), GFP_KERNEL);
	if (!master)
		return -ENOMEM;
	master->dev = dev;
	master->ops = ops;
	master->match = match;
	/* Add to the list of available masters. */
	mutex_lock(&component_mutex);
	list_add(&master->node, &masters);
	ret = try_to_bring_up_master(master, NULL);
	if (ret < 0)
		free_master(master);
	mutex_unlock(&component_mutex);
	return ret < 0 ? ret : 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 142 | 99.30% | 3 | 75.00% | 
| Jon Medhurst (Tixy) | 1 | 0.70% | 1 | 25.00% | 
| Total | 143 | 100.00% | 4 | 100.00% | 
EXPORT_SYMBOL_GPL(component_master_add_with_match);
void component_master_del(struct device *dev,
	const struct component_master_ops *ops)
{
	struct master *master;
	mutex_lock(&component_mutex);
	master = __master_find(dev, ops);
	if (master) {
		take_down_master(master);
		free_master(master);
	}
	mutex_unlock(&component_mutex);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 57 | 98.28% | 1 | 50.00% | 
| Jon Medhurst (Tixy) | 1 | 1.72% | 1 | 50.00% | 
| Total | 58 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL_GPL(component_master_del);
static void component_unbind(struct component *component,
	struct master *master, void *data)
{
	WARN_ON(!component->bound);
	component->ops->unbind(component->dev, master->dev, data);
	component->bound = false;
	/* Release all resources claimed in the binding of this component */
	devres_release_group(component->dev, component);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 61 | 100.00% | 1 | 100.00% | 
| Total | 61 | 100.00% | 1 | 100.00% | 
void component_unbind_all(struct device *master_dev, void *data)
{
	struct master *master;
	struct component *c;
	size_t i;
	WARN_ON(!mutex_is_locked(&component_mutex));
	master = __master_find(master_dev, NULL);
	if (!master)
		return;
	/* Unbind components in reverse order */
	for (i = master->match->num; i--; )
		if (!master->match->compare[i].duplicate) {
			c = master->match->compare[i].component;
			component_unbind(c, master, data);
		}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 105 | 100.00% | 2 | 100.00% | 
| Total | 105 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL_GPL(component_unbind_all);
static int component_bind(struct component *component, struct master *master,
	void *data)
{
	int ret;
	/*
         * Each component initialises inside its own devres group.
         * This allows us to roll-back a failed component without
         * affecting anything else.
         */
	if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
		return -ENOMEM;
	/*
         * Also open a group for the device itself: this allows us
         * to release the resources claimed against the sub-device
         * at the appropriate moment.
         */
	if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
		devres_release_group(master->dev, NULL);
		return -ENOMEM;
	}
	dev_dbg(master->dev, "binding %s (ops %ps)\n",
		dev_name(component->dev), component->ops);
	ret = component->ops->bind(component->dev, master->dev, data);
	if (!ret) {
		component->bound = true;
		/*
                 * Close the component device's group so that resources
                 * allocated in the binding are encapsulated for removal
                 * at unbind.  Remove the group on the DRM device as we
                 * can clean those resources up independently.
                 */
		devres_close_group(component->dev, NULL);
		devres_remove_group(master->dev, NULL);
		dev_info(master->dev, "bound %s (ops %ps)\n",
			 dev_name(component->dev), component->ops);
	} else {
		devres_release_group(component->dev, NULL);
		devres_release_group(master->dev, NULL);
		dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
			dev_name(component->dev), component->ops, ret);
	}
	return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 209 | 100.00% | 1 | 100.00% | 
| Total | 209 | 100.00% | 1 | 100.00% | 
int component_bind_all(struct device *master_dev, void *data)
{
	struct master *master;
	struct component *c;
	size_t i;
	int ret = 0;
	WARN_ON(!mutex_is_locked(&component_mutex));
	master = __master_find(master_dev, NULL);
	if (!master)
		return -EINVAL;
	/* Bind components in match order */
	for (i = 0; i < master->match->num; i++)
		if (!master->match->compare[i].duplicate) {
			c = master->match->compare[i].component;
			ret = component_bind(c, master, data);
			if (ret)
				break;
		}
	if (ret != 0) {
		for (; i--; )
			if (!master->match->compare[i].duplicate) {
				c = master->match->compare[i].component;
				component_unbind(c, master, data);
			}
	}
	return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 179 | 100.00% | 2 | 100.00% | 
| Total | 179 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL_GPL(component_bind_all);
int component_add(struct device *dev, const struct component_ops *ops)
{
	struct component *component;
	int ret;
	component = kzalloc(sizeof(*component), GFP_KERNEL);
	if (!component)
		return -ENOMEM;
	component->ops = ops;
	component->dev = dev;
	dev_dbg(dev, "adding component (ops %ps)\n", ops);
	mutex_lock(&component_mutex);
	list_add_tail(&component->node, &component_list);
	ret = try_to_bring_up_masters(component);
	if (ret < 0) {
		if (component->master)
			remove_component(component->master, component);
		list_del(&component->node);
		kfree(component);
	}
	mutex_unlock(&component_mutex);
	return ret < 0 ? ret : 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 127 | 89.44% | 1 | 50.00% | 
| Daniel Stone | 15 | 10.56% | 1 | 50.00% | 
| Total | 142 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL_GPL(component_add);
void component_del(struct device *dev, const struct component_ops *ops)
{
	struct component *c, *component = NULL;
	mutex_lock(&component_mutex);
	list_for_each_entry(c, &component_list, node)
		if (c->dev == dev && c->ops == ops) {
			list_del(&c->node);
			component = c;
			break;
		}
	if (component && component->master) {
		take_down_master(component->master);
		remove_component(component->master, component);
	}
	mutex_unlock(&component_mutex);
	WARN_ON(!component);
	kfree(component);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 112 | 100.00% | 2 | 100.00% | 
| Total | 112 | 100.00% | 2 | 100.00% | 
EXPORT_SYMBOL_GPL(component_del);
MODULE_LICENSE("GPL v2");
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp | 
| Russell King | 2253 | 95.75% | 9 | 81.82% | 
| Jon Medhurst (Tixy) | 85 | 3.61% | 1 | 9.09% | 
| Daniel Stone | 15 | 0.64% | 1 | 9.09% | 
| Total | 2353 | 100.00% | 11 | 100.00% | 
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.