内核模块中的驱动程序代码不执行? [英] Driver code in kernel module doesn't execute?

查看:33
本文介绍了内核模块中的驱动程序代码不执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

#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
");
    return 0;
}
static int hello_exit(struct platform_device *pdev){
    printk(KERN_ALERT "Goodbye, cruel world
");
    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 ,如果我执行 lsmod 模块似乎已加载:

It musts print Hello, world , if i do lsmod the module appear to be loaded:

lsmod
hello_world 1538 0 - Live 0xbf000000 (O)

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

but nothing is printed neither in the console nor in dmesg.

如果我使用 module_initmodule_exit 一切正常,但我需要指向设备的指针 platform_device *pdev,我该怎么办?

If i use module_init and module_exit all works, but i need the pointer platform_device *pdev to the device, what can i do?

原始模块如下所示:

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

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

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


module_init(hello_init);
module_exit(hello_exit);

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

In my device tree blob is present this entry:

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

推荐答案

如果我使用 module_init 和 module_exit 一切正常

If i use module_init and module_exit all works

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

That short "original" code only consists of the module framework. The init routine is guaranteed to be called when the module is loaded, and the exit routine is called prior to unloading. That "original" code is not a driver.

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

The longer kernel module is a driver and getting loaded, but since it has the default init and exit code that does nothing (as generated by the expansion of the module_platform_driver() macro), there are no messages. The driver code in the loadable module is not guaranteed to be called when the kernel uses a Device Tree.

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

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

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

The probe function of the driver (which would output messages) is probably not getting called because there is nothing in your Device Tree that indicates that this device driver is needed.

电路板设备树的片段有

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

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

but the driver declares that it should specified as

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

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

These strings should match so that the driver can bind with this referenced device in the Device Tree node.

设备节点也应该声明为

    status = "okay";

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

to override any default status that could disable the device.

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

A properly configured node in the Device Tree should get the driver's probe function to be executed as expected.

这篇关于内核模块中的驱动程序代码不执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