Linux设备驱动程序注册程序



我是一个linux新手,试图了解linux设备模型。我一直在浏览Linux 3.1.6代码库,特别是驱动程序部分,并发现

  1. 一些驱动程序正在使用(例如i2c-bus device: linux-3.1.6/drivers/i2c/i2c-dev.c) *register_chrdev()*和
  2. 其他一些(例如pci bus: linux-3.1.6/drivers/pci/bus.c)使用了*device_register()*.

我的问题是什么时候使用register_chrdev(是的,我知道它的字符设备,但为什么不使用device_register)和device_register ?

这是否取决于驱动程序开发人员希望他的设备/驱动程序被列出的位置,如devfs vs sysfs ?还是将接口暴露给用户空间以访问设备?

一个函数注册一个字符设备关联(将major:minor连接到您的函数),另一个函数只是创建一个抽象设备对象(仅),可以这么说。这两者是互补的。设备对象用于生成事件,如果注册了cdev关联,则udev可以在/dev中创建节点。(例如与drivers/char/misc.c比较)

当你将一个设备注册为字符设备时,会发生以下事情:

主号码按此填写。如果你使用任何设备的功能,其注册是基于字符设备(如tty,输入等),那么这些将有他们各自的主号码。

某些文件操作对应于只能在char设备上执行的操作。

在创建字符设备驱动程序并希望为用户空间程序提供字符设备接口时使用register_chrdev。当您使用register_chrdev注册字符设备时,您实际上是在通知内核,您的模块提供了一个字符设备接口,用户空间程序可以使用标准的I/O系统调用(如open、read、write和close)与该接口进行交互。

当你在一个涉及到各种类型设备的子系统或模块上工作,并且你想在内核的设备框架内管理它们时,使用device_register。Device_register用于向内核的设备框架注册平台或其他类型的设备。该框架为管理设备对象提供了一个抽象层,其中可以包括各种类型的设备,如平台设备、虚拟设备等。假设您正在开发一个需要管理特定硬件平台的子系统。

#include <linux/module.h>
#include <linux/device.h>
MODULE_LICENSE("GPL");
static struct platform_device my_platform_device = {
    .name = "my_platform_device",
    .id = -1,
};
static int __init my_subsystem_init(void) {
    int ret = platform_device_register(&my_platform_device);
    if (ret < 0) {
        pr_err("my_subsystem: Platform device registration failedn");
        return ret;
    }
    pr_info("my_subsystem: Platform device registered successfullyn");
    return 0;
}
static void __exit my_subsystem_exit(void) {
    platform_device_unregister(&my_platform_device);
    pr_info("my_subsystem: Platform device unregisteredn");
}
    enter code here
module_init(my_subsystem_init);
module_exit(my_subsystem_exit);

最新更新