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

Luc*_*uca 5 insmod kernel-module linux-device-driver linux-kernel device-tree

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

#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, world\n");
    return 0;
}
static int hello_exit(struct platform_device *pdev){
    printk(KERN_ALERT "Goodbye, cruel world\n");
    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);
Run Code Online (Sandbox Code Playgroud)

它必须打印Hello, world\n,如果我这样做lsmod模块似乎被加载:

lsmod
hello_world 1538 0 - Live 0xbf000000 (O)
Run Code Online (Sandbox Code Playgroud)

但是在控制台和控制台上都没有打印任何内容dmesg.

如果我使用module_init并且module_exit所有工作,但我需要指向platform_device *pdev设备的指针,我该怎么办?

编辑:

原始模块看起来像这样:

#include <linux/init.h>
#include <linux/module.h>

static int hello_init(void){
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}

static void hello_exit(void){
    printk(KERN_ALERT "Goodbye, cruel world\n");
}


module_init(hello_init);
module_exit(hello_exit);
Run Code Online (Sandbox Code Playgroud)

在我的设备树blob中出现此条目:

hello {
    compatible = "dglnt,hello-1.00.a";
    reg = <0x41220000 0x10000>;
};
Run Code Online (Sandbox Code Playgroud)

saw*_*ust 6

如果我使用module_init和module_exit都可以

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

较长的内核模块是驱动程序并且正在加载,但由于它具有无效的默认init和退出代码(由module_platform_driver()宏的扩展生成),因此没有消息.当内核使用设备树时,不保证可以调用可加载模块中的驱动程序代码.

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

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

电路板的设备树的片段有

    compatible = "dglnt,hello-1.00.a";
Run Code Online (Sandbox Code Playgroud)

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

#define DEVICE_NAME "hello-1.00.a"
...   
    {.compatible = DEVICE_NAME},
Run Code Online (Sandbox Code Playgroud)

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

设备节点也应声明为

    status = "okay";
Run Code Online (Sandbox Code Playgroud)

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

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