【韦东山驱动入门实验班】如何写一个简单的驱动程序

1. 一切皆文件

  在linux系统中,无论是普通文件、设备文件,我们都是调用同一套接口:open、read、write、ioclt…

  那么,最简单的写驱动程序的办法就是在内核中实现驱动版本的open、read、write、ioctl…等操作函数

2. 写驱动程序的步骤

  1. 构造file_operations结构体,比如:struct file_operations fops;

 a. 在里面填充open/read/write/ioctl等成员函数;

/* 1. create file_operations */
static const struct file_operations hello_drv = {
    .owner      = THIS_MODULE,
	.read		= hello_read,
	.write		= hello_write,
	.open		= hello_open,
    .release    = hello_release,
};

 b.在结构体外实现这些成员函数,以hello_open为例:

static int hello_open (struct inode *node, struct file *filp)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}
  1. 注册file_operations结构体(告诉内核),这样我们就可以通过应用层的系统调用函数访问到驱动程序提供的接口;
  int major = register_chrdev(0, "name", &fops);//major主设备号,用来表示驱动程序相同的一类设备
  1. 入口函数:调用:register_chrdev

 调用的是第2步的注册函数,在入口函数中注册file_operations结构体

/* 2. register_chrdev */
/* 3. entry function */
static int hello_init(void)
{
   major = register_chrdev(0, "100ask_hello", &hello_drv);
   return 0;
}
  1. 出口函数:调用 unregister_chrdev

 出口函数中实现入口函数的反操作,在入口函数中注册了file_operations结构体,那么在出口函数就要反注册``file_operations```结构体

  1. 其他的辅助信息:

 a.class_create/class_destroy创建/销毁类;
 b.device_create/device_destroy创建/销毁设备节点;

3. hello驱动程序

根据上述的步骤,我们实现一个最简单的驱动程序:

#include <linux/mm.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/raw.h>
#include <linux/tty.h>
#include <linux/capability.h>
#include <linux/ptrace.h>
#include <linux/device.h>
#include <linux/highmem.h>
#include <linux/backing-dev.h>
#include <linux/shmem_fs.h>
#include <linux/splice.h>
#include <linux/pfn.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/uio.h>

#include <linux/uaccess.h>


static int major;

static int hello_open (struct inode *node, struct file *filp)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}
static ssize_t hello_read (struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return size;
}

static ssize_t hello_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return size;
}

static int hello_release (struct inode *node, struct file *filp)
{
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}

/* 1. create file_operations */
static const struct file_operations hello_drv = {
    .owner      = THIS_MODULE,
	.read		= hello_read,
	.write		= hello_write,
	.open		= hello_open,
    .release    = hello_release,
};


/* 2. register_chrdev */
/* 3. entry function */
static int hello_init(void)
{
   major = register_chrdev(0, "100ask_hello", &hello_drv);
   return 0;
}

/* 4. exit function */
static void hello_exit(void)
{
    unregister_chrdev(major, "100ask_hello");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

4. 测试

// 插入内核模块
insmod hello_drv.ko

//查看设备号,获得主设备号
cat /proc/devices

//添加设备节点
mknod /dev/xyz c 241 0
ls /dev/xyz

5. 小结

  1. 编写驱动时,选择相同的设备类型的内核源码进行参考。比如字符设备的驱动,在编写hello驱动程序的时候,就参考了Linux-5.4/drivers/char/mem.c
  2. 在编写驱动代码时,要善用工具。在Windos下,我们可以使用Source Insight。在Ubuntu环境中,我们可以搭建起vscode环境,并使用clangd插件和工作区实现源码跳转;
  3. 编写过程中,可以使用AI工具,比如:通义灵码插件、chatgpt等等;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cifeng79

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值