我现在尝试编写一个简单的字符设备驱动程序,即使我打电话unregister_chrdev_region
我仍然会在/proc/devices
中看到我的设备,如下所示:
248 chardev
249 chardev
250 chardev
现在我无法插入任何模块,每次我使用insmod
shell告诉我:
Error: could not insert module test.ko: Device or resource busy
我要求如何从/proc/devices
删除这些已注册的设备。我已使用rmmod
,rm
来自chardev
的任何/dev
。但他们仍然在那里,陷入/proc/devices
。
答案 0 :(得分:0)
你可以这样做。这很好用。省略了头文件,其中实现了所有文件操作。
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include "my_char_device.h"
MODULE_AUTHOR("alakesh");
MODULE_DESCRIPTION("Char Device");
static int r_init(void);
static void r_cleanup(void);
module_init(r_init);
module_exit(r_cleanup);
static struct cdev r_cdev;
static int r_init(void)
{
int ret=0;
dev_t dev;
dev = MKDEV(222,0);
if (register_chrdev_region(dev, 2, "alakesh")){
goto error;
}
cdev_init(&r_cdev, &my_fops);
ret = cdev_add(&r_cdev, dev, 2);
return 0;
error:
unregister_chrdev_region(dev, 2);
return 0;
}
static void r_cleanup(void)
{
cdev_del(&r_cdev);
unregister_chrdev_region(MKDEV(222,0),2);
return;
}
答案 1 :(得分:0)
调用unregister_chrdev_region
时,请确保您拥有正确的设备主号码。我有一个类似的问题,我用一个同名的本地范围变量覆盖我的全局dev_major
变量,导致我将0传递给unregister_chrdev_region
。
答案 2 :(得分:0)
当我编写初始char衍生程序时,我也遇到了类似的问题。问题是由于函数unregister_chrdev_region(dev_t div_major,unsigned count)中传递了无效的主号码。
我在退出例程中添加了一段代码,以删除未从/ proc / devices中删除的设备文件。
lets say these are the devices we need to remove.
248 chardev
249 chardev
250 chardev
static void r_cleanup(void)
{
cdev_del(&r_cdev);
unregister_chrdev_region(MKDEV(222,0),2);
//My code changes.
unregister_chrdev_region(MKDEV(248,0), 1);
unregister_chrdev_region(MKDEV(249,0), 1);
unregister_chrdev_region(MKDEV(250,0), 1);
return;
}
退出例程中的上述参考代码更改将删除主设备号为248,249和250的字符设备。