您的位置:首页 > 其它

查找struct device *dev

2017-06-22 11:53 211 查看
在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查找范围更广一些.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: