首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用设备驱动程序连接设备

使用设备驱动程序连接设备
EN

Stack Overflow用户
提问于 2017-03-09 04:02:30
回答 2查看 510关注 0票数 1

我正在尝试学习Linux平台驱动程序。我从下面的教程中选择了一个驱动程序:

http://linuxseekernel.blogspot.com/2014/05/platform-device-driver-practical.html

它是一个基本的平台驱动程序。我已经编译了它并加载了模块。它加载得很好,但是,它的探测函数从未执行过。有很多文档说,只要设备id和驱动程序id匹配,就会调用探测函数。嗯,我有以下驱动程序:

代码语言:javascript
代码运行次数:0
运行
复制
#include <linux/module.h>
#include <linux/kernel.h>
//for platform drivers....
#include <linux/platform_device.h>
#define DRIVER_NAME "twl12xx"

MODULE_LICENSE("GPL");

/**************/
static int sample_drv_probe(struct platform_device *pdev){
    printk(KERN_ALERT "twl12xx: Probed\n");
    return 0;
}
static int sample_drv_remove(struct platform_device *pdev){
    printk(KERN_ALERT "twl12xx: Removing twl12xx\n");
    return 0;
}

static const struct platform_device_id twl12xx_id_table[] = {
    { "twl12xx", 0},
    {}
};
MODULE_DEVICE_TABLE(platform, twl12xx_id_table);

static struct platform_driver sample_pldriver = {
    .probe          = sample_drv_probe,
    .remove         = sample_drv_remove,
    .driver = {
            .name  = DRIVER_NAME,
    },
};
/**************/

int ourinitmodule(void)
{
    printk(KERN_ALERT "\n Welcome to twl12xx driver.... \n");

    /* Registering with Kernel */
    platform_driver_register(&sample_pldriver);

    return 0;
}

void ourcleanupmodule(void)
{
    printk(KERN_ALERT "\n Thanks....Exiting twl12xx driver... \n");

    /* Unregistering from Kernel */
    platform_driver_unregister(&sample_pldriver);

    return;
}

module_init(ourinitmodule);
module_exit(ourcleanupmodule);

我的设备树中还有以下条目:

代码语言:javascript
代码运行次数:0
运行
复制
twl12xx: twl12xx@2 {
    compatible = "twl12xx";
};

我觉得我一定是遗漏了什么或者错误地定义了我的设备树。

EN

回答 2

Stack Overflow用户

发布于 2017-03-09 22:47:33

无论您读取的是什么,都是正确的;驱动程序和设备ID都应该匹配。

您刚刚创建了驱动程序的框架,并且您正在使用设备树。所以它看起来很好。但是您的代码中缺少of_match_table条目;这对于设备ID匹配(与您从设备树传递的字符串相同)和驱动程序探测器调用非常重要。

因此,添加以下代码更改:

代码语言:javascript
代码运行次数:0
运行
复制
#ifdef CONFIG_OF
static const struct of_device_id twl12xx_dt_ids[] = {
 .compatible = "ti,twl12xx",
 {}
};
MODULE_DEVICE_TABLE(of, twl12xx_dt_ids);
#endif

static struct platform_driver sample_pldriver = {
.probe          = sample_drv_probe,
.remove         = sample_drv_remove,
.driver = {
        .name  = DRIVER_NAME,
        .of_match_table = of_match_ptr(twl12xx_dt_ids),
 },
.id_table    = twl12xx_id_table,
};
票数 1
EN

Stack Overflow用户

发布于 2018-03-09 22:41:15

我也遇到过类似的问题。探测函数也无法打印任何内容。在我的例子中,原因是:在我的linux中,我已经准备好了绑定到设备的驱动程序。当我解绑这个驱动时,探测函数成功地工作了。

(注意,要解除绑定:

代码语言:javascript
代码运行次数:0
运行
复制
    cd /sys/bus/platform/drivers/<driver_name>
    echo "device_name" > unbind

)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42680869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档