上篇介紹了Linux驅動中sysfs接口的創(chuàng)建,今天介紹procfs接口的創(chuàng)建。
procfs
:可實現類似cat /proc/cpuinfo
的操作
procfs接口創(chuàng)建
實現效果:
例如, 在/proc
下創(chuàng)建一個clk節(jié)點,通過cat /proc/clk
可查看內容:
代碼實現:
系統(tǒng) | 內核版本 |
---|---|
Linux | 4.9.88 |
在驅動中添加以下代碼:
#include
#include
#include
#include
#include
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");