当前位置:网站首页>hello world驱动(二)-初级版
hello world驱动(二)-初级版
2022-07-26 02:57:00 【鸡蛋炒肉】
hello world驱动(一)讲述了一个简单hello world驱动的实现,本次在原基础上新增文件操作接口的实现及应用。(2022/07/23 21:27)
再牛逼的梦想,也抵不住傻逼般的坚持!
makefile不变,hello demo新增文件操作接口,如下
test demo
#include <linux/init.h> //这个头文件包含了你的模块初始化与清除的函数
#include <linux/module.h> //这个头文件包含了许多符号与函数的定义,这些符号与函数多与加载模块有关
#include <linux/kernel.h>
#include <linux/fs.h>
#ifdef MODULE_NAME
#unfine MODULE_NAME
#endif
#define MODULE_NAME “bgm_str_dev”
static int hello_open(struct inode *inode, struct file *file)
{
printk(“hello open!\n”);
// open的时候可以将自己的私有数据保存在private_data指针中,那么在read_write的时候,也可以通过这个指针访问对应的数据
// 有时候还需要在这里面识别次设备号
// file->private_data = pdata;
return 0;
}
static ssize_t hello_read(struct file *filp, char __user * buf, size_t size, loff_t * off)
{
printk(“hello read!\n”);
return 0;
}
static ssize_t hello_write (struct file *filp, const char __user *buf, size_t size, loff_t * off)
{
printk(“hello write!\n”);
return 0;
}
static const struct file_operations hello_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = hello_open,
.read = hello_read,
.write = hello_write,
};
//设置初始化入口函数
static int __init hello_init(void)
{
register_chrdev(4, MODULE_NAME, &hello_fops);/参数依次为主设备号、设备名称、结构体/
return 0;
}
//设置出口函数
static void __exit hello_exit(void)
{
unregister_chrdev(4, MODULE_NAME);/参数依次为主设备号、设备名称、结构体/
}
//指定license版本 若不指定
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“BGM”);
MODULE_DESCRIPTION(“BGM str driver”);
//将上述定义的init()和exit()函数定义为模块入口/出口函数
module_init(hello_init);
module_exit(hello_exit);
设备节点生成方法:
如上Demo 需要手动生成设备节点,生成方法如下:
mknod /dev/bgm c 4 0 //其中bgm为节点名称 4 为主设备号
生成设备节点后,我们可以通过cat /dev/bgm 或 echo命令对节点进行操作。大家操作前可以思考下,cat 和 echo会分别执行哪些动作?
APP test
节点生成后, 我们通常的目标是将节点提供给用户态程序进行操作,此处可将如下demo用于控制上述字符设备节点,此处可对字符设备进行read / write / open等动作
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open(“/dev/bgm”, O_RDWR);//根据自己的设备名称
if (fd < 0)
{
printf(“can’t open!\n”);
}
return 0;
}
到这里,一个字符设备就算基本完成了,这里思考下,是否能够通过代码实现自动生成设备节点?如何实现?
参考https://blog.csdn.net/zichuanning520/article/details/123962725总结实践
边栏推荐
- Longest Substring Without Repeating Characters
- Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
- Arthas' dynamic load class (retransform)
- How about GF Securities? Is it safe to open an account online?
- Arthas view the source code of the loaded class (JAD)
- Is it safe to open galaxy securities account by mobile phone?
- Arthas download and startup
- Annotation development management third-party beans
- From the annual reports of major apps, we can see that user portraits - labels know you better than you do
- Pinia的数据持久化插件 pinia-plugin-persist
猜你喜欢

How to design automated test cases?

(九)属性自省

DFS Niuke maze problem

AMD64(x86_64)架构abi文档:

ES6 advanced - using prototype object inheritance methods

C language -- program environment and preprocessing

My friend took 25koffer as soon as he learned automation test. When will my function test end?

第3章业务功能开发(删除线索)

Information system project managers must recite the core examination site (50). The contract content is not clearly stipulated

What if the test / development programmer gets old? Lingering cruel facts
随机推荐
Binary search 33. search rotation sort array
Get hours, minutes and seconds
[translation] announce Vites 13
[SQL] 自连接的用法
Games101 review: shading, rendering pipelines
Usage of fuser and lsof
Neo4j import CSV data error: neo4j load CSV error: couldn't load the external resource
Win11隐藏输入法状态栏方法
High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
Nahamcon CTF 2022 babyrev reverse analysis
Vofa+ serial port debugging assistant
Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation
测试/开发程序员老了怎么办?挥之不去残酷的事实......
Image recognition (VII) | what is the pooling layer? What's the effect?
Arthas download and startup
MySQL build websites data table
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
Uni app cross domain configuration
Recorded target detection NMS (non maximum suppression)