日期:2014-05-16  浏览次数:20768 次

怎样在Linux驱动中创建proc节点(示例)

一、定义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);