##System | Kernel version |
Linux | 4.9.88 |
Add the following code in the driver:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
struct proc_dir_entry *my_proc_entry;
static int proc_clk_show(struct seq_file *m, void *v)
{
//cat显示的内容
seq_printf(m,
"pll0: %u Mhz\n"
"pll1: %u Mhz\n"
"pll2: %u Mhz\n",
100, 200, 300);
return 0;
}
static int clk_info_open(struct inode *inode, struct file *filp)
{
return single_open(filp, proc_clk_show, NULL);
}
static struct file_operations myops =
{
.owner = THIS_MODULE,
.open = clk_info_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init my_module_init(void)
{
//注册proc接口
my_proc_entry = proc_create("clk", 0644, NULL, &myops);
return 0;
}
static void __exit my_module_exit(void)
{
//注销proc接口
proc_remove(my_proc_entry);
}
module_init(my_module_init);
module_exit(my_module_exit);
MODULE_LICENSE("GPL");
Copy after login
The creation of the procfs interface mainly implements the struct file_operations
structure, and then proceeds through the proc_create
function Register and unregister through the proc_remove
function.
procfs is usually used to obtain various information such as CPU, memory, process, etc., such as cat /proc/cpuinfo
, cat /proc/meminfo
, so we only Need to implement the .open member function. When using the cat
command to view the information under /proc
, the corresponding implementation function of .open
will be called.
Here we use the seq_file
interface. What needs to be remembered is that procfs is usually used together with the seq_file interface. seq_file is a sequence file interface. When the proc data content we create is composed of a series of data sequences or it is a relatively large proc file system, it is recommended to use the seq_file interface , such as cat / proc/meminfo
will display a lot of content.
The seq_file interface mainly solves the problems existing in proc interface programming. It is recommended to use the seq_file interface when programming the proc interface. In addition, the .read, .llseek, and .release member functions can also be used directly seq_read
, seq_lseek
and seq_release
.
proc new interface
Note that in newer versions of the kernel, procfs
The function interface has changed.
System | Kernel version |
---|
Linux | 5.10.111 |
在驱动中添加以下代码:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
struct proc_dir_entry *my_proc_entry;
static int proc_clk_show(struct seq_file *m, void *v)
{
seq_printf(m,
"pll0: %lu Mhz\n"
"pll1: %lu Mhz\n"
"pll2: %lu Mhz\n",
100, 200, 300);
return 0;
}
static int clk_info_open(struct inode *inode, struct file *filp)
{
return single_open(filp, proc_clk_show, NULL);
}
static const struct proc_ops clk_stat_proc_fops = {
.proc_open = clk_info_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = seq_release,
};
static int __init my_module_init(void)
{
my_proc_entry = proc_create("clk", 0, NULL, &clk_stat_proc_fops);
return 0;
}
static void __exit my_module_exit(void)
{
proc_remove(my_proc_entry);
}
module_init(my_module_init);
module_exit(my_module_exit);
MODULE_LICENSE("GPL");
Copy after login
新的proc
接口中,将原来的struct file_operations
换成了struct proc_ops
,其中成员函数也添加了对应的前缀proc
,但本质还是一样的,只是换了名字,更加规范了一些。