linux驱动学习加强版-3 (驱动代码测试,以及代码完善)
创始人
2025-05-31 18:17:47
0

文章目录

  • 一、用户测试代码
  • 二、驱动功能完善。
  • 三、open函数的特异性
  • 四、代码中的注意事项

一、用户测试代码

#include 
#include 
#include 
#include 
#include 
#include int main(int argc,char **argv)
{   if(argc <2){printf("Usage:%s -w \n",argv[0]);printf("Usage:%s -r \n",argv[0]);return -1;}int fd;char buf[1024];fd = open("/dev/filectl",O_RDWR);if(fd < 0){printf("can not open /dev/filectl\n");return -1;}if(strcmp(argv[1],"-w")==0 && argc==3){write(fd, argv[2], strlen(argv[2]));}else{read(fd,buf,1024);buf[1023] = '\n';printf("APP get data %s\n",buf);}close(fd);return  0;
}

这样一个简单的用户demo就写完了,然后让我们来试试看结果吧;
使用 gcc -o file_ctl file_test.c 将测试的demo编译为一个可执行文件。
在这里插入图片描述
执行完后是乱码,我们看下log呢

在这里插入图片描述
发现Log是调用了的,为啥呢 。
因为我们驱动之提供了接口,但没有提供功能。
在这里插入图片描述

我们驱动没有提供功能,你们怎么使用呢。哈哈!所以接下来我们需要的是完善驱动功能

二、驱动功能完善。

这个demo的功能就是我调用对应的函数,将我要写的文件传递到底层,然后再把数据读取出来,返回给上层。
这个时候我们就需要内核的标准的函数

copy_from_user  // 从用户空间拷贝文件到驱动
copy_to_user // 从驱动拷贝文件到用户层
#include 
#include 
#include 
#include 
#include 
#include 
#include static int major = 0; //确定主设备号,0默认系统分配
static struct class *filectl_class;
static char kbuffer[1024]; //内核空间的bufferssize_t filectl_read(struct file *file, char __user *buf, size_t size, loff_t *pops)
{int ret;printk("[%s %d]\n", __FUNCTION__, __LINE__);ret = copy_to_user(buf, kbuffer, size);if(ret < 0) {printk("[%s %d]copy_to_user error \n ",__FUNCTION__, __LINE__ );}return size;
}ssize_t filectl_write(struct file *file,const char __user *buf, size_t size, loff_t *pops)
{int ret;printk("[%s %d]\n", __FUNCTION__, __LINE__);ret = copy_from_user(kbuffer, buf, size);if(ret < 0) {printk("[%s %d]copy_from_user error \n ",__FUNCTION__, __LINE__ );}return size;
}int filectl_open(struct inode *inode, struct file *file)
{printk("[%s %d]\n", __FUNCTION__, __LINE__);return 0;
}int filectl_close(struct inode *inode, struct file *file)
{printk("[%s %d]\n", __FUNCTION__, __LINE__);return 0;
}// 定义自己的驱动的 file_operations
static struct file_operations filectl_ops = {.owner	 = THIS_MODULE,.read    = filectl_read,.write   = filectl_write,.open    = filectl_open,.release = filectl_close, // 好像没有close这个函数
};static int __init filectl_init(void)
{// 在初始化的时候进行驱动注册,设备号major = register_chrdev(0,"filectl",&filectl_ops);if(major < 0) {printk("[%s %d] filectl error\n", __FUNCTION__, __LINE__); // 注册失败return -1;}filectl_class = class_create(THIS_MODULE, "filectl"); // class_create 动态创建dev的类// IS_ERR 查看指针是否有错误if(IS_ERR(filectl_class)) {printk("[%s %d] class_create error\n", __FUNCTION__, __LINE__);unregister_chrdev(major,"filectl");return -1;}// 创建字符设备device_create(filectl_class, NULL, MKDEV(major, 0),NULL, "filectl");printk("[%s %d] filectl driver create success\n", __FUNCTION__, __LINE__);return 0;
}static void __exit filectl_exit(void) {device_destroy(filectl_class, MKDEV(major, 0));class_destroy(filectl_class);// 注销字符设备unregister_chrdev(major,"filectl");printk("[%s %d]goodbye filectl driver\n",  __FUNCTION__, __LINE__);
}module_init(filectl_init);
module_exit(filectl_exit);
MODULE_LICENSE		("GPL");
MODULE_AUTHOR		("cong.luo");
MODULE_DESCRIPTION	("First file contrl module");

再来测试一把呢:
在这里插入图片描述
在这里插入图片描述

这样就OK了哇。

三、open函数的特异性

下面我们修改下代码:
首先是驱动文件,我们去掉read、write、open

