android-kernel 增加debug-fs调试方法

1, 在相应代码增加下面代码:
#ifdef CONFIG_DEBUG_FS   //add by wyj

static int reg_open_file(struct inode *inode, struct file *file)
{
     file->private_data = inode->i_private;
     return 0;
}

static ssize_t reg_read_file(struct file *file, char __user *user_buf,
                       size_t count, loff_t *ppos)
{
     ssize_t ret;
     struct s3cfb_global *fbdev = file->private_data;
     char *buf;

     if (*ppos < 0 || !count)
          return -EINVAL;

     printk("read count:%d\n", count);

     printf_reg_value("S3C_VIDCON0", (u32)(fbdev->regs +S3C_VIDCON0));
     printf_reg_value("S3C_VIDCON1", (u32)(fbdev->regs +S3C_VIDCON1));
     printf_reg_value("S3C_VIDCON2", (u32)(fbdev->regs +S3C_VIDCON2));

     printf_reg_value("S3C_VIDTCON0", (u32)(fbdev->regs +S3C_VIDTCON0));
     printf_reg_value("S3C_VIDTCON1", (u32)(fbdev->regs +S3C_VIDTCON1));
     printf_reg_value("S3C_VIDTCON2", (u32)(fbdev->regs +S3C_VIDTCON2));

     printf_reg_value("S3C_WINCON0", (u32)(fbdev->regs +S3C_WINCON0));
     printf_reg_value("S3C_WINCON1", (u32)(fbdev->regs +S3C_WINCON1));
     printf_reg_value("S3C_WINCON2", (u32)(fbdev->regs +S3C_WINCON2));
     printf_reg_value("S3C_WINCON3", (u32)(fbdev->regs +S3C_WINCON3));
     printf_reg_value("S3C_WINCON4", (u32)(fbdev->regs +S3C_WINCON4));


     return 0;
}

int chtoi(char c)
{
     int ret = -1;
    
     if ((c >= '0')&&(c <= '9'))
     {
          ret = c -48;
     }
     else if ((c >= 'a')&&(c <= 'f'))
     {
          ret = c -97 + 10;
     }
     else if ((c >= 'A')&&(c <= 'F'))
     {
          ret = c -65 + 10;
     }

     //printk("char =%c , ret=%d\n", c, ret);
    
     return ret;
}

static ssize_t reg_write_file(struct file *file,
          const char __user *user_buf, size_t count, loff_t *ppos)
{
     char buf[32];
     size_t buf_size;
     char *start = buf;
     unsigned int reg= 0, value = 0;
     volatile unsigned int *tmpaddr = NULL;
     int step = 1;
     int tmp_i=0;
     struct s3cfb_global *sd = file->private_data;

     buf_size = min(count, (sizeof(buf)-1));
     if (copy_from_user(buf, user_buf, buf_size))
          return -EFAULT;
     buf[buf_size] = 0;
    
     while (*start == ' ')
          start++;

     //printk("1write count:%d, start=%s\n", count, start);

     if (count < 11)
     {
          printk("input addr invalid\n");
          return -EINVAL;
     }

     if ((*(start++) != '0')||(tolower(*(start++)) != 'x'))
     {
          printk("input addr invalid, start with '0x'\n");
          return -EINVAL;
     }

     int i;
     for (i = 0; i < 8; i++)
     {
          if (~((*start >= '0')&&(*start <= '9'))
               &&((*start >= 'a')&&(*start <= 'f'))
               &&((*start >= 'A')&&(*start <= 'F')))
               return -EINVAL;

         
          tmp_i = chtoi(*start);
          if (tmp_i < 0)
               return -EINVAL;
          tmp_i = tmp_i<<((8- i-1)*4);
          reg = reg+ tmp_i;

          //printk_wyj("reg=%x, tmp_i=%d", (unsigned int)reg, tmp_i);
          start++;
     }

     //printk_wyj("reg=0x%x, value=%x", (unsigned int)reg, (unsigned char)value);

     tmpaddr = (volatile unsigned int *)ioremap(reg, 1024);

     value = readl(tmpaddr);

     printk_wyj("Regs(0x%x) to map(0x%x)=0x%x", (unsigned int)reg,(unsigned int)tmpaddr, (unsigned int)value);

     iounmap(tmpaddr);
     return buf_size;
}

static const struct file_operations s3cfb_reg_fops = {
     .open = reg_open_file,
     .read = reg_read_file,
     .write = reg_write_file,
     //.llseek = default_llseek,
};

static void s3cfb_debugfs_init(struct s3cfb_global *sd)
{
     struct dentry *s3cfb_debugfs_root = NULL;

     printk_wyj("%s in s3cfb.c\n", __func__);
    
     s3cfb_debugfs_root = debugfs_create_dir("wyj", NULL);
     if (IS_ERR(s3cfb_debugfs_root) || !s3cfb_debugfs_root)
     {
          printk(KERN_WARNING  "s3cfb: Failed to create debugfs directory\n");
          s3cfb_debugfs_root = NULL;
     }

     if (!debugfs_create_file("regs", 0644, s3cfb_debugfs_root, sd, &s3cfb_reg_fops))
          pr_warn("s3cfb: Failed to create regs list debugfs file\n");

}

#endif


2, 在配置文件中,打开CONFIG_DEBUG_FS
增加CONFIG_DEBUG_FS=y到arch/arm/configs/smdkc110_android_defconfig文件中

3,将s3cfb_debugfs_init()加入到某个必执行的probe函数中,这里是加到s3cfb.c中的probe()中,同时增加#include <linux/debugfs.h>

你可能感兴趣的:(c,struct,user,File,null,input)