在linux中获取指向结构设备指针的更简洁方法是什么?

Mir*_*chi 4 device linux-device-driver linux-kernel device-tree

我需要获取指向linux中注册的特定设备的指针.简而言之,该设备代表一个mii_bus对象.问题是这个设备似乎不属于总线(它dev->busNULL)所以我不能使用例如功能bus_for_each_dev.然而,该设备由Open Firmware层注册,我可以看到相对of_device(我感兴趣的设备的父亲)/sys/bus/of_platform.我的设备也注册了,class所以我可以找到它/sys/class/mdio_bus.现在的问题是:

  1. 可以使用指向of_device我们想要的设备的父指针来获取指针吗?

  2. 如何通过仅使用名称来获取指向已经实例化的类的指针?如果有可能,我可以迭代该类的设备.

任何其他建议都非常有用!谢谢你们.

Mir*_*chi 5

我找到了方法.我简要解释一下,也许它可能有用.我们可以使用的方法是device_find_child.该方法将第三个参数作为指向实现比较逻辑的函数的指针.如果在使用特定设备作为第一个参数调用时函数返回非零,device_find_child则返回该指针.

#include <linux/device.h>
#include <linux/of_platform.h>

static int custom_match_dev(struct device *dev, void *data)
{
  /* this function implements the comaparison logic. Return not zero if device
     pointed by dev is the device you are searching for.
   */
}

static struct device *find_dev()
{
  struct device *ofdev = bus_find_device_by_name(&of_platform_bus_type,
                                                 NULL, "OF_device_name");
  if (ofdev)
  {
    /* of device is the parent of device we are interested in */

    struct device *real_dev = device_find_child(ofdev,
                                                NULL, /* passed in the second param to custom_match_dev */
                                                custom_match_dev);
    if (real_dev)
      return real_dev;
  }
  return NULL;
}
Run Code Online (Sandbox Code Playgroud)

  • 请记住 device_find_child() 会增加找到的子设备的引用计数器。所以,当你使用完子设备时,你**必须**做 put_device(); (2认同)