如何在为设备驱动程序加载内核模块后在/dev中自动创建设备? [英] How to create a device in /dev automatically upon loading of the kernel module for a device driver?
问题描述
我正在尝试开发Linux设备驱动程序,并且第一次尝试是开发具有以下文件选项的char设备驱动程序,
I am attempting to develop Linux device drivers and as my first attempt I am trying to develop a char device driver that has the following file options,
struct file_operations fops{
.open=open_fun,
.release=release_fun,
.write=write_fun,
.read=read_fun,
};
当我使用insmod
加载驱动程序时,我看到/proc/devices
列出了char设备下的驱动程序,但是在/dev
中找不到它. Google搜索建议使用mknod
在/dev
中创建设备并将其与驾驶员的主要和次要相关联.但是,即使以超级用户身份执行此操作,也会导致权限被拒绝"错误.
When I load the driver using insmod
, I see that /proc/devices
lists the driver under char devices but I can't find it in /dev
. A Google search suggested use of mknod
to create a deivce in /dev
and associate it with the driver's major and minor. However, an attempt to do so resulted in "Permission denied" error even when done as a super user.
加载内核模块后,我应该怎么做才能使设备出现在/dev
中?我尝试了较旧(register_chrdev
)和较新版本(cdev_init & cdev_add
)的设备注册,但均无效果.
What should I do to make the device appear in /dev
when the kernel module is loaded? I tried both the older (register_chrdev
) and the newer version (cdev_init & cdev_add
) of registering the device but none of them works.
谢谢,
米尔
推荐答案
-
包括头文件 linux/device.h 和 linux/kdev_t.h
静态结构体类c_dev;
static struct class c_dev;
在驱动程序的__init函数内部添加以下API
- cl = class_create(THIS_MODULE,"x");
其中x-加载驱动程序时在/sys/class/中显示的名称.
where x - Name to be displayed inside /sys/class/ when driver is loaded.
- 使用device_create()内核api和device_create(cl,NULL,dev,NULL,"d");
其中d-要在/dev下创建的设备文件.
where d - device file to be created under /dev.
其中dev是在使用 alloc_chrdev_region API为驱动程序动态分配主号期间初始化的第一个设备号的变量
where dev is variable for the first device number that is initialized during the usage of alloc_chrdev_region API for dynamic allocation of major number for the driver
有关更多参考,请通过链接 http://opensourceforu .com/2011/04/character-device-files-creation-operations/
For Further reference please go through the link http://opensourceforu.com/2011/04/character-device-files-creation-operations/
这篇关于如何在为设备驱动程序加载内核模块后在/dev中自动创建设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!