当前位置:网站首页>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
边栏推荐
- Stack Title: the longest absolute path of a file
- AMD64 (x86_64) architecture ABI document:
- [detailed explanation of key and difficult points of document operation]
- Usage of '...' in golang
- Teach you to rely on management hand in hand
- 规范自己debug的流程
- Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?
- After clicking play, the variables in editorwindow will be destroyed inexplicably
- The source of everything, the choice of code branching strategy
- Masscode is an excellent open source code fragment manager
猜你喜欢

Arthas' dynamic load class (retransform)

Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation

Detailed explanation of extended physics informedneural networks paper

ES6高级-利用构造函数继承父类属性

ES6 advanced - inherit parent class attributes with constructors

The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track

Multithreaded programming
![[sql] case expression](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[sql] case expression

Autojs cloud control source code + display

(pc+wap) dream weaving template vegetable and fruit websites
随机推荐
STM32——DMA笔记
软件测试岗:阿里三面,幸好做足了准备,已拿offer
如何有效的去防止别人穿该网站首页快照
[translation] cloud like internal load balancer for kubernetes?
[SQL] CASE表达式
Get hours, minutes and seconds
简单使用 MySQL 索引
Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation
Vofa+ serial port debugging assistant
Be highly vigilant! Weaponization of smartphone location data on the battlefield
High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
GoLang 抽奖系统 设计
Basics - network and server
Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
Simply use MySQL index
Win11更改磁盘驱动器号的方法
My friend took 25koffer as soon as he learned automation test. When will my function test end?
Win11麦克风权限的开启方法
富文本转化为普通文本
Usage of arguments.callee