字符设备驱动(内核态用户态内存交互)

前言

内核驱动:运行在内核态的动态模块,遵循内核模块框架接口,更倾向于插件。

应用程序:运行在用户态的进程。

应用程序与内核驱动交互通过既定接口,内核态和用户态访问依然遵循内核既定接口。

环境搭建

系统:openEuler-20.03-LTS-SP3

复制代码
yum install gcc kernel-devel

编写源码

  • char_module.c
c 复制代码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <asm/device.h> //下面这三个头文件是由于动态创建需要加的
#include <linux/device.h>
#include <linux/cdev.h>

MODULE_LICENSE("GPL");

#define DEVICE_NAME "char_module"
#define BUF_SIZE 32

static struct class *cdev_class;
dev_t dev_num = 0; // 这里是动态分配设备号和动态创建设备结点需要用到的
struct cdev dev_c;

static char context_buf[BUF_SIZE]={"this a test context buffer\0"};

static ssize_t read(struct file *, char *, size_t, loff_t *);
static ssize_t write(struct file *, const char *, size_t, loff_t *);
static int open(struct inode *, struct file *);
static int release(struct inode *, struct file *);

// 初始化字符设备驱动的 file_operations 结构体
struct file_operations fops = {
        .read = read,
        .write = write,
        .open = open,
        .release = release
};

static int __init demo_init(void)
{
        int ret, err;

        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);

        // 注册设备驱动
        ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME); // 动态分配设备号
        if (ret)
        {
                printk("demo_init register failure\n");
                unregister_chrdev_region(dev_num, 1);
                return ret;
        }
        printk("demo_init register success\n");

        // 初始化设备操作
        cdev_init(&dev_c, &fops);
        err = cdev_add(&dev_c, dev_num, 1);
        if (err)
        {
                printk(KERN_NOTICE "error %d adding cdev\n", err);
                unregister_chrdev_region(dev_num, 1);
                return err;
        }

        // 动态创建设备结点
        cdev_class = class_create(THIS_MODULE, DEVICE_NAME); 
        if (IS_ERR(cdev_class))
        {
                printk("ERR:cannot create a cdev_class\n");
                unregister_chrdev_region(dev_num, 1);
                return -1;
        }
        device_create(cdev_class, NULL, dev_num, 0, DEVICE_NAME);

        return ret;
}

static void __exit demo_exit(void)
{
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);

        // 注销设备驱动
        device_destroy(cdev_class, dev_num);
        class_destroy(cdev_class);
        unregister_chrdev_region(dev_num, 1);
}

static ssize_t read(struct file *filp, char *buf, size_t len, loff_t *off)
{
        // 内核空间到用户空间copy
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);
        if (raw_copy_to_user(buf, &context_buf, sizeof(context_buf)))
        {
                return -EFAULT;
        }
        printk(KERN_INFO "user space: %pF", buf);
        printk(KERN_INFO "read: %pF; size: %ld; data: %s", &context_buf, sizeof(context_buf), context_buf);
        return BUF_SIZE;
}

static ssize_t write (struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
        // 用户空间到内核空间copy
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);
        if (raw_copy_from_user(&context_buf, buf, sizeof(context_buf)))
        {
                return -EFAULT;
        }
        printk(KERN_INFO "user space: %pF", buf);
        printk(KERN_INFO "write: %pF; size: %ld; data: %s", &context_buf, sizeof(context_buf), context_buf);
        return BUF_SIZE;
}

static int open(struct inode *inodp, struct file *filp)
{
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);
        return 0;
}

static int release(struct inode *inodp, struct file *filp)
{
        printk(KERN_INFO "%s: %s", DEVICE_NAME, __func__);
        return 0;
}

module_init(demo_init);
module_exit(demo_exit);
  • Makefile
c 复制代码
ifneq ($(KERNELRELEASE),)
obj-m := char_module.o

else
PWD  := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
        $(MAKE) -C $(KDIR) M=$(PWD) modules modules_install
clean:
        rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions modules.*  Module.*
endif

app.c

c 复制代码
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
 
#define CHAR_DEV_NAME "/dev/char_module"

int main()
{
        int ret;
        int fd;
        char buf[32];
 
        fd = open(CHAR_DEV_NAME, O_RDWR | O_NDELAY);
        if(fd < 0)
        {
                printf("open failed!\n");
                return -1;
        }

        int size = read(fd, buf, 32);

        printf("read size: %d;\nbuffer:[%s]\n", size, buf);

        char *write_buf = "use a application wirte to driver buffer";

        int w_size = write(fd, write_buf, strlen(write_buf));

        printf("write size: %d;\nbuffer:[%s]\n", w_size, write_buf);

        close(fd);

        return 0;
}

构建并测试

  • 驱动构建

    bash 复制代码
    make && insmod char_module.ko
  • 驱动信息确认

  • 应用程序构建

    bash 复制代码
    gcc app.c -o app
    ./app
  • 应用程序运行结果

  • 查看驱动日志

    bash 复制代码
    dmesg
相关推荐
北极星日淘15 小时前
前端 i18n 中日双语交互 + 翻译客服接口联动方案|日系海淘平台中文友好化开发实战
前端·交互
UXbot19 小时前
帮助企业低门槛开展AI应用开发的平台推荐
前端·低代码·ui·交互·产品经理·原型模式·web app
蓝速科技19 小时前
蓝速科技 AI 数字人部署与交互实战指南
人工智能·科技·交互
UXbot1 天前
原型设计工具如何帮助新人快速进入产品行业?
前端·低代码·ui·交互·团队开发·原型模式·web app
Resurgence_zc2 天前
openGauss 资源池化主备页面交互流程梳理
网络·交互·数据库开发
potion()2 天前
浏览器用户画像分析-大屏静态布局制作+数据接入+交互设置
交互·助睿数智·商业数据分析
LONGZETECH2 天前
无人机仿真教学软件选型实战:5 个硬核技术维度,避开实训建设踩坑
3d·无人机·交互·cocos2d
袖手蹲3 天前
K10 百炼 AI 语音助手从网络配置到全链路语音交互的嵌入式实战
网络·人工智能·交互
Z-D-K3 天前
S-44的周末”旅行“-周日
人工智能·ai·aigc·交互·agi