
在驱动开发中,通常会为每个设备创建一个设备结构体,用来存放该设备的硬件信息。本章将讲解设备结构体的工作原理以及如何处理文件中的私有数据。
一、文件私有数据简介
在 Linux 系统中,虽然没有强制要求使用文件私有数据,但驱动开发中广泛采用这一方法,这已成为一种常见做法。Linux 的 struct file 结构体中专门设置了一个字段(域),供驱动程序存储自己的私有数据。

在 Linux 设备驱动中,文件私有数据(private_data)是一个常用方法。它的作用是将设备结构体的地址保存到文件私有数据指针中,这样就能在打开设备时记录设备信息,并在后续的读写操作中直接使用这些数据。具体来说:
- open 函数中:将设备结构体的地址赋值给
private_data; - read/write 函数中:通过
private_data获取设备信息,执行具体操作。
这种方式让设备信息在驱动的不同函数之间传递变得简单直接。
c
struct device_test dev1;
static int cdev_test_open(struct inode *inode, struct file *file)
{
file->private_data = &dev1;
return 0;
}1
2
3
4
5
6
7
2
3
4
5
6
7
在代码中,我们首先创建了一个名为 dev1 的设备信息结构。当设备被打开时,我们把设备的私有数据指针设置为指向这个 dev1 结构。这样在后续的读操作和写操作中,就可以直接通过这个私有数据指针访问到设备的数据了。
c
static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off)
{
struct device_test *test_dev = (struct device_test *)file->private_data;
return 0;
}1
2
3
4
5
2
3
4
5
二、实验程序编写
源代码下载
bash
git clone git@gitee.com:yangxuesong314/linux-driver.git1
- 代码位于:
linux-driver/05.字符设备驱动模型/06.文件私有数据实验
c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
struct device_test {
dev_t dev_num;
int major;
int minor;
struct cdev cdev_test;
struct class *class;
struct device *device;
char kbuf[32];
};
struct device_test dev1;
static int cdev_test_open(struct inode *inode, struct file *file)
{
file->private_data = &dev1;
printk("This is cdev_test_open\r\n");
return 0;
}
static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off)
{
struct device_test *test_dev = (struct device_test *)file->private_data;
if (copy_from_user(test_dev->kbuf, buf, size) != 0) {
printk("copy_from_user error\r\n");
return -1;
}
printk("This is cdev_test_write\r\n");
printk("kbuf is %s\r\n", test_dev->kbuf); //打印kbuf的值
return 0;
}
static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off)
{
struct device_test *test_dev = (struct device_test *)file->private_data;
if (copy_to_user(buf, test_dev->kbuf, strlen(test_dev->kbuf)) != 0) {
printk("copy_to_user error\r\n");
return -1;
}
printk("This is cdev_test_read\r\n");
return 0;
}
static int cdev_test_release(struct inode *inode, struct file *file)
{
printk("This is cdev_test_release\r\n");
return 0;
}
struct file_operations cdev_test_fops = {
.owner = THIS_MODULE,
.open = cdev_test_open,
.read = cdev_test_read,
.write = cdev_test_write,
.release = cdev_test_release,
};
static int __init chr_fops_init(void)
{
int ret;
ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name");
if (ret < 0) {
printk("alloc_chrdev_region is error\n");
}
printk("alloc_chrdev_region is ok\n");
dev1.major = MAJOR(dev1.dev_num);
dev1.minor = MINOR(dev1.dev_num);
printk("major is %d \r\n", dev1.major); //打印主设备号
printk("minor is %d \r\n", dev1.minor); //打印次设备号
dev1.cdev_test.owner = THIS_MODULE;
cdev_init(&dev1.cdev_test, &cdev_test_fops);
cdev_add(&dev1.cdev_test, dev1.dev_num, 1);
dev1.class = class_create(THIS_MODULE, "test");
dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test");
return 0;
}
static void __exit chr_fops_exit(void)
{
unregister_chrdev_region(dev1.dev_num, 1);
cdev_del(&dev1.cdev_test);
device_destroy(dev1.class, dev1.dev_num);
class_destroy(dev1.class);
}
module_init(chr_fops_init);
module_exit(chr_fops_exit);
MODULE_LICENSE("GPL v2");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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
实验:
insmod file.ko
