当前位置:网站首页>Hello World driver (II) - primary version
Hello World driver (II) - primary version
2022-07-26 03:01:00 【Fried meat with eggs】
hello world drive ( One ) About a simple hello world The implementation of the driver , This time, the implementation and application of file operation interface are added on the original basis .(2022/07/23 21:27)
It's a fantastic dream , Can't resist silly persistence !
makefile unchanged ,hello demo New file operation interface , as follows
test demo
#include <linux/init.h> // This header file contains the functions for initializing and clearing your module
#include <linux/module.h> // This header file contains many definitions of symbols and functions , These symbols and functions are mostly related to loading modules
#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 You can save your private data in private_data Pointer , So in read_write When , You can also access the corresponding data through this pointer
// Sometimes it is necessary to identify the secondary equipment number
// 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 is a macro , It's automatically created when the module is compiled __this_module Variable */
.open = hello_open,
.read = hello_read,
.write = hello_write,
};
// Set the initialization entry function
static int __init hello_init(void)
{
register_chrdev(4, MODULE_NAME, &hello_fops);/ The parameters are the main equipment number 、 Equipment name 、 Structure /
return 0;
}
// Set exit function
static void __exit hello_exit(void)
{
unregister_chrdev(4, MODULE_NAME);/ The parameters are the main equipment number 、 Equipment name 、 Structure /
}
// Appoint license edition If not specified
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“BGM”);
MODULE_DESCRIPTION(“BGM str driver”);
// The above defined init() and exit() Function is defined as module entry / Exit function
module_init(hello_init);
module_exit(hello_exit);
Device node generation method :
Above Demo You need to manually generate device nodes , The generation method is as follows :
mknod /dev/bgm c 4 0 // among bgm Is the node name 4 Main equipment number
After generating the device node , We can go through cat /dev/bgm or echo Commands operate on nodes . You can think about it before you operate ,cat and echo What actions will be performed ?
APP test
After the node is generated , Our usual goal is to provide nodes to user mode programs for operation , Here you can put the following demo Used to control the above character device node , The character device can be read / write / open Wait for the action
#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);// According to your own device name
if (fd < 0)
{
printf(“can’t open!\n”);
}
return 0;
}
Come here , Even if a character device is basically completed , Think about it here , Whether it can automatically generate equipment nodes through code ? How to achieve ?
Reference resources https://blog.csdn.net/zichuanning520/article/details/123962725 Summarize the practice
边栏推荐
- Method of manually cloning virtual machine in esxi6.7
- 基础知识-网络与服务器
- ES6 advanced - using prototype object inheritance methods
- [translation] safety. Value of sboms
- [C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
- Longest Substring Without Repeating Characters
- ES6 advanced - inherit parent class attributes with constructors
- GoLang 抽奖系统 设计
- File operation (I) -- File introduction and file opening and closing methods
- 信息系统项目管理师必背核心考点(五十)合同内容约定不明确规定
猜你喜欢

AMD64(x86_64)架构abi文档:中
![[steering wheel] how to transfer the start command and idea: VM parameters, command line parameters, system parameters, environment variable parameters, main method parameters](/img/97/159d7df5e2d11b129c400d61e3fde6.png)
[steering wheel] how to transfer the start command and idea: VM parameters, command line parameters, system parameters, environment variable parameters, main method parameters

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

JS get the time composition array of two time periods

ES6 advanced - using prototype object inheritance methods

AMD64 (x86_64) architecture ABI document: medium

图像识别(六)| 激活函数

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

STM32——PWM学习笔记

Binary search 33. search rotation sort array
随机推荐
How to speed up matrix multiplication
多线程编程
[C language] deeply understand integer lifting and arithmetic conversion
Safety margin of mass consumption
Difference between soft link and hard link
[SQL] CASE表达式
Jenkins' study notes are detailed
My friend took 25koffer as soon as he learned automation test. When will my function test end?
这种动态规划你见过吗——状态机动态规划之股票问题(上)
Zhimeng prompts you how to solve the problem of setting the field as linkage type
案例:使用keepalived+Haproxy搭建Web群集
Games101 review: rasterization
Masscode is an excellent open source code fragment manager
1.软件测试-----软件测试的基本概念
Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
[translation] announce Vites 13
【方向盘】启动命令和IDEA如何传递:VM参数、命令行参数、系统参数、环境变量参数、main方法参数
[reading notes] user portrait methodology and engineering solutions
MySQL tutorial: MySQL database learning classic (from getting started to mastering)
Multithreaded programming