您的位置:首页 > 编程语言

proc文件系统创建实例二(引出 seq file 文件系统的创建过程,结构化信息显示)

2017-10-08 14:37 816 查看
一. 在分析 mtkfb.c 源代码时,注意到以下代码:

#include <linux/seq_file.h>
#include <linux/proc_fs.h>

#define DEVINFO_HASH_SIZE    1024

char twdevinfo[1024]={0};
EXPORT_SYMBOL(twdevinfo);

static int devinfo_show(struct seq_file *f, void *v)
{
int i = *(loff_t *) v;
if (i < DEVINFO_HASH_SIZE) {
if (i == 0)
seq_puts(f, "show devices:\n");/* 将这个字符串输入到 f 中 */

chrdev_show(f, i);
seq_printf(f, "%s", twdevinfo); /* 将 twdevinfo 信息格式化输入到 f 中 */
}
return 0;
}

static void *devinfo_start(struct seq_file *f, loff_t *pos)
{
if (*pos < 1)
return pos;
return NULL;
}

static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
{
(*pos)++;
if (*pos >= 1)
return NULL;
return pos;
}

static void devinfo_stop(struct seq_file *f, void *v)
{
/* devinfo_stop函数为空即可 */
}

static const struct seq_operations devinfo_ops = {
.start = devinfo_start,
.next  = devinfo_next,
.stop  = devinfo_stop,
.show  = devinfo_show
/* 在 devinfo_ops 结构体中,我们需要实现 devinfo_start,devinfo_next,devinfo_stop,devinfo_show 这4个函数 */
};

static int devinfo_open(struct inode *inode, struct file *filp)
{
return seq_open(filp, &devinfo_ops);
/* 在 devinfo_open 函数中,调用 seq_open 函数。接下来,我们只需要实现 devinfo_ops 结构体。这里是结构化显示,使用seq_open函数 */
}

static const struct file_operations proc_devinfo_operations = {
.open		= devinfo_open,
.read		= seq_read,
.llseek		= seq_lseek,
.release	= seq_release,
/* 在 proc_devinfo_operations 结构体中,我们实现了 seq 文件系统,其中,seq_read,seq_lseek,seq_release 都已经在内核中实现好了,
我们只需要实现 devinfo_open 函数 。这里是结构化信息显示,使用seq_realease函数*/
};

static int mtkfb_probe(struct device *dev)
{
……
proc_create("devinfo", 0, NULL, &proc_devinfo_operations);
/* 创建一个 proc 文件系统,名称为 devinfo */
 ……
}


二. 一般来说,创建一个proc 文件系统后,推荐使用seq 文件系统来实现具体接口。在实现以上代码后,进入手机的abd shell,输入 cat /proc/devinfo,可以得到以下信息:

show devices:

AUDIO_VERNO: 009

LCD: YKL_S052HPKP0003ACD3YA_OTM1289A_170711(*)

FINGERPRINT: goodix 2017-07-05

FLASH name:DF4032, cid: 45010044463430333201ae8f(*)

LPSENSOR: stk33112017-04-1 (*)

GSENSOR: kxtj2_1009 2017-06-30

TP: YKL_S052HPKP0003ACD3YA_FT3327 2017-06-27 fts_vend_id: 0x7, firmware version: 23.0.0(*)

BATTERY_CHARGING:

AC=100000

FULL_CUR=140

CV_VOLTAGE=4375000

RECHARGING_VOL=4110

CC2TOPOFF=4050

HIGH VOLTAGE BATTERY:

QMAX=2708

QMAX_50=2737

QMAX_0=2648

QMAX_10=2500

CAR_TUNE=101

[CAM.B.0]: s5k3l8mipiraw, 2017-07-04, RAW(*)

[CAM.F.0]: ov8856mipiraw, 2017-06-26, RAW(*)

OV8856R1AOTP : FLAG[d0] MID[42] LID[1] DA[0] Y[17] M[6] D[19]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