内核模块中的驱动程序代码不执行?



为什么这个内核模块在加载时什么都不做?

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#define DEVICE_NAME "hello-1.00.a"
#define DRIVER_NAME "hello"
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(struct platform_device *pdev){
    printk(KERN_ALERT "Hello, worldn");
    return 0;
}
static int hello_exit(struct platform_device *pdev){
    printk(KERN_ALERT "Goodbye, cruel worldn");
    return 0;
}
static const struct of_device_id myled_of_match[] =
{
    {.compatible = DEVICE_NAME},
    {},
};
MODULE_DEVICE_TABLE(of, myled_of_match);
static struct platform_driver hello_driver =
    {
        .driver = {
        .name = DRIVER_NAME,
        .owner = THIS_MODULE,
        .of_match_table = myled_of_match
    },
    .probe = hello_init,
    .remove = hello_exit
};
module_platform_driver(hello_driver);

它必须打印Hello, worldn,如果我打印lsmod,模块似乎已加载:

lsmod
hello_world 1538 0 - Live 0xbf000000 (O)

但是在控制台和CCD_ 3中都不打印任何内容。

如果我使用module_initmodule_exit都有效,但我需要指向设备的指针platform_device *pdev,我该怎么办?

编辑:

原来的模块是这样的:

#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void){
    printk(KERN_ALERT "Hello, worldn");
    return 0;
}
static void hello_exit(void){
    printk(KERN_ALERT "Goodbye, cruel worldn");
}

module_init(hello_init);
module_exit(hello_exit);

在我的设备树blob中存在以下条目:

hello {
    compatible = "dglnt,hello-1.00.a";
    reg = <0x41220000 0x10000>;
};

如果我使用module_init和module_exit,则所有工作

那个简短的"原始"代码只包含模块框架。init例程保证在加载模块时被调用,而exit例程则在卸载之前被调用。那个"原始"代码不是驱动程序。

较长的内核模块是一个驱动程序,正在加载,但由于它有默认的init和exit代码,不执行任何操作(由module_platform_driver()宏的扩展生成),因此没有消息。当内核使用设备树时,不能保证可加载模块中的驱动程序代码会被调用。

为什么这个内核模块在加载时什么都不做?

驱动程序的探测函数(将输出消息)可能没有被调用,因为设备树中没有任何内容表明需要此设备驱动程序。

板的设备树的片段具有

    compatible = "dglnt,hello-1.00.a";

但是驱动程序声明它应该指定为

#define DEVICE_NAME "hello-1.00.a"
...   
    {.compatible = DEVICE_NAME},

这些字符串应该匹配,以便驱动程序可以在"设备树"节点中与此引用的设备绑定。

此外,设备节点应声明为

    status = "okay";

以覆盖可能禁用设备的任何默认状态。

设备树中正确配置的节点应按预期执行驱动程序的探测功能。

最新更新