“/ dev / ****没有这样的设备”即使有效的charachter设备也会出错

时间:2017-03-31 23:07:23

标签: linux linux-kernel linux-device-driver embedded-linux

我正在drivers/char/new_driver.c编写内核驱动程序。 这个新驱动程序通过调用/dev/device1 api来注册两个新设备/dev/device2misc_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
};

当我加载模块时,我看到在/dev/device1/dev/device2

中正确创建了2个设备

但是,当我尝试在此设备上执行 write/read 操作时,会出现错误消息,指出“没有此类设备”

知道导致此类错误的原因吗?驱动程序代码中缺少什么?

2 个答案:

答案 0 :(得分:0)

我仍然认为这是许可问题。我附加了示例代码(没有任何实际实现)骨架。请看一下。

HashMap

然后编译驱动程序:

#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");

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

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

答案 1 :(得分:0)

对于仍然有此问题的任何人,我设法通过将我的次要数字从疯狂的高数字(999)更改为更合理的数字(71)来解决此问题。这是我生成的miscdevice结构:

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