查找struct device *dev

本文介绍了在Linux内核中两种常用的方法来查找设备:通过class使用`class_find_device`和通过bus使用`bus_find_device`。前者效率更高但范围有限,后者范围更广但可能效率较低。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在driver中经常要找到另外一个struct device *dev。这时候有两种办法,一种是根据这个device所在的class查找
例如下例。即调用class_find_device
static int __ae_match(struct device *dev, const void *data)
{
    struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);

    if (dev_of_node(hdev->dev))
        return (data == &hdev->dev->of_node->fwnode);
    else if (is_acpi_node(hdev->dev->fwnode))
        return (data == hdev->dev->fwnode);

    dev_err(dev, "__ae_match cannot read cfg data from OF or acpi\n");
    return 0;
}

static struct hnae_ae_dev *find_ae(const struct fwnode_handle *fwnode)
{
    struct device *dev;

    WARN_ON(!fwnode);

    dev = class_find_device(hnae_class, NULL, fwnode, __ae_match);

    return dev ? cls_to_ae_dev(dev) : NULL;
}

第二中是通过bus来查找,即调用bus_find_device
static int hns_roce_node_match(struct device *dev, void *fwnode)
{
    return dev->fwnode == fwnode;
}

static struct
platform_device *hns_roce_find_pdev(struct fwnode_handle *fwnode)
{
    struct device *dev;

    /* get the 'device'corresponding to matching 'fwnode' */
    dev = bus_find_device(&platform_bus_type, NULL,
                  fwnode, hns_roce_node_match);
    /* get the platform device */
    return dev ? to_platform_device(dev) : NULL;
}
很明显通过class查找效率会高一点,通过bus查找范围更广一些.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值