即使使用有效的字符设备也会出现"/dev/**** No such device"错误



我正在用drivers/char/new_driver.c编写内核驱动程序。此新驱动程序通过调用 API 注册两个新设备/dev/device1/dev/device2 misc_register()

error = misc_register(&device1);
error = misc_register(&device2);
static struct miscdevice device1 = {
    MISC_DYNAMIC_MINOR,
    "device1",
    &device1_fops
};
static struct miscdevice device2 = {
    MISC_DYNAMIC_MINOR,
    "device2",
    &device2_fops
};

当我加载模块时,我看到 2 两个设备在 /dev/device1/dev/device2 中正确创建

但是当我尝试在此设备上write/read操作时,它会给出错误,说"没有这样的设备"。

知道是什么原因导致这种类型的错误吗? 驱动程序代码中缺少任何内容?

我仍然认为这是权限问题。我正在附加示例代码(没有任何实际实现(骨架。请看一看。

#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int sample_open(struct inode *inode, struct file *file)
{
    pr_info("Maverick: %s func :n",__func__);
    return 0;
}
static int sample_close(struct inode *inodep, struct file *filp)
{
    pr_info("Maverick: %s func :n",__func__);
    return 0;
}
static ssize_t sample_write(struct file *file, const char __user *buf,
                      size_t len, loff_t *ppos)
{
    pr_info("Maverick: %s func :n",__func__);
    return len; /* Not doing anything with the data */
}
static ssize_t sample_read(struct file *file, char __user *buf,
                      size_t len, loff_t *ppos)
{
    pr_info("Maverick: %s func :n",__func__);
    return len; /* Not do anything with the data */
}
static const struct file_operations sample_fops = {
    .owner   = THIS_MODULE,
    .write   = sample_write,
    .read    = sample_read,
    .open    = sample_open,
    .release = sample_close,
};
struct miscdevice sample_device = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = "device1",
    .fops = &sample_fops,
};
static int __init misc_init(void)
{
    int error;
    error = misc_register(&sample_device);
    if (error) {
        pr_err("can't misc_register :(n");
        return error;
    }
    pr_info("Maverick: %s func :n",__func__);
    return 0;
}
static void __exit misc_exit(void)
{
    misc_deregister(&sample_device);
    pr_info("Maverick: %s func :n",__func__);
}
module_init(misc_init)
module_exit(misc_exit)
MODULE_DESCRIPTION("Sample Misc Driver");
MODULE_AUTHOR("Vinod Maverick <vinodmaverickr007@gmail.com>");
MODULE_LICENSE("GPL");

然后编译驱动程序:

make
sudo insmod misc_sample.ko
ilab@SSID-iLBPG3:~/vinod/ldd$ echo "hello" > /dev/device1
-bash: /dev/device1: Permission denied
ilab@SSID-iLBPG3:~/vinod/ldd$ sudo su
root@SSID-iLBPG3:/home/ilab/vinod/ldd# echo "hello" > /dev/device1

但是我在读/写回调中没有任何代码,但您仍然可以看到 dmesg 而没有任何错误

root@SSID-iLBPG3:/home/ilab/vinod/ldd# dmesg
[2903599.416005] Maverick: misc_init func :
[2903623.966281] Maverick: sample_open func :
[2903623.966292] Maverick: sample_write func :
[2903623.966295] Maverick: sample_close func :
root@SSID-iLBPG3:/home/ilab/vinod/ldd#

对于仍然有此问题的任何人,我设法通过将我的次要号码从疯狂的高数字 (999( 更改为更合理的数字 (71( 来修复它。这是我生成的杂项设备结构:

static struct miscdevice chrdev = {
    .minor = 71,
    .name = "tracefunc",
    .fops = &chrdev_fops,
    .mode = S_IRUGO,
};

相关内容

最新更新