我在设备驱动程序和设备树绑定方面面临一些问题。以下是我的设备树:
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:~#所以我的问题是:
发布于 2020-08-27 10:32:05
我发现了一个肮脏的修复方法:
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");
}然而,这并没有回答我的实际问题。
发布于 2021-01-04 09:29:07
我也面临着同样的问题。因为运行的linux内核与内核模块构建环境不同。我通过用相同的代码库重新编译linux内核和内核模块来解决这个问题。
https://stackoverflow.com/questions/63388663
复制相似问题