【问题标题】:Linux - How to remove entries from /proc/devicesLinux - 如何从 /proc/devices 中删除条目
【发布时间】:2013-06-21 20:01:37
【问题描述】:

我尝试编写一个简单的字符设备驱动程序,现在,即使我调用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 来自/dev 的任何chardev。但他们还在那里,卡在/proc/devices

【问题讨论】:

    标签: linux module kernel driver


    【解决方案1】:

    你可以做这样的事情。这工作正常。头文件被省略,所有文件操作都在这里实现。

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

    【讨论】:

      【解决方案2】:

      在拨打unregister_chrdev_region 时,请确保您拥有正确的设备主号码。我有一个类似的问题,我用同名的局部范围变量覆盖了我的全局 dev_major 变量,导致我将 0 传递给 unregister_chrdev_region

      【讨论】:

      • 抱歉迟到了,我已经远离linux内核的东西几十年了,甚至不记得我写这个问题时的场景。只是认为需要一个答案,而您的答案可能是最接近的。谢谢你们的时间,伙计们。
      • 我一直在打电话给unregister_chrdev_region,但它仍然没有被删除!
      【解决方案3】:

      我在编写初始 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 的字符设备。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        相关资源
        最近更新 更多