二、新字符设备

字符设备驱动开发重点是使用 register_chrdev 函数注册字符设备,当不再使用设备的时候就使用unregister_chrdev 函数注销字符设备,驱动模块加载成功以后还需要手动使用 mknod 命令创建设备节点。register_chrdev 和 unregister_chrdev 这两个函数是老版本驱动使用的函数,现在新的字符设备驱动已经不再使用这两个函数,而是使用 Linux内核推荐的新字符设备驱动 API函数。本节我们就来学习一下如何编写新字符设备驱动,并且在驱动模块加载的时候自动创建设备节点文件。

一、新字符设备驱动原理
1.1分配和释放设备号

使用 register_chrdev 函数注册字符设备的时候只需要给定一个主设备号即可,但是这样会带来两个问题:
①、需要我们事先确定好哪些主设备号没有使用。
②、会将一个主设备号下的所有次设备号都使用掉,比如现在设置 LED 这个主设备号为
200,那么 0~1048575(2^20-1)这个区间的次设备号就全部都被 LED 一个设备分走了。这样太浪费次设备号了!一个 LED 设备肯定只能有一个主设备号,一个次设备号。解决这两个问题最好的方法就是要使用设备号的时候向 Linux 内核申请,需要几个就申请几个,由 Linux 内核分配设备可以使用的设备号。

如果没有指定设备号的话就使用如下函数来申请设备号:
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
如果给定了设备的主设备号和次设备号就使用如下所示函数来注册设备号即可:
int register_chrdev_region(dev_t from, unsigned count, const char *name)
统一使用如下释放函数:
void unregister_chrdev_region(dev_t from, unsigned count)

新字符设备驱动下,设备号分配示例代码如下:

1 int major; /* 主设备号 */
2 int minor; /* 次设备号 */
3 dev_t devid; /* 设备号 */
4 5
if (major) { /* 定义了主设备号 */
6 devid = MKDEV(major, 0); /* 大部分驱动次设备号都选择 0 */
7 register_chrdev_region(devid, 1, "test");
8 } else { /* 没有定义设备号 */
9 alloc_chrdev_region(&devid, 0, 1, "test"); /* 申请设备号 */
10 major = MAJOR(devid); /* 获取分配号的主设备号 */
11 minor = MINOR(devid); /* 获取分配号的次设备号 */
12 }
1.2 新的字符设备注册方法
1.2.1 字符设备结构

在 Linux 中使用 cdev 结构体表示一个字符设备, cdev 结构体在 include/linux/cdev.h 文件中的定义如下:

1 struct cdev {
2 struct kobject kobj;
3 struct module *owner;
4 const struct file_operations *ops;
5 struct list_head list;
6 dev_t dev;
7 unsigned int count;
8 };

在 cdev 中有两个重要的成员变量: ops 和 dev,这两个就是字符设备文件操作函数集合
file_operations 以及设备号 dev_t。编写字符设备驱动之前需要定义一个 cdev 结构体变量.

1.2.2 cdev_init 函数

定义好 cdev 变量以后就要使用 cdev_init 函数对其进行初始化, cdev_init 函数原型如下:

void cdev_init(struct cdev *cdev, const struct file_operations *fops)

参数 cdev 就是要初始化的 cdev 结构体变量,参数 fops 就是字符设备文件操作函数集合。
使用 cdev_init 函数初始化 cdev 变量的示例代码如下:

1 struct cdev testcdev;
2
 3
/* 设备操作函数 */
4 static struct file_operations test_fops = {
5 .owner = THIS_MODULE,
6 /* 其他具体的初始项 */
7 };
8 
9testcdev.owner = THIS_MODULE;
10 cdev_init(&testcdev, &test_fops); /* 初始化 cdev 结构体变量 */
1.2.3 cdev_add 函数

cdev_add 函数用于向 Linux 系统添加字符设备(cdev 结构体变量),首先使用 cdev_init 函数完成对 cdev 结构体变量的初始化,然后使用 cdev_add 函数向 Linux 系统添加这个字符设备。
cdev_add 函数原型如下:

int cdev_add(struct cdev *p, dev_t dev, unsigned count)
参数 p 指向要添加的字符设备(cdev 结构体变量),参数 dev 就是设备所使用的设备号,参
数 count 是要添加的设备数量。
1 struct cdev testcdev;
2 3
/* 设备操作函数 */
4 static struct file_operations test_fops = {
5 .owner = THIS_MODULE,
6 /* 其他具体的初始项 */
7 };
8
9testcdev.owner = THIS_MODULE;
10 cdev_init(&testcdev, &test_fops); /* 初始化 cdev 结构体变量 */
11 cdev_add(&testcdev, devid, 1); /* 添加字符设备 */

示例代码就是新的注册字符设备代码段, Linux 内核中大量的字符设备驱动都是采用这种方法向 Linux 内核添加字符设备。如果在加上配设备号的程序,那么就它们一起实现的就是函数 register_chrdev 的功能。

1.2.3 cdev_del 函数

卸载驱动的时候一定要使用 cdev_del 函数从 Linux 内核中删除相应的字符设备, cdev_del函数原型如下:

void cdev_del(struct cdev *p)

参数 p 就是要删除的字符设备。如果要删除字符设备,参考如下代码:

cdev_del(&testcdev); /* 删除 cdev */

cdev_del 和 unregister_chrdev_region 这两个函数合起来的功能相当于 unregister_chrdev 函数。

二、自动创建设备节点

在前面的 Linux 驱动实验中,当我们使用 modprobe 加载驱动程序以后还需要使用命令
“mknod”手动创建设备节点。本节就来讲解一下如何实现自动创建设备节点,在驱动中实现自动创建设备节点的功能以后,使用 modprobe 加载驱动模块成功的话就会自动在/dev 目录下创建对应的设备文件。

2.1 mdev机制

udev 是一个用户程序,在 Linux 下通过 udev 来实现设备文件的创建与删除, udev 可以检测系统中硬件设备状态,可以根据系统中硬件设备状态来创建或者删除设备文件。比如使用modprobe 命令成功加载驱动模块以后就自动在/dev 目录下创建对应的设备节点文件,使用rmmod 命令卸载驱动模块以后就删除掉/dev 目录下的设备节点文件。 使用 busybox 构建根文件系统的时候, busybox 会创建一个 udev 的简化版本—mdev,所以在嵌入式 Linux 中我们使用mdev 来实现设备节点文件的自动创建与删除。

2.1.1创建和删除类

自动创建设备节点的工作是在驱动程序的入口函数中完成的,一般在 cdev_add 函数后面添加自动创建设备节点相关代码。首先要创建一个 class 类, class 是个结构体,定义在文件include/linux/device.h 里面。 class_create 是类创建函数, class_create 是个宏定义,内容如下:

1 #define class_create(owner, name) \
2 ({ \
3 static struct lock_class_key __key; \
4 __class_create(owner, name, &__key); \
5 })
6 7
struct class *__class_create(struct module *owner, const char *name,
8 struct lock_class_key *key)

根据上述代码,将宏 class_create 展开以后内容如下:
struct class *class_create (struct module *owner, const char *name)
class_create 一共有两个参数,参数 owner 一般为 THIS_MODULE,参数 name 是类名字。返回值是个指向结构体 class 的指针,也就是创建的类。
卸载驱动程序的时候需要删除掉类,类删除函数为 class_destroy,函数原型如下:
void class_destroy(struct class *cls);
参数 cls 就是要删除的类。

42.2.2 创建设备

上一小节创建好类以后还不能实现自动创建设备节点,我们还需要在这个类下创建一个设
备。使用 device_create 函数在类下面创建设备, device_create 函数原型如下:

struct device *device_create(struct class *class,
struct device *parent,
dev_t devt,
void *drvdata,
const char *fmt, ...)

同样的,卸载驱动的时候需要删除掉创建的设备,设备删除函数为 device_destroy,函数原型如下:

void device_destroy(struct class *class, dev_t devt)
2.2.3 参考示例

在驱动入口函数里面创建类和设备,在驱动出口函数里面删除类和设备,参考示例如下:

1 struct class *class; /* 类 */
2 struct device *device; /* 设备 */
3 dev_t devid; /* 设备号 */
4 5
/* 驱动入口函数 */
6 static int __init xxx_init(void)
7 {
8 /* 创建类 */
9 class = class_create(THIS_MODULE, "xxx");
10 /* 创建设备 */
11 device = device_create(class, NULL, devid, NULL, "xxx");
12 return 0;
13 }
14
15 /* 驱动出口函数 */
16 static void __exit led_exit(void)
17 {
18 /* 删除设备 */
19 device_destroy(newchrled.class, newchrled.devid);
20 /* 删除类 */
21 class_destroy(newchrled.class);
22 }
23
24 module_init(led_init);
25 module_exit(led_exit);
三、设置文件私有数据

每个硬件设备都有一些属性,比如主设备号(dev_t),类(class)、设备(device)、开关状态(state)
等等,在编写驱动的时候你可以将这些属性全部写成变量的形式,如下所示:
示例代码 42.3.1 变量形式的设备属性
dev_t devid; /* 设备号 /
struct cdev cdev; /
cdev */
struct class class; / 类 */
struct device device; / 设备 /
int major; /
主设备号 /
int minor; /
次设备号 */
这样写肯定没有问题,但是这样写不专业!对于一个设备的所有属性信息我们最好将其做
成一个结构体。编写驱动 open 函数的时候将设备结构体作为私有数据添加到设备文件中,如下所示:

