zoukankan      html  css  js  c++  java
  • 驱动调试(五)寄存器读写


    title: 驱动调试(五)寄存器读写
    date: 2019/1/14 22:30:32
    toc: true

    驱动调试(五)寄存器读写

    这个章节没什么新知识,就是先写一个能够读写寄存器的驱动,再写一个app调用提供的读写接口

    bug

    这里应该有个问题,就是当传入的地址不是4字节对齐的时候,读取u32的时候,指针转换是有问题的,但实际上这也属于app传输进来的错误

    驱动程序

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <linux/init.h>
    #include <linux/delay.h>
    #include <linux/irq.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/poll.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 int major;
    static struct class *class;
    static struct class_device	*ker_dev;
    
    
    static int ker_rw_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
    {
    	volatile unsigned char  *p8;
    	volatile unsigned short *p16;
    	volatile unsigned int   *p32;
    	unsigned int val;
    	unsigned int addr;
    
    	unsigned int buf[2];
    
    	copy_from_user(buf, (const void __user *)arg, 8);
    	addr = buf[0];
    	val  = buf[1];
    	
    	p8  = (volatile unsigned char *)ioremap(addr, 4);
    	p16 = p8;
    	p32 = p8;
    
    	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 = val;
    			break;
    		}
    
    		case KER_RW_W16:
    		{
    			*p16 = val;
    			break;
    		}
    
    		case KER_RW_W32:
    		{
    			*p32 = val;
    			break;
    		}
    	}
    
    	iounmap(p8);
    	return 0;
    }
    
    static struct file_operations ker_rw_ops = {
    	.owner   = THIS_MODULE,
    	.ioctl   = ker_rw_ioctl,
    };
    
    static int ker_rw_init(void)
    {
    	major = register_chrdev(0, "ker_rw", &ker_rw_ops);
    
    	class = class_create(THIS_MODULE, "ker_rw");
    
    	/* 为了让mdev根据这些信息来创建设备节点 */
    	ker_dev = class_device_create(class, NULL, MKDEV(major, 0), NULL, "ker_rw"); /* /dev/ker_rw */
    	
    	return 0;
    }
    
    static void ker_rw_exit(void)
    {
    	class_device_unregister(ker_dev);
    	class_destroy(class);
    	unregister_chrdev(major, "ker_rw");
    }
    
    module_init(ker_rw_init);
    module_exit(ker_rw_exit);
    MODULE_LICENSE("GPL");
    

    APP程序

    #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:
    ");
    	printf("%s <r8 | r16 | r32> <phy addr> [num]
    ", file);
    	printf("%s <w8 | w16 | w32> <phy addr> <val>
    ", 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/ker_rw", O_RDWR);
    	if (fd < 0)
    	{
    		printf("can't open /dev/ker_rw
    ");
    		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
    ", 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
    ", 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
    ", 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;
    	
    }
    
  • 相关阅读:
    在Visual Studio中启用对jquery等javascript框架的智能感知
    网页的复制和防复制
    发现数据库对象的依赖关系
    这个世界并不缺少创意,而是缺少发现
    如何在ASP.NET程序中使用报表查看器控件并传递用户凭据
    Jquery调用webService的四种方法 【转载】
    浏览器的多线程技术
    .NET Framework 2.0的客户端是否可以调用WCF?
    在页面中实现内容的展开和收缩
    面向接口的编程(WCF)
  • 原文地址:https://www.cnblogs.com/zongzi10010/p/10272072.html
Copyright © 2011-2022 走看看