查找struct device *dev

来源:互联网 发布:淄博网络推广哪家好 编辑:程序博客网 时间:2024/06/07 00:09
在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查找范围更广一些.

原创粉丝点击