kernel - component
Posted qzhang1535
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kernel - component相关的知识,希望对你有一定的参考价值。
component在多个模块相互关联并且存在一定的初始化顺序时非常有用。现分析下其工作原理,以便后续组织自己的驱动模块。
一、component_match分析
component_match在master和component匹配时用于,它包含一个匹配函数指针以后一个void *类型的数据指针,其结构体定义如下:
struct component_match {
size_t alloc;
size_t num;
struct {
void *data;
int (*fn)(struct device *, void *);
} compare[0];
};
- alloc与num主要用于内存的动态分配与管理
- compare主要用于master和component的匹配
- 主要通过component_match_add函数填充match结构体
void component_match_add(struct device *dev, struct component_match **matchptr,
int (*compare)(struct device *, void *), void *compare_data)
{
struct component_match *match = *matchptr;
if (IS_ERR(match))
return;
if (!match || match->num == match->alloc) {
size_t new_size = match ? match->alloc + 16 : 15;
match = component_match_realloc(dev, match, new_size);
*matchptr = match;
if (IS_ERR(match))
return;
}
match->compare[match->num].fn = compare;
match->compare[match->num].data = compare_data;
match->num++;
}
- num记录当前match中compare的个数
- alloc记录当前compare的容量
- 使用参数compare和compare_data填充compare结构体
二、master注册
int component_master_add_with_match(struct device *dev,
const struct component_master_ops *ops,
struct component_match *match)
{
struct master *master;
int ret;
if (ops->add_components && match)
return -EINVAL;
if (match) {
/* Reallocate the match array for its true size */
match = component_match_realloc(dev, match, match->num);
if (IS_ERR(match))
return PTR_ERR(match);
}
master = kzalloc(sizeof(*master), GFP_KERNEL);
if (!master)
return -ENOMEM;
master->dev = dev;
master->ops = ops;
master->match = match;
INIT_LIST_HEAD(&master->components);
/* 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) {
/* Delete off the list if we weren‘t successful */
list_del(&master->node);
kfree(master);
}
mutex_unlock(&component_mutex);
return ret < 0 ? ret : 0;
}
- 该函数是注册一个带有match的master, 如果要注册不带match的master使用component_match_add函数
- 使用dev、ops、match填充master结构体,然后调用try_to_bring_up_master尝试启动master
static int try_to_bring_up_master(struct master *master,
struct component *component)
{
......
if (find_components(master)) {
/* Failed to find all components */
ret = 0;
goto out;
}
......
/* 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
", ret);
goto out;
}
......
}
- 调用find_components查找master对应的component是否全部attached,如果是则返回0
- 如何master对应的component全部attached,则调用master的bind函数
static int find_components(struct master *master)
{
struct component_match *match = master->match;
size_t i;
int ret = 0;
if (!match) {
/*
* Search the list of components, looking for components that
* belong to this master, and attach them to the master.
*/
return master->ops->add_components(master->dev, master);
}
/*
* Scan the array of match functions and attach
* any components which are found to this master.
*/
for (i = 0; i < match->num; i++) {
ret = component_master_add_child(master,
match->compare[i].fn,
match->compare[i].data);
if (ret)
break;
}
return ret;
}
- 由于我们注册的是带match的master,不走if分支而来到for循环
- 在for循环里遍历match,并调用component_master_add_child向master添加component
- 如果match里的每个compare都能成功添加child,则find_components返回成功,否则返回失败
int component_master_add_child(struct master *master,
int (*compare)(struct device *, void *), void *compare_data)
{
struct component *c;
int ret = -ENXIO;
list_for_each_entry(c, &component_list, node) {
if (c->master && c->master != master)
continue;
if (compare(c->dev, compare_data)) {
if (!c->master)
component_attach_master(master, c);
ret = 0;
break;
}
}
return ret;
}
- 遍历component_list,对每个component调用compare函数进行匹配
- 如果匹配成功,调用component_attach_master将component添加到master中, 并返回0。
三、component注册
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)
", ops);
mutex_lock(&component_mutex);
list_add_tail(&component->node, &component_list);
ret = try_to_bring_up_masters(component);
if (ret < 0) {
list_del(&component->node);
kfree(component);
}
mutex_unlock(&component_mutex);
return ret < 0 ? ret : 0;
}
- 使用参数dev和ops填充component结构体,然后添加到component_list
- 然后调用try_to_bring_up_masters尝试启动和些component相关的master
static int try_to_bring_up_masters(struct component *component)
{
struct master *m;
int ret = 0;
list_for_each_entry(m, &masters, node) {
ret = try_to_bring_up_master(m, component);
if (ret != 0)
break;
}
return ret;
}
- 遍历masters并调用try_to_bring_up_master尝试启动master
- try_to_bring_up_master在master注册时已经分析。
以上是关于kernel - component的主要内容,如果未能解决你的问题,请参考以下文章
Symfony Http Kernel Component简介
PCA(principal component analysis)主成分分析降维和KPCA(kernel principal component analysis)核
How to Patch PetaLinux Modifying the kernel, drivers and standalone components
"arch/arm/kernel/head.S"里面一点片段的理解
核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程
解释一下核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程(转载)