当前位置:网站首页>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总结实践
边栏推荐
- 文件操作(一)——文件简介与文件的打开方式和关闭
- Neo4j import CSV data error: neo4j load CSV error: couldn't load the external resource
- Arthas download and startup
- Zhimeng prompts you how to solve the problem of setting the field as linkage type
- How to speed up matrix multiplication
- 图像识别(六)| 激活函数
- GAMES101复习:光栅化
- c语言分层理解(c语言函数)
- Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
- Detailed explanation of extended physics informedneural networks paper
猜你喜欢

【C进阶】深入探索数据的存储(深度剖析+典例解读)

如何根据登录测试的需求设计测试用例?

ES6高级-利用原型对象继承方法

FPGA_ Initial use process of vivado software_ Ultra detailed
![[early knowledge of activities] list of recent activities of livevideostack](/img/14/d2cdae45a18a5bba7ee1ffab903af2.jpg)
[early knowledge of activities] list of recent activities of livevideostack

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)

Binary search 33. search rotation sort array

(九)属性自省

How to effectively prevent others from wearing the homepage snapshot of the website

图像识别(六)| 激活函数
随机推荐
MySQL建Websites数据表
1.软件测试-----软件测试的基本概念
Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
获取时分秒
What if the test / development programmer gets old? Lingering cruel facts
Usage of fuser and lsof
(pc+wap) dream weaving template vegetable and fruit websites
assert _ Aligns
17. Reverse the linked list
Pinia的数据持久化插件 pinia-plugin-persist
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)
(九)属性自省
Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
The El table header merges the first four columns into one cell
Literature speed reading | in the face of danger, anxious people run faster?
C language -- program environment and preprocessing
Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
Turn on the LED
Application of shift distance and hypothesis
GAMES101复习:着色(Shading)、渲染管线