设备驱动调试自制工具寄存器编辑器

完成版的代码

  • view.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/device.h>

#define KER_RW_R8      0
#define KER_RW_R16     1
#define KER_RW_R32     2

#define KER_RW_W8      3
#define KER_RW_W16     4
#define KER_RW_W32     5
static struct class *view_class;
static struct class_device  *view_class_dev;



static int view_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 
    unsigned long arg)
{
    
    unsigned long *buf = (unsigned long *)arg;
    unsigned long val = buf[1]; 
    volatile unsigned long *p8;
    volatile unsigned long *p16;
    volatile unsigned long *p32;
    p8 = (volatile unsigned long*)ioremap(buf[0], 4);
    p16 = p8;
    p32 = p8;
    
#if 0
    printk("func:%s, line:%d,  p8:%p, p16:%p, p32:%p\n", 
    __func__, __LINE__, p8, p16, p32);
    printk("buf[0]:%#lx, buf[1]:%#lx, cmd:%d\n", 
    buf[0], buf[1], cmd);
#endif
    switch (cmd) {
        case KER_RW_R8: {
            val = *p8;
            copy_to_user((void __user * )(arg + 4), &val, 4);
        }  
        break;
        case KER_RW_R16:
        {
            val = *p16;
            copy_to_user((void __user * )(arg + 4), &val, 4);
        }
        break;

        case KER_RW_R32: {
            val = *p32;
            copy_to_user((void __user * )(arg + 4), &val, 4);
        }
        break;

        case KER_RW_W8: {
            *p8 = (unsigned char)val;
        }
        break;

        case KER_RW_W16: 
            *p16 = (unsigned short)val;

        case KER_RW_W32:
            *p32 = (unsigned int)val;
            printk("*p32 = %x, val:%x, unisgned int val:%x\n", 
            *p32, val, (unsigned int)val);
        break;
        default:
            printk("no match");
        break;  
    }
    return 0;
}

static struct file_operations view_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .ioctl  =   view_ioctl,
};


int major;
static int view_init(void)
{
    major = register_chrdev(0, "view", &view_drv_fops); // 注册, 告诉内核

    view_class = class_create(THIS_MODULE, "view");

    view_class_dev = class_device_create(view_class, NULL, MKDEV(major, 0), NULL, "view"); /* /dev/xyz */

    return 0;
}

static void view_exit(void)
{
    unregister_chrdev(major, "view"); // 卸载

    class_device_unregister(view_class_dev);
    class_destroy(view_class);
}

module_init(view_init);
module_exit(view_exit);


MODULE_LICENSE("GPL");

  • test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define KER_RW_R8      0
#define KER_RW_R16     1
#define KER_RW_R32     2

#define KER_RW_W8      3
#define KER_RW_W16     4
#define KER_RW_W32     5


/* Usage:
 * ./regeditor r8  addr [num]
 * ./regeditor r16 addr [num]
 * ./regeditor r32 addr [num]
 *
 * ./regeditor w8  addr val
 * ./regeditor w16 addr val 
 * ./regeditor w32 addr val
 */

void print_usage(char *file)
{
    printf("Usage:\n");
    printf("%s <r8 | r16 | r32> <phy addr> [num]\n", file);
    printf("%s <w8 | w16 | w32> <phy addr> <val>\n", file);
}

int main(int argc, char **argv)
{
    int fd;
    unsigned int buf[2];
    unsigned int i;
    unsigned int num;
    
    if ((argc != 3) && (argc != 4))
    {
        print_usage(argv[0]);
        return -1;
    }

    fd = open("/dev/view", O_RDWR);
    if (fd < 0)
    {
        printf("can't open /dev/ker_rw\n");
        return -2;
    }

    /* addr */
    buf[0] = strtoul(argv[2], NULL, 0);

    if (argc == 4)
    {
        buf[1] = strtoul(argv[3], NULL, 0);
        num    = buf[1];
    }
    else
    {
        num = 1;
    }

    if (strcmp(argv[1], "r8") == 0)
    {
        for ( i = 0; i < num; i++)
        {
            ioctl(fd, KER_RW_R8, buf);  /* val = buf[1] */
            printf("%02d. [%08x] = %02x\n", i, buf[0], (unsigned char)buf[1]);
            buf[0] += 1;
        }
    }
    else if (strcmp(argv[1], "r16") == 0)
    {
        for ( i = 0; i < num; i++)
        {
            ioctl(fd, KER_RW_R16, buf);  /* val = buf[1] */
            printf("%02d. [%08x] = %02x\n", i, buf[0], (unsigned short)buf[1]);
            buf[0] += 2;
        }
    }
    else if (strcmp(argv[1], "r32") == 0)
    {
        for ( i = 0; i < num; i++)
        {
            ioctl(fd, KER_RW_R32, buf);  /* val = buf[1] */
            printf("%02d. [%08x] = %02x\n", i, buf[0], (unsigned int)buf[1]);
            buf[0] += 4;
        }
    }
    else if (strcmp(argv[1], "w8") == 0)
    {
        ioctl(fd, KER_RW_W8, buf);  /* val = buf[1] */
    }
    else if (strcmp(argv[1], "w16") == 0)
    {
        ioctl(fd, KER_RW_W16, buf);  /* val = buf[1] */
    }
    else if (strcmp(argv[1], "w32") == 0)
    {
        ioctl(fd, KER_RW_W32, buf);  /* val = buf[1] */
    }
    else
    {
        printf(argv[0]);
        return -1;
    }

    return 0;
    
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容