在platform_driver的探测功能中dev.of_node始终为NULL

问题描述

我在设备驱动程序和设备树绑定中遇到了一些问题。以下是我的设备树:

my_device {
    compatible = "my_driver";
    status = "okay";
};

驱动程序源代码为:

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/of_device.h>


static int my_driver_platform_remove(struct platform_device *pdev)
{
        return 0;
}
  
static int my_driver_platform_probe(struct platform_device *pdev)
{   
    const char* status_prop = NULL;
    if (pdev->dev.of_node == NULL) {
        printk(KERN_ERR "my_driver_platform_probe: Can't find compatible node in device tree\n");
        return -ENOENT;
    }

    status_prop = of_get_property(pdev->dev.of_node,"status",NULL);
    if(status_prop)
        printk(KERN_INFO "my_driver_platform_probe : status prop %s\n",status_prop);

    return 0;
}


static struct of_device_id my_driver_of_match[] = {
        { .compatible = "my_driver"},{}
};
MODULE_DEVICE_TABLE(of,my_driver_of_match);  
  

static struct platform_driver cdottdc_platform_driver = {
        .driver = {
                .name = "my_driver",//.name = MODULE_NAME,.owner = THIS_MODULE,.of_match_table = my_driver_of_match,},.probe = my_driver_platform_probe,.remove = my_driver_platform_remove,};

static void __exit mymodule_exit(void)
{
        platform_driver_unregister(&cdottdc_platform_driver);
    printk(KERN_INFO "mymodule_exit : unregistered platform driver\n");
}
  
static int __init mymodule_init(void)
{
    printk(KERN_INFO "mymodule_init : registering platform driver\n");  
        return platform_driver_register(&cdottdc_platform_driver);
}

module_init(mymodule_init);
module_exit(mymodule_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Sample driver");
MODULE_AUTHOR("Prasanta");
MODULE_VERSION("1.0");

在插入模块时得到以下打印结果:

root@petalinux:~# insmod ./my_driver.ko 
[  149.309748] my_driver: loading out-of-tree module taints kernel.
[  149.316086] mymodule_init : registering platform driver
[  149.321529] my_driver_platform_probe: Can't find compatible node in device tree
[  149.328844] my_driver: probe of my_device Failed with error -2

设备树中已经存在的设备,可以通过以下命令进行确认:

root@petalinux:~# ls /proc/device-tree/my_device/ -l                                                                                      
total 0
-r--r--r--    1 root     root            10 Aug 13 05:17 compatible
-r--r--r--    1 root     root            10 Aug 13 05:17 name
-r--r--r--    1 root     root             5 Aug 13 05:17 status
root@petalinux:~# cat  /proc/device-tree/my_device/status 
okayroot@petalinux:~# cat  /proc/device-tree/my_device/compatible 
my_driverroot@petalinux:~# cat  /proc/device-tree/my_device/name 
my_deviceroot@petalinux:~#

所以我的问题是:

  1. 为什么my_driver_platform_probe调用中的pdev-> dev.of_node为NULL?
  2. 我是否错过了设备树或驱动程序中的某些内容

解决方法

我发现了一个肮脏的修补程序:

if (pdev->dev.of_node == NULL) {
    printk(KERN_ERR "my_driver_platform_probe: Can't find compatible node in device tree. Retrying\n"); 
    pdev->dev.of_node  = of_find_node_by_name(NULL,"mydevice");        
}

但是这不能回答我的实际问题。

,

我面临同样的问题。 因为运行的 linux 内核与内核模块构建环境不同。 我通过使用相同的代码库重新编译 linux 内核和内核模块来解决这个问题。