您尚未登录。

楼主 # 2022-07-04 22:56:27

linux-xc
会员
注册时间: 2022-06-28
已发帖子: 29
积分: 51

驱动模块加载到/dev无法进行读写操作

// devfs模板

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>


static unsigned char read_buf[50] = {"ABCD"};
static unsigned char write_buf[50] = {0};

static int usr_open(struct inode *inode, struct file *file)
{
    return 0;
}


static int usr_close(struct inode *inode, struct file *file)
{
    return 0;
}


static ssize_t usr_read(struct file *file, char __user *buf,
                        size_t count, loff_t *offset)
{
    int ret = copy_to_user(buf, read_buf, count);
    if (ret < 0)
    {
        return -1;
    }
    return 0;
}


static ssize_t usr_write(struct file *file, const char __user *buf,
                         size_t count, loff_t *offset)
{
    memset(write_buf, 0, sizeof(write_buf));        
    int ret = copy_from_user(write_buf, buf, count);
    if (ret < 0)
    {
        return -1;
    }
    printk("re_data:%s\r\n", write_buf);
    return 0;
}

#define module_major 200      
#define module_name "usr_dev"

/* 注册结构体 */
static const struct file_operations usr_fops = {

    .owner = THIS_MODULE, 
    .open = usr_open,     
    .release = usr_close, 
    .read = usr_read,    
    .write = usr_write,   
};


static int __init usr_init(void)
{
    printk("init\r\");
    register_chrdev(module_major, module_name, &usr_fops); //注册驱动模块
    return 0;
}


static void __exit usr_exit(void)
{
    printk("exit\r\");    
    unregister_chrdev(module_major, module_name); //卸载驱动模块
}

module_init(usr_init);
module_exit(usr_exit);


MODULE_LICENSE("GPL");                     
MODULE_AUTHOR("Author");                  
MODULE_DESCRIPTION("Module is introduced"); 
MODULE_ALIAS("The alias");               

代码如上,在荔枝派zero核心板上的shell操作:

insmod usr_dev.ko
mknod /dev/usr_dev c 200 0

已经确保 lsmod 和 cat /proc/devices 能够找到这个驱动模块
且 dmesg 也有打印 init 和 exit

但使用应用程序 open 打开字符设备后,read 和 write 均没有反应,这可能是什么原因导致的?

离线

楼主 #1 2022-07-04 23:14:13

linux-xc
会员
注册时间: 2022-06-28
已发帖子: 29
积分: 51

Re: 驱动模块加载到/dev无法进行读写操作

使用dmesg发现,使用echo写入内容后,会不断打印收到的数据,像是write不断被执行。

使用了两次 cat 打开,dmesg打印如下:

[131910.069992] ------------[ cut here ]------------
[131910.075935] WARNING: CPU: 0 PID: 27183 at ./include/linux/thread_info.h:134 usr_read+0xac/0xc8 [main]
[131910.087880] Buffer overflow detected (50 < 131072)!
[131910.094329] Modules linked in: main(O) mousedev [last unloaded: main]
[131910.102468] CPU: 0 PID: 27183 Comm: cat Tainted: G           O      5.2.0-licheepi-zero #4
[131910.112530] Hardware name: Allwinner sun8i Family
[131910.119097] [<c010ed14>] (unwind_backtrace) from [<c010b72c>] (show_stack+0x10/0x14)
[131910.128868] [<c010b72c>] (show_stack) from [<c0735810>] (dump_stack+0x84/0x98)
[131910.138215] [<c0735810>] (dump_stack) from [<c011db98>] (__warn+0xfc/0x114)
[131910.147355] [<c011db98>] (__warn) from [<c011dbf4>] (warn_slowpath_fmt+0x44/0x68)
[131910.157141] [<c011dbf4>] (warn_slowpath_fmt) from [<bf0061bc>] (usr_read+0xac/0xc8 [main])
[131910.167846] [<bf0061bc>] (usr_read [main]) from [<c0215d18>] (__vfs_read+0x2c/0x1c0)
[131910.178121] [<c0215d18>] (__vfs_read) from [<c0215f38>] (vfs_read+0x8c/0x118)
[131910.187884] [<c0215f38>] (vfs_read) from [<c0216230>] (ksys_read+0x58/0xd0)
[131910.197567] [<c0216230>] (ksys_read) from [<c0101000>] (ret_fast_syscall+0x0/0x54)
[131910.207956] Exception stack(0xc3175fa8 to 0xc3175ff0)
[131910.215903] 5fa0:                   00020000 00020000 00000003 b6e61000 00020000 000271c4
[131910.227167] 5fc0: 00020000 00020000 b6e61000 00000003 7fffe000 00000000 00000000 00020000
[131910.238513] 5fe0: 00000000 bee52b44 00013835 b6f0cea6
[131910.246969] ---[ end trace 8651e93d3c6eace7 ]---
[131910.254936] read ok
[131943.150461] ------------[ cut here ]------------
[131943.164150] WARNING: CPU: 0 PID: 27434 at ./include/linux/thread_info.h:134 usr_read+0xac/0xc8 [main]
[131943.180628] Buffer overflow detected (50 < 131072)!
[131943.189413] Modules linked in: main(O) mousedev [last unloaded: main]
[131943.199901] CPU: 0 PID: 27434 Comm: cat Tainted: G        W  O      5.2.0-licheepi-zero #4
[131943.212265] Hardware name: Allwinner sun8i Family
[131943.221124] [<c010ed14>] (unwind_backtrace) from [<c010b72c>] (show_stack+0x10/0x14)
[131943.233136] [<c010b72c>] (show_stack) from [<c0735810>] (dump_stack+0x84/0x98)
[131943.244569] [<c0735810>] (dump_stack) from [<c011db98>] (__warn+0xfc/0x114)
[131943.255633] [<c011db98>] (__warn) from [<c011dbf4>] (warn_slowpath_fmt+0x44/0x68)
[131943.267254] [<c011dbf4>] (warn_slowpath_fmt) from [<bf0061bc>] (usr_read+0xac/0xc8 [main])
[131943.279704] [<bf0061bc>] (usr_read [main]) from [<c0215d18>] (__vfs_read+0x2c/0x1c0)
[131943.291586] [<c0215d18>] (__vfs_read) from [<c0215f38>] (vfs_read+0x8c/0x118)
[131943.302852] [<c0215f38>] (vfs_read) from [<c0216230>] (ksys_read+0x58/0xd0)
[131943.313907] [<c0216230>] (ksys_read) from [<c0101000>] (ret_fast_syscall+0x0/0x54)
[131943.325563] Exception stack(0xc3845fa8 to 0xc3845ff0)
[131943.334711] 5fa0:                   00020000 00020000 00000003 b6eb6000 00020000 000271c4
[131943.347078] 5fc0: 00020000 00020000 b6eb6000 00000003 7fffe000 00000000 00000000 00020000
[131943.359400] 5fe0: 00000000 beaf3b44 00013835 b6f61ea6
[131943.368877] ---[ end trace 8651e93d3c6eace8 ]---

最近编辑记录 linux-xc (2022-07-04 23:16:52)

离线

楼主 #2 2022-07-05 01:12:24

linux-xc
会员
注册时间: 2022-06-28
已发帖子: 29
积分: 51

Re: 驱动模块加载到/dev无法进行读写操作

很不巧,又是自己解决了。

发现应用程序所访问的设备文件差了一个字母。。。。。

然后用read/write正常了

但是,使用 cat 和 echo 还是会有问题,我想应该是后面学到的东西才能解决。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn