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注冊時已經分析。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM