0%

linux驱动框架总结

常用头文件

1
2
3
4
5
6
7
8
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/device.h>
......

字符型驱动基本框架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//常用宏定义、全局变量
#define DEV_NAME "xxx" //定义设备名称,Linux下可用ls /dev/查看

#define DEV_CNT (x) //设备数量

static dev_t devno; //字符设备的设备号
struct class *dev_class; //保存创建的类

// 保存创建的设备
//struct device *device_dev;

//设备树节点结构体
//struct device_node *dev_node;

static int dev_open(struct inode *inode,struct file *filp) //.open函数实现
{
...
return 0;
}

static ssize_t dev_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *off)//.write函数实现
{
...
...
return 0;
}

static ssize_t dev_read(struct file *filp,char __user *buf,size_t cnt, loff_t *off)//.read函数实现
{
...
...
return 0;
}

static int dev_release(struct inode *inode, struct file *filp)//.release函数实现
{
...
return 0;
}
//文件操作结构体
static struct file_operations chr_dev_fops = {
.owner = THIS_MODULE,
.open = dev_open,
.write = dev_write,
.read = dev_read,
.release = dev_release,
...
};

//驱动入口函数
static int __init driver_init(void)
{
......
return 0;
}

//驱动出口函数
static void __exit driver_exit(void)
{
......
}


module_init(driver_init);
module_exit(driver_exit);

MODULE_AUTHOR("xxx");
MODULE_LICENSE("GPL");
常用结构体、函数

平台驱动模型

……

-------------THE END-------------

欢迎关注我的其它发布渠道