摘要
我想编写一个简单的,它将根据从设备树获得的信息初始化一个GPIO。但目前我的模块从未进入探测函数。为了分解它,我创建了一个没有GPIO部分的最小示例。我想在这个最小内核模块中看到的是,探测和删除函数被正确调用。
HW &核
我正在运行一个具有以下内核的Raspberry Pi 3:
$ uname -a
Linux raspberrypi 5.10.92-v7+ #1514 SMP Mon Jan 17 17:36:39 GMT 2022 armv7l GNU/Linux设备树覆盖
下面是我的设备树覆盖层的代码:
/dts-v1/;
/plugin/;
/ {
compatible = "raspberrypi,3-model-b-plusbrcm,bcm2837";
fragment@0 {
target-path = "/";
__overlay__ {
jo_test {
compatible = "j4l,testdev";
status="enabled";
label = "Test";
};
};
};
};我使用以下命令编译它:
dtc -W no-unit_address_vs_reg -I dts -O dtb -o testoverlay.dtbo testoverlay.dts然后我把它装入:
sudo dtoverlay -d . testoverlay.dtbo现在,我可以在/proc/设备树中看到我的覆盖,并且这些值也被正确地分配:
$ sudo ls /proc/device-tree/jo_test
compatible label name status
$ for afile in $(sudo ls /proc/device-tree/jo_test); do echo Name: $afile; sudo cat /proc/device-tree/jo_test/$afile; echo ""; done
Name: compatible
j4l,testdev
Name: label
Test
Name: name
jo_test
Name: status
enabled核模块
下面是我的最小Linux内核模块。我使用设备树的幻灯片作为参考(您可以在这里找到它,幻灯片16-18)/ 1:https://bootlin.com/pub/conferences/2014/elc/petazzoni-device-tree-dummies/petazzoni-device-tree-dummies.pdf
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/gpio.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio/consumer.h>
#include <linux/property.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
/* Meta Information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Johannes 4 GNU/Linux");
MODULE_DESCRIPTION("Module creates a folder and file in procfs and implements read and write callbacks");
static int dt_probe(struct platform_device *pdev);
static int dt_remove(struct platform_device *pdev);
static struct of_device_id my_ids[] = {
{
.compatible = "j4l,testdev",
}, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, my_ids);
static struct platform_driver my_driver = {
.probe = dt_probe,
.remove = dt_remove,
.driver = {
.name = "my_driver",
.of_match_table = my_ids,
},
};
static int dt_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
const struct of_device_id *of_id = of_match_device(my_ids, dev);
printk("dt_test - Now I am in dt_probe\n");
if (!of_id) {
printk("test_dt - Something went wrong...\n");
return -1;
}
return 0;
}
static int dt_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
const struct of_device_id *of_id = of_match_device(my_ids, dev);
printk("dt_test - Now I am in dt_remove\n");
if (!of_id) {
printk("test_dt - Something went wrong...\n");
} else {
printk("dt_test - of_id->name: %s\n",of_id->name);
printk("dt_test - of_id->compatible: %s\n",of_id->compatible);
}
return 0;
}
/**
* @brief This function is called, when the module is loaded into the kernel
*/
static int __init my_init(void)
{
int error;
printk("dt_test - Module Init\n");
error = platform_driver_register(&my_driver);
printk("dt_test: error=%d\n", error);
if (error) {
printk("dt_test - Error probing \n");
return error;
}
return 0;
}
/**
* @brief This function is called, when the module is removed from the kernel
*/
static void __exit my_exit(void)
{
printk("dt_test - Removing module\n");
platform_driver_unregister(&my_driver);
}
module_init(my_init);
module_exit(my_exit);这是我的Makefile
obj-m += dt_test.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean运行我的例子
在编译模块、插入DT覆盖和内核模块之后,我在内核的日志中看到了以下内容:
$ sudo insmod dt_test.ko
$ dmesg | tail -n 5
[ 33.755037] cam1-reg: disabling
[ 33.755061] cam-dummy-reg: disabling
[ 811.832947] dt_test: loading out-of-tree module taints kernel.
[ 811.833341] dt_test - Module Init
[ 811.833643] dt_test: error=0所以,我的模块从来没有进入dt_probe()函数,否则我应该看到“现在我在dt_probe”打印。我不知道,“加载树外模块”意味着什么。似乎模块找不到设备树中的兼容部分.
在第二次尝试中,我将编译后的覆盖复制到/boot/overlays,并使用dtoverlay=testoverlay在/boot/config.txt中添加了覆盖。覆盖出现在/proc/设备树中,但我在加载模块时有相同的行为.
你知不知道,我做错了什么,或者你有什么建议,我如何调试这个?
发布于 2022-05-12 13:21:47
我让它起作用了。删除状态= "okay“的status=”已启用“,修复了它。问题是,我已经将早期版本的OVerlay复制到/boot/overlay中,并且启用了/boot/config/ my overlay。因此,在每次引导时,都加载了一个错误版本的覆盖。但是,在从/boot/overlay和/boot/config中移除覆盖之后,它就工作了。我的结果可以在这里找到:https://github.com/Johannes4Linux/Linux_Driver_Tutorial/tree/main/20_dt_probe
https://stackoverflow.com/questions/72151808
复制相似问题