【问题标题】:No such device or address - Linux device driver development没有这样的设备或地址——Linux设备驱动开发
【发布时间】:2017-10-25 04:49:29
【问题描述】:

当我尝试cat /dev/gpio-reflect 时,我收到错误: No such device or address

cat proc/devices 列出了我的驱动程序以及正确的主设备号。

dmesg 打印来自 init 和 exit 函数的日志。

我也在/dev下创建了对应的文件(major是正确的)。

cdev_addalloc_chrdev_region 不返回错误代码。

我不知道我做错了什么。请帮我。

static struct file_operations fops = {
.owner = THIS_MODULE,
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};


static int __init gpio_reflect_init(void)
{

int err,res=alloc_chrdev_region(&dev, 0, 1, dname);
classptr = class_create(THIS_MODULE, "socledclass");
device_create(classptr, NULL, MAJOR(dev), NULL, dname);
cdev_init(&c_dev, &fops);
c_dev.ops=&fops;
c_dev.owner=THIS_MODULE;
err=cdev_add(&c_dev, MAJOR(dev), 1);

if(err){
    printk(KERN_INFO "Could not add device");
}

if(res<0){
    printk(KERN_INFO "Could not alloc device");
    return res;
}else{
    printk(KERN_INFO "Major: %i",MAJOR(dev));
}
printk(KERN_INFO "Input Pin is: %i\n",in);
printk(KERN_INFO "Output Pin is: %i\n",out);


return 0;
}

static void __exit gpio_reflect_cleanup(void)
{
cdev_del(&c_dev);
unregister_chrdev_region(dev,1);
printk(KERN_INFO "%s unloaded\n",dname);

}

static int device_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "open\n");
if (Device_Open)
    return -EBUSY;

Device_Open++;
sprintf(msg, "Hello\n");
msg_Ptr = msg;
try_module_get(THIS_MODULE);

return SUCCESS;
}

static int device_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "release\n");
Device_Open--;

module_put(THIS_MODULE);

return SUCCESS;
}

 static ssize_t device_read(struct file *filp, char *buffer,size_t length,  loff_t * offset){
int bytes_read = 0;
printk(KERN_INFO "read\n");

if (*msg_Ptr == 0)
    return 0;
while (length && *msg_Ptr) {
    put_user(*(msg_Ptr++), buffer++);

    length--;
    bytes_read++;
}
return bytes_read;
}


static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t *       off)
{
printk(KERN_INFO "write\n");
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}

module_init(gpio_reflect_init);
module_exit(gpio_reflect_cleanup);

抱歉,代码格式错误:)

【问题讨论】:

  • 我想最好的方法是阅读代码示例。也许像 LDD3 这样的书会对你有所帮助。

标签: linux-device-driver kernel-module


【解决方案1】:

如果有人遇到同样的问题。 这只是一个愚蠢的错误。

错误:cdev_add(&amp;c_dev, MAJOR(dev), 1);

右:cdev_add(&amp;c_dev, dev, 1);

【讨论】:

    【解决方案2】:

    如果你/dev/下的设备节点是/dev/gpio-reflect试试cat /dev/gpio-reflect

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 2021-11-14
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多