驱动程序demo.c
demo_ioctl():实现接口调用的过程。
do_write():实现将用户写入的数据逆序排列。
demo_read():返回逆序排列后的数据。
应用层程序列表。
test_demo.c:应用层用户测试程序源码。
Makefile:驱动程序编译配置文件。
static void do_write(void);
static ssize_t demo_write(struct file *filp,const char *buffer, size_t count)
static ssize_t demo_read(struct file *filp, char *buffer, size_t count, loff_t *ppos);
static int demo_ioctl(struct inode *inode, struct file *file,unsigned int cmd, unsigned long arg);
static int demo_open(struct inode *inode, struct file *file);
static int demo_release(struct inode *inode, struct file *filp);
static int __init demo_init(void);
static void __exit demo_exit(void);
static struct file_operations demo_fops = {
owner: THIS_MODULE,
write: demo_write,
read:demo_read,
ioctl: demo_ioctl,
open: demo_open,
release: demo_release}
module_init(demo_init); //模块初始化
MODULE_LICENSE("Dual BSD/GPL"); //版权信息
#endif //MODULE
makefile
TARGET = test_demo
CROSS_COMPILE = arm-linux-
CC = $(CROSS_COMPILE)gcc
STRIP = $(CROSS_COMPILE)strip
ifeq ($(KERNELRELEASE),)
KERNELDIR ?=/CBT-SuperIOT/SRC/kernel/linux-2.6.35.7 #确认系统内核路径
PWD := $(shell pwd)
all: $(TARGET) modules
$(TARGET):
$(CC) -o $(TARGET) $(TARGET).c
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
.PHONY: modules modules_install clean
else
obj-m := demo.o #需要编译生成驱动程序模块demo.ko
endif
test_demo.c应用层程序
int main()
{
int fd;
int i;
char buf[255];
for(i=0; i<MAX_LEN; i++){ //初始化数组
buf[i]=i;}
fd=open("/dev/demo",O_RDWR);//系统open打开ARM 端设备节点/dev/demo
if(fd < 0){
printf("####DEMO device open fail####\n");
return (-1);}
printf("write %d bytes data to /dev/demo \n",MAX_LEN);
showbuf(buf);
write(fd,buf,MAX_LEN); //系统write,向设备节点/dev/demo 写入数组内容
printf("Read %d bytes data from /dev/demo \n",MAX_LEN);
read(fd,buf,MAX_LEN); //系统read,从设备节点/dev/demo读回数组内容并显示
showbuf(buf);
close(fd); //驱动程序定义了release,这里使用系统调用
return 0;
}