#include 
#include 
#include 
#include 
#include 
#include 
#include static int major = 0; //确定主设备号,0默认系统分配
static struct class *filectl_class;
static char kbuffer[1024]; //内核空间的buffer// ssize_t filectl_read(struct file *file, char __user *buf, size_t size, loff_t *pops)
// {
//     int ret;
// 	printk("[%s %d]\n", __FUNCTION__, __LINE__);
//     ret = copy_to_user(buf, kbuffer, size);
//     if(ret < 0) {
//         printk("[%s %d]copy_to_user error \n ",__FUNCTION__, __LINE__ );
//     }
//     return size;
// }// ssize_t filectl_write(struct file *file,const char __user *buf, size_t size, loff_t *pops)
// {
//     int ret;
// 	printk("[%s %d]\n", __FUNCTION__, __LINE__);
//     ret = copy_from_user(kbuffer, buf, size);
//     if(ret < 0) {
//         printk("[%s %d]copy_from_user error \n ",__FUNCTION__, __LINE__ );
//     }
//     return size;
// }// int filectl_open(struct inode *inode, struct file *file)
// {
// 	printk("[%s %d]\n", __FUNCTION__, __LINE__);
//     return 0;
// }int filectl_close(struct inode *inode, struct file *file)
{printk("[%s %d]\n", __FUNCTION__, __LINE__);return 0;
}// 定义自己的驱动的 file_operations
static struct file_operations filectl_ops = {.owner	 = THIS_MODULE,// .read    = filectl_read,// .write   = filectl_write,//.open    = filectl_open,.release = filectl_close, // 好像没有close这个函数
};static int __init filectl_init(void)
{// 在初始化的时候进行驱动注册,设备号major = register_chrdev(0,"filectl",&filectl_ops);if(major < 0) {printk("[%s %d] filectl error\n", __FUNCTION__, __LINE__); // 注册失败return -1;}filectl_class = class_create(THIS_MODULE, "filectl"); // class_create 动态创建dev的类// IS_ERR 查看指针是否有错误if(IS_ERR(filectl_class)) {printk("[%s %d] class_create error\n", __FUNCTION__, __LINE__);unregister_chrdev(major,"filectl");return -1;}// 创建字符设备device_create(filectl_class, NULL, MKDEV(major, 0),NULL, "filectl");printk("[%s %d] filectl driver create success\n", __FUNCTION__, __LINE__);return 0;
}static void __exit filectl_exit(void) {device_destroy(filectl_class, MKDEV(major, 0));class_destroy(filectl_class);// 注销字符设备unregister_chrdev(major,"filectl");printk("[%s %d]goodbye filectl driver\n",  __FUNCTION__, __LINE__);
}module_init(filectl_init);
module_exit(filectl_exit);
MODULE_LICENSE		("GPL");
MODULE_AUTHOR		("cong.luo");
MODULE_DESCRIPTION	("First file contrl module");

在app代码中添加一些打印:

#include 
#include 
#include 
#include 
#include 
#include int main(int argc,char **argv)
{   if(argc <2){printf("Usage:%s -w \n",argv[0]);printf("Usage:%s -r \n",argv[0]);return -1;}int fd;char buf[1024];fd = open("/dev/filectl",O_RDWR);if(fd < 0){printf("can not open /dev/filectl\n");return -1;}printf("open /dev/filectl success \n");if(strcmp(argv[1],"-w")==0 && argc==3){int err = write(fd, argv[2], strlen(argv[2]));if (err < 0 ) {printf("write /dev/filectl error \n");return -1;}printf("write /dev/filectl success \n");}else{int err = read(fd,buf,1024);if (err < 0 ) {printf("read /dev/filectl error \n");return -1;}printf("read /dev/filectl success \n");buf[1023] = '\n';printf("APP get data %s\n",buf);}close(fd);return  0;
}

在这里插入图片描述没有read和write函数,我们无法读写。
但是我们可以进行open,这说明了open函数是一个比较特殊的函数,或许硬件默认就是可以进行open的,是为了方便一些初始化吧。大家记住就好。

四、代码中的注意事项

copy_to_user 和 copy_from_user传递的参数位置是不一样的哟

ret = copy_to_user(buf, kbuffer, size);
ret = copy_from_user(kbuffer, buf, size);

有些小伙伴可能没有注意到这点。

相关内容

热门资讯

亿利达:诉讼事项进展 金融界4月23日消息,亿利达公告称,公司于近日收到安徽省合肥市包河区人民法院送达的(2022)皖 0...
菲媒:菲律宾副总统称,不优先考... 【环球网报道】综合菲律宾《马尼拉标准报》等媒体6月1日报道,菲律宾副总统莎拉·杜特尔特称,她不优先考...
原创 美... 特朗普再次执掌白宫后,他的“地盘扩张梦”可谓是雷声大雨点小,搞得沸沸扬扬却未见实效。他本想一口气吞掉...
法网-郑钦文鏖战2-1萨姆索诺... 北京时间6月1日,2025赛季网球大满贯法国公开赛继续进行,在女单第三轮的一场比赛中,赛会8号种子、...
以国防军:黎以停火以来超180... △黎巴嫩南部地区(资料图) 以色列国防军当地时间6月1日下午发布消息称,当天上午,一名黎巴嫩真主党特...
深夜,巨子生物突发声明:接受检... 每经编辑|金冥羽 巨子生物旗下重组胶原蛋白品牌可复美产品成分争议持续发酵。 6月1日22点32分,...
新修订的《快递暂行条例》6月1... 6月1日起,《国务院关于修改〈快递暂行条例〉的决定》正式施行。此次修改,专门增加了“快递包装”章节,...
开放“以债换房”政策,可直接置... “南京网络辟谣”微信公众号6月1日发文称,近日,有“南京二手房零首付李经理”“合肥瑶珺房地产代理有限...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...