From d59d0d03a66499b7d298e3ac9143a9742fd8d376 Mon Sep 17 00:00:00 2001 From: zhji Date: Wed, 17 Dec 2025 15:18:58 +0800 Subject: [PATCH] [feat] add linux hello_driver --- linux/driver/hello_driver/Makefile | 14 ++++ linux/driver/hello_driver/app.c | 36 ++++++++++ linux/driver/hello_driver/hello_driver.c | 89 ++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 linux/driver/hello_driver/Makefile create mode 100644 linux/driver/hello_driver/app.c create mode 100644 linux/driver/hello_driver/hello_driver.c diff --git a/linux/driver/hello_driver/Makefile b/linux/driver/hello_driver/Makefile new file mode 100644 index 0000000..2967d67 --- /dev/null +++ b/linux/driver/hello_driver/Makefile @@ -0,0 +1,14 @@ +KERNELDIR := /lib/modules/$(shell uname -r)/build +CURRENT_PATH := $(shell pwd) +obj-m := hello_driver.o + +build: kernel_modules + +kernel_modules: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules + $(CROSS_COMPILE)gcc -o app.elf app.c +clean: + $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean + rm -rf app.elf + + diff --git a/linux/driver/hello_driver/app.c b/linux/driver/hello_driver/app.c new file mode 100644 index 0000000..9806107 --- /dev/null +++ b/linux/driver/hello_driver/app.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +uint8_t buffer[512] = { 0 }; + +int main(int argc, char *argv[]) +{ + int fd; + int ret; + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + printf("open failed!\r\n"); + return -1; + } + if (!strcmp("read", argv[2])) { + printf("read data from kernel\r\n"); + ret = read(fd, buffer, sizeof(buffer)); + printf("ret len:%d data:%s\r\n", ret, buffer); + } else if (!strcmp("write", argv[2])) { + printf("write data to kernel %s len:%ld\r\n", argv[3], strlen(argv[3])); + ret = write(fd, argv[3], strlen(argv[3])); + printf("ret len:%d\r\n", ret); + } else { + printf("please input correct para, read/write\r\n"); + } + close(fd); + return 0; +} + diff --git a/linux/driver/hello_driver/hello_driver.c b/linux/driver/hello_driver/hello_driver.c new file mode 100644 index 0000000..be8d88b --- /dev/null +++ b/linux/driver/hello_driver/hello_driver.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +dev_t hello_dev_id; +struct cdev hello_cdev; +int hello_major = 0; +int hello_minor; + +uint8_t kernel_buffer[1024] = { 0 }; +static struct class *hello_driver_class; + +static int hello_driver_open(struct inode *inode, struct file *file) +{ + printk("hello_driver_open\r\n"); + return 0; +} + +static int hello_driver_release(struct inode *inode, struct file *file) +{ + printk("hello_driver_release\r\n"); + return 0; +} + +static ssize_t hello_driver_read(struct file *file, char __user *buffer, size_t size, loff_t *ppos) +{ + printk("hello_driver_read size=%ld\r\n", size); + copy_to_user(buffer, kernel_buffer, size); + return size; +} + +static ssize_t hello_driver_write(struct file *file, const char __user *buffer, size_t size, loff_t *ppos) +{ + printk("hello_driver_write size=%ld\r\n", size); + copy_from_user(kernel_buffer, buffer, size); + return size; +} + +static const struct file_operations hello_driver_fops = { + .owner = THIS_MODULE, + .open = hello_driver_open, + .release = hello_driver_release, + .read = hello_driver_read, + .write = hello_driver_write, +}; + +static int __init hello_driver_init(void) +{ + printk("hello_driver_init\r\n"); + + /* automatically apply for device number */ + alloc_chrdev_region(&hello_dev_id, 0, 1, "hello_driver"); + hello_major = MAJOR(hello_dev_id); + hello_minor = MINOR(hello_dev_id); + printk("hello_driver: major=%d, minor=%d\r\n", hello_major, hello_minor); + + hello_cdev.owner = THIS_MODULE; + cdev_init(&hello_cdev, &hello_driver_fops); + cdev_add(&hello_cdev, hello_dev_id, 1); + // ret = register_chrdev(CHRDEVBASE_MAJOR, "hello_driver", &hello_driver_fops); + + /* automatically add device node file */ + hello_driver_class = class_create("hello_driver_class"); + device_create(hello_driver_class, NULL, hello_dev_id, NULL, "hello_driver_device"); + + return 0; +} + +static void __exit hello_driver_exit(void) +{ + printk("hello_driver_exit\r\n"); + + cdev_del(&hello_cdev); + unregister_chrdev_region(hello_dev_id, 1); + + device_destroy(hello_driver_class, hello_dev_id); + class_destroy(hello_driver_class); + // unregister_chrdev(CHRDEVBASE_MAJOR, "hello_driver"); +} + +module_init(hello_driver_init); +module_exit(hello_driver_exit); +MODULE_LICENSE("GPL");