Linux驱动中创建procfs接口的方法

嵌入式技术

1337人已加入

描述

上篇介绍了Linux驱动中sysfs接口的创建,今天介绍procfs接口的创建。

procfs:可实现类似cat /proc/cpuinfo的操作

procfs接口创建

实现效果:

例如, 在/proc下创建一个clk节点,通过cat /proc/clk可查看内容:

Linux

代码实现:

系统 内核版本
Linux 4.9.88

在驱动中添加以下代码:

#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");

procfs接口的创建,主要是实现struct file_operations结构体,然后通过proc_create函数进行注册,通过proc_remove函数进行注销。

procfs通常是用来获取CPU、内存、进程等各种信息,例如cat /proc/cpuinfocat /proc/meminfo,所以我们只需要实现.open成员函数。 当使用cat命令查看/proc下的信息时,会调用到.open对应的实现函数。

这里我们使用了seq_file接口,需要记住的是, procfs通常会和seq_file接口一起使用 。 seq_file是一个序列文件接口, 当我们创建的proc数据内容由一系列数据顺序组合而成或者是比较大的proc文件系统时,都建议使用seq_file接口 ,例如cat /proc/meminfo就会显示很多内容。

seq_file接口主要就是解决proc接口编程存在的问题, 推荐在proc接口编程时使用seq_file接口 ,另外.read、.llseek、.release成员函数也可以直接用seq_readseq_lseekseq_release

proc新接口

注意,在较新版本的内核中,procfs的函数接口有所变化。

系统 内核版本
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");

新的proc接口中,将原来的struct file_operations换成了struct proc_ops,其中成员函数也添加了对应的前缀proc,但本质还是一样的,只是换了名字,更加规范了一些。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分