美文网首页
设备驱动调试自制工具寄存器编辑器

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

作者: sgy1993 | 来源:发表于2017-08-02 16:52 被阅读0次

    完成版的代码

    • 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;
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:设备驱动调试自制工具寄存器编辑器

          本文链接:https://www.haomeiwen.com/subject/gyjelxtx.html