/* 设备结构体 */
1 struct test_dev{
2 dev_t devid; /* 设备号 */
3 struct cdev cdev; /* cdev */
4 struct class *class; /* 类 */
5 struct device *device; /* 设备 */
6 int major; /* 主设备号 */
7 int minor; /* 次设备号 */
8 };
9
10 struct test_dev testdev;
11
12 /* open 函数 */
13 static int test_open(struct inode *inode, struct file *filp)
14 {
15 filp->private_data = &testdev; /* 设置私有数据 */
16 return 0;
17 }

在 open 函数里面设置好私有数据以后,在 write、 read、 close 等函数中直接读取 private_data即可得到设备结构体。

四、实验程序编写
4.1 LED 灯驱动程序编写
1 #include <linux/types.h>
2 #include <linux/kernel.h>
3 #include <linux/delay.h>
4 #include <linux/ide.h>
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/errno.h>
8 #include <linux/gpio.h>
9 #include <linux/cdev.h>
10 #include <linux/device.h>
11 #include <asm/mach/map.h>
12 #include <asm/uaccess.h>
13 #include <asm/io.h>
25 #define NEWCHRLED_CNT 1 /* 设备号个数 */
26 #define NEWCHRLED_NAME "newchrled" /* 名字 */
27 #define LEDOFF 0 /* 关灯 */
28 #define LEDON 1 /* 开灯 */
29
30 /* 寄存器物理地址 */
31 #define CCM_CCGR1_BASE (0X020C406C)
32 #define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
33 #define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
34 #define GPIO1_DR_BASE (0X0209C000)
35 #define GPIO1_GDIR_BASE (0X0209C004)
36
37 /* 映射后的寄存器虚拟地址指针 */
38 static void __iomem *IMX6U_CCM_CCGR1;
39 static void __iomem *SW_MUX_GPIO1_IO03;
40 static void __iomem *SW_PAD_GPIO1_IO03;
41 static void __iomem *GPIO1_DR;
42 static void __iomem *GPIO1_GDIR;
43
44 /* newchrled 设备结构体 */
45 struct newchrled_dev{
46 dev_t devid; /* 设备号 */
47 struct cdev cdev; /* cdev */
48 struct class *class; /* 类 */
49 struct device *device; /* 设备 */
50 int major; /* 主设备号 */
51 int minor; /* 次设备号 */
52 };
53
54 struct newchrled_dev newchrled; /* led 设备 */
55
56 /*
57 * @description : LED 打开/关闭
58 * @param - sta : LEDON(0) 打开 LED, LEDOFF(1) 关闭 LED
59 * @return : 无
60 */
61 void led_switch(u8 sta)
62 {
63 u32 val = 0;
64 if(sta == LEDON) {
65 val = readl(GPIO1_DR);
66 val &= ~(1 << 3);
67 writel(val, GPIO1_DR);
68 }else if(sta == LEDOFF) {
69 val = readl(GPIO1_DR);
70 val|= (1 << 3);
71 writel(val, GPIO1_DR);
72 }
73 }
74
75 /*
76 * @description : 打开设备
77 * @param – inode : 传递给驱动的 inode
78 * @param - filp : 设备文件, file 结构体有个叫做 private_data 的成员变量
79 * 一般在 open 的时候将 private_data 指向设备结构体。
80 * @return : 0 成功;其他 失败
81 */
82 static int led_open(struct inode *inode, struct file *filp)
83 {
84 filp->private_data = &newchrled; /* 设置私有数据 */
85 return 0;
86 }
87
88 /*
89 * @description : 从设备读取数据
90 * @param - filp : 要打开的设备文件(文件描述符)
91 * @param - buf : 返回给用户空间的数据缓冲区
92 * @param - cnt : 要读取的数据长度
93 * @param – offt : 相对于文件首地址的偏移
94 * @return : 读取的字节数,如果为负值,表示读取失败
95 */
96 static ssize_t led_read(struct file *filp, char __user *buf,
size_t cnt, loff_t *offt)
97 {
98 return 0;
99 }
100
101 /*
102 * @description : 向设备写数据
103 * @param – filp : 设备文件,表示打开的文件描述符
104 * @param - buf : 要写给设备写入的数据
105 * @param - cnt : 要写入的数据长度
106 * @param – offt : 相对于文件首地址的偏移
107 * @return : 写入的字节数,如果为负值,表示写入失败
108 */
109 static ssize_t led_write(struct file *filp, const char __user *buf,
size_t cnt, loff_t *offt)
110 {
111 int retvalue;
112 unsigned char databuf[1];
113 unsigned char ledstat;
114
115 retvalue = copy_from_user(databuf, buf, cnt);
116 if(retvalue < 0) {
117 printk("kernel write failed!\r\n");
118 return -EFAULT;
119 }
120
121 ledstat = databuf[0]; /* 获取状态值 */
122
123 if(ledstat == LEDON) {
124 led_switch(LEDON); /* 打开 LED 灯 */
125 } else if(ledstat == LEDOFF) {
126 led_switch(LEDOFF); /* 关闭 LED 灯 */
127 }
128 return 0;
129 }
130
131 /*
132 * @description : 关闭/释放设备
133 * @param – filp : 要关闭的设备文件(文件描述符)
134 * @return : 0 成功;其他 失败
135 */
136 static int led_release(struct inode *inode, struct file *filp)
137 {
138 return 0;
139 }
140
141 /* 设备操作函数 */
142 static struct file_operations newchrled_fops = {
143 .owner = THIS_MODULE,
144 .open = led_open,
145 .read = led_read,
146 .write = led_write,
147 .release = led_release,
148 };
149
150 /*
151 * @description : 驱动出口函数
152 * @param : 无
153 * @return : 无
154 */
155 static int __init led_init(void)
156 {
157 u32 val = 0;
158
159 /* 初始化 LED */
160 /* 1、寄存器地址映射 */
161 IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
162 SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
163 SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);
164 GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
165 GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);
166
167 /* 2、使能 GPIO1 时钟 */
168 val = readl(IMX6U_CCM_CCGR1);
169 val &= ~(3 << 26); /* 清楚以前的设置 */
170 val |= (3 << 26); /* 设置新值 */
171 writel(val, IMX6U_CCM_CCGR1);
172
173 /* 3、设置 GPIO1_IO03 的复用功能,将其复用为
174 * GPIO1_IO03,最后设置 IO 属性。
175 */
176 writel(5, SW_MUX_GPIO1_IO03);
177
178 /* 寄存器 SW_PAD_GPIO1_IO03 设置 IO 属性 */
179 writel(0x10B0, SW_PAD_GPIO1_IO03);
180
181 /* 4、设置 GPIO1_IO03 为输出功能 */
182 val = readl(GPIO1_GDIR);
183 val &= ~(1 << 3); /* 清除以前的设置 */
184 val |= (1 << 3); /* 设置为输出 */
185 writel(val, GPIO1_GDIR);
186
187 /* 5、默认关闭 LED */
188 val = readl(GPIO1_DR);
189 val |= (1 << 3);
190 writel(val, GPIO1_DR);
191
192 /* 注册字符设备驱动 */
193 /* 1、创建设备号 */
194 if (newchrled.major) { /* 定义了设备号 */
195 newchrled.devid = MKDEV(newchrled.major, 0);
196 register_chrdev_region(newchrled.devid, NEWCHRLED_CNT,
NEWCHRLED_NAME);
197 } else { /* 没有定义设备号 */
198 alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_CNT,
NEWCHRLED_NAME); /* 申请设备号 */
199 newchrled.major = MAJOR(newchrled.devid); /* 获取主设备号 */
200 newchrled.minor = MINOR(newchrled.devid); /* 获取次设备号 */
201 }
202 printk("newcheled major=%d,minor=%d\r\n",newchrled.major,
newchrled.minor);
203
204 /* 2、初始化 cdev */
205 newchrled.cdev.owner = THIS_MODULE;
206 cdev_init(&newchrled.cdev, &newchrled_fops);
207
208 /* 3、添加一个 cdev */
209 cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_CNT);
210
211 /* 4、创建类 */
212 newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);
213 if (IS_ERR(newchrled.class)) {
214 return PTR_ERR(newchrled.class);
215 }
216
217 /* 5、创建设备 */
218 newchrled.device = device_create(newchrled.class, NULL,
newchrled.devid, NULL, NEWCHRLED_NAME);
219 if (IS_ERR(newchrled.device)) {
220 return PTR_ERR(newchrled.device);
221 }
222
223 return 0;
224 }
225
226 /*
227 * @description : 驱动出口函数
228 * @param : 无
229 * @return : 无
230 */
231 static void __exit led_exit(void)
232 {
233 /* 取消映射 */
234 iounmap(IMX6U_CCM_CCGR1);
235 iounmap(SW_MUX_GPIO1_IO03);
236 iounmap(SW_PAD_GPIO1_IO03);
237 iounmap(GPIO1_DR);
238 iounmap(GPIO1_GDIR);
239
240 /* 注销字符设备 */
241 cdev_del(&newchrled.cdev);/* 删除 cdev */
242 unregister_chrdev_region(newchrled.devid, NEWCHRLED_CNT);
243
244 device_destroy(newchrled.class, newchrled.devid);
245 class_destroy(newchrled.class);
246 }
247
248 module_init(led_init);
249 module_exit(led_exit);
250 MODULE_LICENSE("GPL");
251 MODULE_AUTHOR("zuozhongkai");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值