怎样在Linux驱动中创建proc节点(示例)
发布日期:2021-06-30 18:52:33 浏览次数:2 分类:技术文章

本文共 1691 字,大约阅读时间需要 5 分钟。

一、定义proc节点的读、写函数

static int tp_switch_writeproc(struct file *file,const char *buffer,

                           unsigned long count,void *data)

{

        sscanf(buffer,"%d", &tp_dbg);

        printk("tpd: proc-->tp_dbg = %d\n", tp_dbg);

        return count;

}

 

static int tp_switch_readproc(char *page, char **start, off_t off,

                                    int count,int *eof, void *data)

{

        int len;

        unsigned char tmp =tp_dbg&0x0F;

        len = sprintf(page,"%c\n", tmp);

        return 2;

}

 

二、驱动加载时创建proc节点的入口

#include <linux/proc_fs.h>

static struct proc_dir_entry *tp_root;  
static struct proc_dir_entry *debug_entry;  
#define USER_ROOT_DIR "tp_debug"  
#define USER_ENTRY1   "debug_switch"

staticint goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)

{

        … …

        init_debug_port();   //创建proc调试节点

        … …

}

static int init_debug_port(void)

{

        pt_root =proc_mkdir(USER_ROOT_DIR, NULL); 

        if (NULL==pt_root) 

        { 

                printk(KERN_ALERT"Create dir /proc/%s error!\n", USER_ROOT_DIR); 

                return -1; 

        } 

        printk(KERN_INFO"Create dir /proc/%s\n", USER_ROOT_DIR); 

 

        // Create a test entryunder USER_ROOT_DIR 

        pt_entry1 =create_proc_entry(USER_ENTRY1, 0666, pt_root); 

        if (NULL ==pt_entry1) 

        { 

                printk(KERN_ALERT"Create entry %s under /proc/%s error!\n", 

                                            USER_ENTRY1,USER_ROOT_DIR); 

                goto err_out; 

         }

 

         pt_entry1->write_proc= tp_switch_writeproc;

         pt_entry1->read_proc =tp_switch_readproc;

         printk(KERN_INFO"Create /proc/%s/%s\n", 

                   USER_ROOT_DIR,USER_ENTRY1);  

 

         return 0; 

err_out: 

         pt_entry1->read_proc =NULL; 

         pt_entry1->write_proc= NULL;

         remove_proc_entry(USER_ROOT_DIR,pt_root); 

         return -1; 

}

 

三、卸载驱动时删除proc节点

static void remove_debug_port(void)

{

        remove_proc_entry(USER_ENTRY1,pt_root); 

        remove_proc_entry(USER_ROOT_DIR,NULL);

}

 

static int goodix_ts_remove(struct i2c_client *client)

{

        … …

        remove_debug_port();

        … …

}

转载地址:https://linus.blog.csdn.net/article/details/42239271 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android上层怎样读写proc节点(示例)
下一篇:Android 4.1打开相机(自己写的压力测试APK)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月24日 07时08分35秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Image Pyramid(图像金字塔) 2019-04-30
Oracle 作业记录 2019-04-30
putty连接AWS配置(multimedia project) 2019-04-30
Hourglass Network 沙漏网络 (pose estimation姿态估计) 2019-04-30
OpenCV实战(二)——答题卡识别判卷 2019-04-30
目标检测神经网络的发展历程(52 个目标检测模型) 2019-04-30
Boundary loss 损失函数 2019-04-30
神经网络调参实战(一)—— 训练更多次数 & tensorboard & finetune 2019-04-30
tensorflow使用tensorboard进行可视化 2019-04-30
神经网络调参实战(二)—— activation & initializer & optimizer 2019-04-30
凸优化 convex optimization 2019-04-30
数据库索引 & 为什么要对数据库建立索引 / 数据库建立索引为什么会加快查询速度 2019-04-30
IEEE与APA引用格式 2019-04-30
research gap 2019-04-30
pytorch训练cifar10数据集查看各个种类图片的准确率 2019-04-30
Python鼠标点击图片,获取点击点的像素坐标 2019-04-30
路径规划(一) —— 环境描述(Grid Map & Feature Map) & 全局路径规划(最优路径规划(Dijkstra&A*star) & 概率路径规划(PRM&RRT)) 2019-04-30
神经网络调参实战(四)—— 加深网络层次 & 批归一化 batch normalization 2019-04-30
数据挖掘与数据分析(三)—— 探索性数据分析EDA(多因子与复合分析) & 可视化(1)—— 假设检验(μ&卡方检验&方差检验(F检验))&相关系数(皮尔逊&斯皮尔曼) 2019-04-30
RRT算法(快速拓展随机树)的Python实现 2019-04-30