1向节点读数据cat
命令:cat /proc/节点名
例子1:
C:\Users\wuchengbing>adb shell
root@M1:/ # cat proc/tp_info
cat proc/tp_info
[Vendor]guangtai, [fw]0x01, [IC]HX8527
root@M1:/ #
例子2:
root@M1:/sys/class/leds/lcd-backlight # cat /proc/devicesinfo
name module vender IC Version info used
LCM JD-45FS0005-V0 JUNDA ILI9806E (null)480*854 false
... ...
2向节点写数据echo
命令:echo 80 > brightness
例子:
root@M1:/sys/class/leds/lcd-backlight # ls
ls
brightness
device
div
duty
frequency
max_brightness
power
pwm_register
subsystem
trigger
uevent
root@M1:/sys/class/leds/lcd-backlight # echo 80 > brightness
3添加节点方法
proc_create(CTP_PROC_FILE, 0444, NULL,>p_info_proc_fops): 创建节点函数
CTP_PROC_FILE:节点名字
NULL: 基于/proc/目录下创建节点
gtp_info_proc_fops:节点文件结构体
copy_to_user():复制到用户空间函数
proc_create的使用例子
_gProcClassEntry = proc_mkdir(PROC_NODE_CLASS, NULL);
_gProcMsTouchScreenMsg20xxEntry = proc_mkdir(PROC_NODE_MS_TOUCHSCREEN_MSG20XX, _gProcClassEntry);
_gProcDeviceEntry = proc_mkdir(PROC_NODE_DEVICE, _gProcMsTouchScreenMsg20xxEntry);
_gProcChipTypeEntry = proc_create(PROC_NODE_CHIP_TYPE, PROCFS_AUTHORITY, _gProcDeviceEntry, &_gProcChipType);
if (NULL == _gProcChipTypeEntry)
{
DBG(&g_I2cClient->dev, "Failed to create procfs file node(%s)!\n", PROC_NODE_CHIP_TYPE);
}
else
{
DBG(&g_I2cClient->dev, "Create procfs file node(%s) OK!\n", PROC_NODE_CHIP_TYPE);
}
添加节点例子1
#define LCT_ADD_TP_VERSION
#if defined(LCT_ADD_TP_VERSION)
#define CTP_PROC_FILE "tp_info"
static struct proc_dir_entry *g_ctp_proc = NULL;
static int gtp_info_read_proc(struct file *file, char *buffer, size_t count, loff_t *ppos)
{
char *page = NULL;
char *ptr = NULL;
char *manu_string=NULL;
int len, err = -1;
page = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (!page) {
kfree(page);
return -ENOMEM;
}
if (private_ts->vendor_sensor_id == 34){
manu_string = "dijing";
}else if (private_ts->vendor_sensor_id == 18){
manu_string = "tongxingda";
}else{
manu_string = "Unknow";
}
ptr = page;
ptr += sprintf(ptr, "[Vendor]%s, [fw]0x%02x, [IC]HX8527\n", manu_string,private_ts->vendor_config_ver);
len = ptr - page;
if(*ppos >= len){
kfree(page);
return 0;
}
err = copy_to_user(buffer,(char *)page,len);
*ppos += len;
if(err){
kfree(page);
return err;
}
kfree(page);
return len;
}//gtp_info_read_proc
static const struct file_operations gtp_info_proc_fops = {
.write = NULL, # echo 80 > brightness
.read = gtp_info_read_proc // # cat proc/tp_info
};
#endif
static int himax852xes_probe(struct i2c_client *client, const struct i2c_device_id *id)
{...
/* LCT_ADD_TP_VERSION */
//creat tp_info proc file system
#if defined(LCT_ADD_TP_VERSION)
g_ctp_proc = proc_create(CTP_PROC_FILE, 0444, NULL,>p_info_proc_fops);
if (g_ctp_proc == NULL) {
printk("create_proc_entry failed\n");
}
#endif
...
return 0;
...
}//himax852xes_probe
添加节点例子2
#define CONFIG_SLT_DEVINFO_CTP
#ifdef CONFIG_SLT_DEVINFO_CTP
#include
struct devinfo_struct *s_DEVINFO_ctp = NULL;
static char *temp_ver;
static char *temp_ver_config;
static void devinfo_ctp_regchar(char *module,char * vendor,char *version,char *config,char *used)
{
s_DEVINFO_ctp =(struct devinfo_struct*) kmalloc(sizeof(struct devinfo_struct), GFP_KERNEL);
s_DEVINFO_ctp->device_type="CTP";
s_DEVINFO_ctp->device_module=module;
s_DEVINFO_ctp->device_vendor=vendor;
s_DEVINFO_ctp->device_ic="HX8527";
s_DEVINFO_ctp->device_info=config;//DEVINFO_NULL;
s_DEVINFO_ctp->device_version=version;
s_DEVINFO_ctp->device_used=used;
devinfo_check_add_device(s_DEVINFO_ctp);
//-> kernel/drivers/dev_info/dev_info.c:slt_devinfo_init(){proc_create("devicesinfo",...,&devinfo_stats_fops);}
}//cat /proc/devicesinfo
#endif
static int himax852xes_probe(struct i2c_client *client, const struct i2c_device_id *id)
{...
/* CONFIG_SLT_DEVINFO_CTP */
#ifdef CONFIG_SLT_DEVINFO_CTP
temp_ver = (char*)kmalloc(8,GFP_KERNEL);
sprintf(temp_ver,"0x%04x",(private_ts->vendor_fw_ver_H << 8)|private_ts->vendor_fw_ver_L);
temp_ver_config = (char*)kmalloc(8,GFP_KERNEL);
sprintf(temp_ver_config,"0x%02x",private_ts->vendor_config_ver);
switch(private_ts->vendor_sensor_id)
{
case 34:
devinfo_ctp_regchar("dijing,", "dijing.", temp_ver, temp_ver_config,DEVINFO_USED);
break;
case 18:
devinfo_ctp_regchar("tongxingda,", "tongxingda.", temp_ver, temp_ver_config,DEVINFO_USED);
break;
default:
devinfo_ctp_regchar("unknown.1", "unknown.2", "unknown.3", "unknown.4",DEVINFO_USED);
break;
}
#endif
return 0;
...
}
kernel/drivers/dev_info/dev_info.c
/*********************************************************************************
* This functions is designed to check if declared already, and add new device if not yet;
* Input: devinfo_struct
* Output: 1 / 0
* Note: return 1 for there have a same device registed,0 for new device
* *******************************************************************************/
int devinfo_check_add_device(struct devinfo_struct *dev)
{
int result = 0;
unsigned long irqflags;
struct devinfo_struct *dev_all;
printk("[DEVINFO] devinfo_check!\n");
spin_lock_irqsave(&dev_lock, irqflags);
if(list_empty(&active_devinfo_list) != 1)
list_for_each_entry(dev_all, &active_devinfo_list, device_link) {
printk("[DEVINFO] dev type:%s\n",dev->device_type);
printk("[DEVINFO] dev list type:%s\n",dev_all->device_type);
if((strcmp(dev_all->device_type,dev->device_type)==0) && (strcmp(dev_all->device_module,dev->device_module)==0) &&
(strcmp(dev_all->device_vendor,dev->device_vendor)==0) && (strcmp(dev_all->device_ic,dev->device_ic)==0) &&
(strcmp(dev_all->device_version,dev->device_version)==0) &&(strcmp(dev_all->device_info,dev->device_info)==0))// && shaohui mods here
// It will be replaced if there is a used device found! Do not mention HOT plug device! 2013.01.31
// (strcmp(dev_all->device_used,dev->device_used)==0))
{
if(strcmp(dev_all->device_used,dev->device_used)==0)
{
printk("[DEVINFO] find the same device\n");
}else if(strcmp(dev_all->device_used,DEVINFO_UNUSED)==0)
{
//we belive that we find a existed device! del the unexisted one!
printk("[DEVINFO] find device,but unused state!\n");
list_del(&dev_all->device_link);
list_add_tail(&dev->device_link, &active_devinfo_list);
// list_replace(&dev_all->device_link, &active_devinfo_list);
spin_unlock_irqrestore(&dev_lock, irqflags);
return 0;
}else{
//If a for-existed device is found lost,Do nothing~
printk("[DEVINFO] find device,but used state!\n");
}
spin_unlock_irqrestore(&dev_lock, irqflags);
return 1;
}
}
list_add_tail(&dev->device_link, &active_devinfo_list);
spin_unlock_irqrestore(&dev_lock, irqflags);
return 0;
}
static const struct file_operations devinfo_stats_fops = {
.owner = THIS_MODULE,
.open = devinfo_stats_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init slt_devinfo_init(void)
{
int ret;
printk("DEVINFO,devinfo init!\n");
ret = platform_device_register(&devinfo_device);
if (ret) {
pr_err("[slt_devinfo_init]: platform_device_register failed\n");
goto err_platform_device_register;
}
ret = platform_driver_register(&devinfo_driver);
if (ret) {
pr_err("[slt_devinfo_init]: platform_driver_register failed\n");
goto err_platform_driver_register;
}
// add for proc
proc_create("devicesinfo", S_IRUGO, NULL, &devinfo_stats_fops);
return 0;
err_platform_driver_register:
platform_device_unregister(&devinfo_device);
err_platform_device_register:
return ret;
}
Wu_Being博客声明:本人博客欢迎转载,请标明博客原文和原链接!谢谢!
《读节点(cat)和写节点(echo)》:

如果你看完这篇博文,觉得对你有帮助,并且愿意付赞助费,那么我会更有动力写下去。