Linux驱动之component

0 前言

   component算是Linux内核中用的比较多的一类结构,这篇文章具体分析一波。

1 component_match_add

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->num = 15, 后面递增 */
    match->compare[match->num].fn = compare;
    match->compare[match->num].data = compare_data;
    match->num++;
}

 2 component_master_add_with_match

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); // 执行master->ops->bind,传入上面的master->dev

    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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值