proc文件的读写

本文介绍如何在Linux内核中创建并使用proc文件进行调试。通过示例代码展示了如何初始化和注册一个名为'debug_tp'的proc文件,允许读写操作,并在模块卸载时删除。
摘要由CSDN通过智能技术生成

工作中有时需要创建proc文件来debug,这样的代码网上很多,我也参考了网上的代码,作了以下调整,记录下来以资后用。


static int register_addr;

static int proc_read(char *page, char **start, off_t off, int count, int *eof,  
                void *data) {  
    u8 value;
    int len;

    i2c_smbus_read_i2c_block_data(i2c_client, (u8)register_addr, 1, &value);
    len = sprintf(page, "the value of register addr(0x%x) is 0x%x", register_addr, value);
    printk("page is %s\n", page);

    return len;  
}
   
static int proc_write(struct file *file, const char *buffer, unsigned long count,  
                void *data) { 
    int tmp;
    char type;
    unsigned char value;
    char *p = NULL;

    p = strstr(buffer, "type=");
    if (p)
    {
        type = p[5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值