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

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

问题描述

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

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\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,则模块似乎已加载:

It musts print Hello, world\n, 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\n");
    return 0;
}

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


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.

开发板的设备树的代码段具有

The snippet of the board's Device Tree has

    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.

此外,设备节点应声明为

Also the device node should be declared as

    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天全站免登陆