环境确认
因为驱动程序是调用的内核头文件,所以首先需要确认头文件有没有,下面第一条命令是确认有没有,没有的话,使用第二条进行下载。
apt-cache search linux-headers-$(uname -r)
sudo apt-get install linux-headers-$(uname -r)
代码实现
目录结构
其中test_driver.c是驱动程序,编译结果是test.ko文件。app.c 是使用驱动, 通过`gcc app.c -o app`编译成可执行程序。
test_driver.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
static int major = 0;
static struct class *hello_for_class;
#define KMAX_LEN 32
char kbuf[KMAX_LEN+1] = "kernel_data";
static size_t count = strlen("kernel_data");
static ssize_t hello_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s line %d size %d\n",__FILE__,__FUNCTION__, __LINE__, size);
if(size > count)
{
size = count;
}
if(copy_to_user(buf,kbuf, size)) //内核空间拷贝数据到用户空间
{
return -EFAULT;
}
return size;
}
static ssize_t hello_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s line %d size %d\n", __FILE__, __FUNCTION__, __LINE__, size);
if(size > KMAX_LEN)
{
size = KMAX_LEN;
}
count = size;
memset(kbuf,0,sizeof(kbuf));
if(copy_from_user(kbuf, buf, size)) //用户空间拷贝数据到内核空间
{
return -EFAULT;
}
printk("%s\n",kbuf);
return size;
}
static struct file_operations hello_linux_fops = {
.owner = THIS_MODULE,
.read = hello_read,
.write = hello_write,
};
int __init hello_init(void)
{
printk("hello_linux_dri init\n");
major = register_chrdev(0,"hello_linux_dri", &hello_linux_fops);
hello_for_class = class_create(THIS_MODULE, "hello_class");
device_create(hello_for_class, NULL, MKDEV(major, 0), NULL, "hello_linux_dri");
return 0;
}
void __exit hello_exit(void)
{
printk("hello_linux_dri exit\n");
device_destroy(hello_for_class, MKDEV(major, 0));
class_destroy(hello_for_class);
unregister_chrdev(major, "hello_linux_dri");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
Makefile
CONFIG_MODULE_SIG=n
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
MOD_NAME := test
obj-m := ${MOD_NAME}.o
${MOD_NAME}-objs += test_driver.o
modules:
$(MAKE) -C $(KDIR) M=$(PWD) modules
.PHONEY:clean
clean :
$(MAKE) -C $(KDIR) M=$(PWD) clean
app.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int fd;
char buf[1024];
int len;
int ret;
if (argc < 2) {
printf("Usage: %s -w <string>\n", argv[0]);
printf(" %s -r\n",argv[0]);
return -1;
}
fd = open("/dev/hello_linux_dri", O_RDWR);
if (fd == -1)
{
printf("can not open file /dev/hello_linux_dri\n");
return -1;
}
printf("open /dev/hello_linux_dri success\n");
if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
{
len = strlen(argv[2]) + 1;
len = len < 1024 ? len : 1024;
ret = write(fd, argv[2], len);
printf("write driver : %d\n", ret);
} else {
len = read(fd, buf, 1024);
printf("read driver : %d\n", len);
buf[1023] = '\0';
printf("APP read : %s\n", buf);
}
close(fd);
return 0;
}
编译
驱动编译
make
生成: test.ko
app编译
gcc app.c -o app
生成: app
测试
驱动安装卸载:
驱动安装: sudo insmod test.ko
查看驱动安装和卸载信息:dmesg
卸载驱动:sudo rmmod test.ko
运行驱动测试程序
需要驱动安装后测试,驱动卸载后使用会报错
参考:https://harmonyos.51cto.com/posts/9195