工作中有时需要创建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];
printk("operater type = %c\n", type);
}
p = strstr(buffer, "reg=");
if (p)
{
sscanf(p+4, "%d", ®ister_addr);
printk("register_addr = 0x%x\n", register_addr);
}
if(type == 'w' || type == 'W')
{
p = strstr(buffer, "val=");
if (p)
{
sscanf(p+4, "%d", &tmp);
printk("write data(0x%x) to register address(0x%x)\n", tmp, register_addr);
value = (u8)tmp;
i2c_smbus_write_i2c_block_data(i2c_client, (u8)register_addr, 1, &value);
}
}
return count;
}
static struct proc_dir_entry *tp_debug_register;
#define MY_PROC_NAME "debug_tp"
static int __init tpd_driver_init(void) {
tp_debug_register = create_proc_entry(MY_PROC_NAME, S_IRUGO, NULL);
tp_debug_register->read_proc = proc_read;
tp_debug_register->write_proc = proc_write;
return 0;
}
/* should never be called */
static void __exit tpd_driver_exit(void) {
remove_proc_entry(MY_PROC_NAME, NULL);
}
module_init(tpd_driver_init);
module_exit(tpd_driver_exit);