[feat] add linux hello_driver

This commit is contained in:
zhji 2025-12-17 15:18:58 +08:00
parent 8e45f42944
commit d59d0d03a6
3 changed files with 139 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
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;
}

View File

@ -0,0 +1,89 @@
#include <linux/types.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/cdev.h>
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");