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

Linux 使用设备驱动程序连接设备,linux,linux-kernel,linux-device-driver,embedded-linux,Linux,Linux Kernel,Linux Device Driver,Embedded Linux,我正在努力学习Linux平台驱动程序。我从以下教程中获取了一个驱动程序: 它是一个基本的平台驱动程序。我已经编译并加载了模块。它可以很好地加载,但是它的探测函数永远不会执行。有很多文档说明,只要设备id和驱动程序id匹配,就可以调用探测函数。我有一个司机: #include <linux/module.h> #include <linux/kernel.h> //for platform drivers.... #include <linux/platform_d

我正在努力学习Linux平台驱动程序。我从以下教程中获取了一个驱动程序:

它是一个基本的平台驱动程序。我已经编译并加载了模块。它可以很好地加载,但是它的探测函数永远不会执行。有很多文档说明,只要设备id和驱动程序id匹配,就可以调用探测函数。我有一个司机:

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

我觉得我一定是遗漏了什么,或者我的设备树定义不正确。

你读到的都是正确的;驱动程序和设备ID都应该匹配

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

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

#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,
};

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

(注意,要解除绑定:

    cd /sys/bus/platform/drivers/<driver_name>
    echo "device_name" > unbind
cd/sys/bus/platform/drivers/
回显“设备名称”>解除绑定

)

您使用的指南不适用于设备树或开放式固件;作者可能是以x86为中心的。您编写的驱动程序未准备好DT,例如,它不包括linux/of.h,并且没有带有兼容字符串的\u device\u id
结构。内核中有许多平台驱动程序可用作示例。还可以看到@sawdust的可能副本,没有更多以x86为中心的代码,我希望应该少一点以DT为中心,提到的指南可能已经过时了。如今,在其他功能中,统一的设备属性允许编写驱动程序,而不管谁是资源提供者。所以,不要责怪体系结构,责怪开发人员,他们编写了以XYZ为中心的代码。@0andriy——你的咆哮毫无意义,因为你似乎在通过评论阅读一些根本不存在的东西,例如“责怪体系结构”。
    cd /sys/bus/platform/drivers/<driver_name>
    echo "device_name" > unbind