diff --git a/study/driver/debug/kerrw/Makefile b/study/driver/debug/kerrw/Makefile new file mode 100644 index 0000000..9e4a182 --- /dev/null +++ b/study/driver/debug/kerrw/Makefile @@ -0,0 +1,78 @@ +# ------------------------------------------------------------------------------ +# +# Makefile for the LDD-LinuxDeviceDrivers. +# +# Author: gatieme +# Create: 2016-07-29 15:50:46 +# Last modified: 2016-07-29 16:10:29 +# Description: +# This program is loaded as a kernel(v2.6.18 or later) module. +# Use "make install" to load it into kernel. +# Use "make remove" to remove the module out of kernel. +# +# ------------------------------------------------------------------------------ + + +ROOT=.. +#PLATFORM=$(shell $(ROOT)/systype.sh) +#include $(ROOT)/Make.defines.$(PLATFORM) + +# my driver description +DRIVER_VERSION := "1.0.0" +DRIVER_AUTHOR := "Gatieme @ AderStep Inc..." +DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices" +DRIVER_LICENSE := "Dual BSD/GPL" + + +MODULE_NAME := ker_rw + +ifneq ($(KERNELRELEASE),) + +#CFG_FLAGS += -O2 -I./ +#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC) + + + +obj-m := $(MODULE_NAME).o #print_vmarea.o + +else + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build + +PWD := $(shell pwd) + +modules: + make -C $(KERNELDIR) M=$(PWD) modules + +modules_install: + make -C $(KERNELDIR) M=$(PWD) modules_install + + + +insmod: + sudo insmod $(MODULE_NAME).ko + +reinsmod: + sudo rmmod $(MODULE_NAME) + sudo insmod $(MODULE_NAME).ko + +github: + cd $(ROOT) && make github + +rmmod: + sudo rmmod $(MODULE_NAME) + +test : + sudo ../injector/memInjector -l stack -m random -t word_0 --time 1 --timeout 3 -p 1 + +clean: + make -C $(KERNELDIR) M=$(PWD) clean + rm -f modules.order Module.symvers Module.markers + +.PHNOY: + modules modules_install clean + + + +endif + diff --git a/study/driver/debug/kerrw/ker_rw.c b/study/driver/debug/kerrw/ker_rw.c new file mode 100644 index 0000000..56fcd67 --- /dev/null +++ b/study/driver/debug/kerrw/ker_rw.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if 0 +#include +#endif +#include +#include +#include +#include + +#define KER_RW_R8 100 +#define KER_RW_R16 101 /* 切记!cmd命令值不能乱定义 */ +#define KER_RW_R32 102 + +#define KER_RW_W8 103 +#define KER_RW_W16 104 +#define KER_RW_W32 105 + + +static int major; +static struct class *class; + + +static long ker_rw_ioctl( 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, + .unlocked_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根据这些信息来创建设备节点 */ + device_create(class, NULL, MKDEV(major, 0), NULL, "ker_rw"); /* /dev/ker_rw */ + + return 0; +} + +static void ker_rw_exit(void) +{ + device_destroy(class,MKDEV(major, 0)); + class_destroy(class); + unregister_chrdev(major, "ker_rw"); +} + +module_init(ker_rw_init); +module_exit(ker_rw_exit); + + +MODULE_LICENSE("GPL"); diff --git a/study/driver/debug/kerrw/regeditor.c b/study/driver/debug/kerrw/regeditor.c new file mode 100644 index 0000000..5244761 --- /dev/null +++ b/study/driver/debug/kerrw/regeditor.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define KER_RW_R8 100 +#define KER_RW_R16 101 +#define KER_RW_R32 102 + +#define KER_RW_W8 103 +#define KER_RW_W16 104 +#define KER_RW_W32 105 + + +/* 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 [num]\n", file); + printf("%s \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/ker_rw", 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] = %04x\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] = %08x\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; + +} \ No newline at end of file