驱动中/proc文件开发

       在linux驱动开发过程中,经常会遇到在/proc或者/tmpfs下增加文件的情况,/tmpfs下的文件通常都是对采集时间有要求,因此采集完成后保存在相关的文件中,系统需要时进行获取。对应的数据通常也不是实时的,除非利用定时器或者信号配合先采集后进行获取。对于/tmpfs下的文件,可使用”ls -lh”查看出文件的实际大小。这说明文件实际是被写入了内容的,通常/tmpfs文件会在应用层进行读写操作。

       对应地/proc文件一般只是类似一个壳,实际上并不是把内容写入文件中,使用“ls -lh”查看大小通常为0。/proc文件是在获取时(执行cat)实时采集并显示出来,结果是实时的并且不占空间,跟/tmpfs相比,/proc文件保存在内核内存中, 因为读取性能及对系统影响较小。
       开发的步骤大致为:

    1、驱动加载时,利用proc_mkdir创建/proc文件路径。利用proc_create创建文件。

    2、实现文件对应的file_operations结构,将相关处理函数注册到Linux内核。

    3、file_operations中相关函数接口的实现,一般情况下需实现.open(对应cat操作)和.write(对应echo操作)函数。

    4、驱动卸载时,利用remove_proc_entry删除文件路径和文件。

       下面通过一个例子来展示/proc文件的开发过程:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

struct proc_dir_entry *proc_ctcwifi_dir = NULL;
struct proc_dir_entry *ctcwifi_diag_enable = NULL;
unsigned char g_ctcwifiDiagEnable;

/*Interface implementation*/
ssize_t start_ctcwifiDiagEnable_write(struct file *fp,const char __user *buffer,
    size_t count,loff_t *data)
{
    int i = 0;
    unsigned char tmp = 0;

    while(buffer[i] == ' ')
    {
        i++;
    }
    tmp = buffer[i] - '0'; 

    if((tmp == 1) || (tmp == 0))
    {
        g_ctcwifiDiagEnable = tmp;
    }    
    else
    {
        printk("the input parameter out of range\n");
    }

    return count;
}

static int ctcwifiDiagEnable_proc_read(struct seq_file *m, void *v)
{
    int len = 0;
    len = seq_printf(m, "%d\n", g_ctcwifiDiagEnable);

    return len;
}

static int ctcwifiDiagEnable_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, ctcwifiDiagEnable_proc_read, NULL);
}

/*Implement related file_operations  interfaces*/
struct file_operations diag_enable_ops=
{
    .open       = ctcwifiDiagEnable_proc_open,     //对应cat
    .read       = seq_read,
    .write      = start_ctcwifiDiagEnable_write,     //对应echo
    .llseek     = seq_lseek,
    .release    = single_release,
};

int wlan_ctcwifi_init(void)
{
     /*create /proc/ctcwifi*/
     proc_ctcwifi_dir = proc_mkdir("ctcwifi", NULL);
     if(proc_ctcwifi_dir == NULL)
     {
          printk("\n Can't create /proc/ctcwifi\n");
          return -1;
     }

     /*create /proc/ctcwifi/diag_enable*/
     if (( ctcwifi_diag_enable = proc_create("diag_enable", 0644,
         proc_ctcwifi_dir, &diag_enable_ops)) == NULL )
     {
          printk("\n Can't create /proc/ctcwifi/diag_enable file\n");
          return -1;
     }

    return 0;
}

int wlan_ctcwifi_exit(void)
{
    /* remove /proc/ctcwifi/diag_enable */
    if (ctcwifi_diag_enable)
    {
        remove_proc_entry("diag_enable", proc_ctcwifi_dir);
        ctcwifi_diag_enable = NULL;
    }        

    /* remove /proc/ctcwifi*/
    if (proc_ctcwifi_dir)
    {
        remove_proc_entry("ctcwifi", NULL);
        proc_ctcwifi_dir = NULL;
    }

    return 0;
}

module_init(wlan_ctcwifi_init);
module_exit(wlan_ctcwifi_exit);

MODULE_AUTHOR("ZC");
MODULE_LICENSE("Dual BSD/GPL");

猜你喜欢

转载自blog.csdn.net/zouchun2016/article/details/80744211