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_master_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\n", 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)\n", 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注册时已经分析。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值