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

标签 linux-kernel linux-device-driver kernel-module device-tree insmod

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

#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);

它必须打印Hello, world\n , 如果我这样做 lsmod该模块似乎已加载:
lsmod
hello_world 1538 0 - Live 0xbf000000 (O)

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

如果我使用 module_initmodule_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);

在我的设备树 blob 中存在此条目:
hello {
    compatible = "dglnt,hello-1.00.a";
    reg = <0x41220000 0x10000>;
};

最佳答案

If i use module_init and module_exit all works



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

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

Why this kernel module doesn't do anything when i load it?



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

板的设备树的片段有
    compatible = "dglnt,hello-1.00.a";

但驱动程序声明它应该指定为
#define DEVICE_NAME "hello-1.00.a"
...   
    {.compatible = DEVICE_NAME},

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

设备节点也应声明为
    status = "okay";

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

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

关于linux-kernel - 内核模块中的驱动程序代码不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840267/

相关文章:

c - 在skb中打印transport_header中的值

c - 如何获取 uinput 创建的设备的名称(路径)

c - Linux内核模块复制进程的.text段

c - 从内核模块中的 AF_UNIX 套接字的 fd 获取绝对路径

linux - 内核开发中是否有类似down_interruptible()的互斥量函数?

linux - Linux 内核模块中的 module_init 和 init_module 有什么区别?

linux - 将内核模块添加到 Debian

linux - 如何在内核和用户空间之间使用 mmap&proc 共享内存

copy_to_user 与 memcpy

linux - Linux 中内核定时器实现示例(内核 2.6.32)