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

标签 linux-kernel linux-device-driver device device-tree

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

  • 可以使用指向 of_device 的指针来获取指针。那是我们想要的设备的父级吗?
  • 如何仅使用名称获得指向已实例化类的指针?如果可能,我可以遍历该类的设备。

  • 任何其他建议都会非常有用!谢谢你们。

    最佳答案

    我找到了方法。我简要解释一下,也许它可能有用。我们可以使用的方法是 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;
    }
    

    关于linux-kernel - 在 linux 中获取指向结构设备的指针的更简洁方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7636801/

    相关文章:

    device - 从/dev/input 获取实时信息

    android - 如何在 Android Studio 中将应用上传到设备上

    module - 如何修复 : fatal error: openssl/opensslv. h:RedHat 7 中没有此类文件或目录

    io - 以零长度调用 write() 的后果是什么?

    linux - 如何更改MTD设备序列号?

    linux - 学习网络驱动程序接口(interface)的好链接

    linux - 是否可以将 2 个内核模块链接到同一个/dev/device?

    linux - 低延迟中断处理(从内核返回到用户空间的预期平均时间是?)

    linux - 如何将依赖模块插入内核?

    linux - 这是 linux 内核中有关写入/proc/self/loginuid 的错误吗?