Linux-如何从/proc/devices中删除条目

Linux-如何从/proc/devices中删除条目,linux,module,kernel,driver,Linux,Module,Kernel,Driver,我尝试编写一个简单的字符设备驱动程序,现在,即使我调用了unregister\u chrdev\u region,我仍然可以在/proc/devices中看到我的设备,如下所示: 248 chardev 249 chardev 250 chardev 现在我无法插入任何模块,每次我使用insmodshell都会告诉我: Error: could not insert module test.ko: Device or resource busy 我在问如何从/proc/devices中删除这

我尝试编写一个简单的字符设备驱动程序,现在,即使我调用了
unregister\u chrdev\u 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
,并且已经从
/dev
使用了
rm
any
chardev
。但它们仍然存在,卡在
/proc/devices
中,您可以这样做。这个很好用。头文件是ommitted,在这里实现了所有文件操作

#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;
}
#包括
#包括
#包括
#包括“my_char_device.h”
模块作者(“alakesh”);
模块描述(“字符设备”);
静态int r_init(void);
静态空隙r_清理(空隙);
模块_init(r_init);
模块_退出(r_清理);
静态结构cdev ru cdev;
静态int r_init(void)
{
int-ret=0;
发展与发展;
dev=MKDEV(222,0);
if(登记chrdev地区(dev,2,“alakesh”)){
转到错误;
}
cdev_init(&r_cdev,&my_fops);
ret=cdev\U add(&r\U cdev,dev,2);
返回0;
错误:
注销chrdev_地区的注册(dev,2);
返回0;
}
静态空心r_清理(空心)
{
cdev_del(&r_cdev);
取消注册chrdev_地区(MKDEV(222,0),2);
返回;
}

在调用
注销chrdev\u区域时,请确保您拥有正确的设备主要号码。我遇到了一个类似的问题,我用一个同名的局部作用域变量覆盖了我的全局
dev_major
变量,导致我将0传递到
unregister_chrdev_region
我在编写初始字符派生程序时也遇到了类似的问题。问题是由于在函数unregister_chrdev_区域(dev_t div_major,unsigned count)中传递了无效的主数字

我在exit例程中添加了一段代码,以删除未从/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的char设备。

很抱歉,我已经远离linux内核几十年了,甚至记不起我写这个问题时的场景。只要想一个答案是必要的,你的答案也许是最接近的。谢谢你们的时间,伙计们。我一直在打电话给
注销chrdev\u地区
,但它仍然没有被删除!