一、前言
续前章:Linux驱动学习(一)
学习参考网址:
https://embetronicx.com/tutorials/linux/
二、创建/dev/目录下的文件
在使用insmod加载驱动后,应创建设备文件,以便系统能够访问此设备。
linux设备文件都存放在/dev/目录下。
例如:
1
2
crw--w---- 1 root tty 4, 0 Aug 15 10:40 tty0
brw-rw---- 1 root disk 1, 0 Aug 15 10:40 ram0
1、手动创建设备文件
可以通过mknod手动创建设备文件:
1
mknod -m <permissions> <name> <device type> <major> <minor>
-m <permissions
> – optional argument that sets the permission bits of the new device file to permissions
<name
> – your device file name that should have a full path (/dev/name
)
<device
* type
>* – Put **c or b
c – Character Device
b – Block Device
<major
> – major number of your driver
<minor
> – minor number of your driver
举个例子:
1
sudo mknod -m 666 /dev/etx_device c 246 0
可以通过rmmod进行删除设备文件
2、自动创建设备文件
例程:create_file.c
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
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/device.h>
dev_t dev = 0;
static struct class *dev_class;
/*Module init function*/
static int __init hello_world_init(void)
{
/*Automatically allocating Major number*/
if((alloc_chrdev_region(&dev, 0, 1, "new_Dev")) <0){
pr_err("Cannot allocate major number for device\n");
return -1;
}
pr_info("Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
/*Creating struct class*/
dev_class = class_create(THIS_MODULE,"new_class");
if(IS_ERR(dev_class)){
pr_err("Cannot create the struct class for device\n");
goto r_class;
}
/*Creating device*/
if(IS_ERR(device_create(dev_class,NULL,dev,NULL,"new_device"))){
pr_err("Cannot create the Device\n");
goto r_device;
}
pr_info("Kernel Module Inserted Successfully...\n");
return 0;
r_device:
class_destroy(dev_class);
r_class:
unregister_chrdev_region(dev,1);
return -1;
}
/*Module exit function*/
static void __exit hello_world_exit(void)
{
device_destroy(dev_class,dev);
class_destroy(dev_class);
unregister_chrdev_region(dev, 1);
pr_info("Kernel Module Removed Successfully...\n");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("Automatically Creating a Device file");
MODULE_VERSION("1:1.0");
进行make,然后insmod命令即可。
三、操作设备文件
创建设备文件后,用户需要操作这个文件,与设备进行通信。
那在这个文件中,就需要定义一组可以被调用的函数,完成通信任务。
例程:
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/cdev.h>
#include <linux/device.h>
dev_t dev = 0;
static struct class *dev_class;
static struct cdev etx_cdev;
/*
** Function Prototypes
*/
static int __init etx_driver_init(void);
static void __exit etx_driver_exit(void);
static int etx_open(struct inode *inode, struct file *file);
static int etx_release(struct inode *inode, struct file *file);
static ssize_t etx_read(struct file *filp, char __user *buf, size_t len,loff_t * off);
static ssize_t etx_write(struct file *filp, const char *buf, size_t len, loff_t * off);
static struct file_operations fops =
{
.owner = THIS_MODULE,
.read = etx_read,
.write = etx_write,
.open = etx_open,
.release = etx_release,
};
/*
** This function will be called when we open the Device file
*/
static int etx_open(struct inode *inode, struct file *file)
{
pr_info("Driver Open Function Called...!!!\n");
return 0;
}
/*
** This function will be called when we close the Device file
*/
static int etx_release(struct inode *inode, struct file *file)
{
pr_info("Driver Release Function Called...!!!\n");
return 0;
}
/*
** This function will be called when we read the Device file
*/
static ssize_t etx_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
pr_info("Driver Read Function Called...!!!\n");
return 0;
}
/*
** This function will be called when we write the Device file
*/
static ssize_t etx_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
pr_info("Driver Write Function Called...!!!\n");
return len;
}
/*
** Module Init function
*/
static int __init etx_driver_init(void)
{
/*Allocating Major number*/
if((alloc_chrdev_region(&dev, 0, 1, "operate_Dev")) <0){
pr_err("Cannot allocate major number\n");
return -1;
}
pr_info("Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
/*Creating cdev structure*/
cdev_init(&etx_cdev,&fops);
/*Adding character device to the system*/
if((cdev_add(&etx_cdev,dev,1)) < 0){
pr_err("Cannot add the device to the system\n");
goto r_class;
}
/*Creating struct class*/
if(IS_ERR(dev_class = class_create(THIS_MODULE,"operate_class"))){
pr_err("Cannot create the struct class\n");
goto r_class;
}
/*Creating device*/
if(IS_ERR(device_create(dev_class,NULL,dev,NULL,"operate_device"))){
pr_err("Cannot create the Device 1\n");
goto r_device;
}
pr_info("Device Driver Insert...Done!!!\n");
return 0;
r_device:
class_destroy(dev_class);
r_class:
unregister_chrdev_region(dev,1);
return -1;
}
/*
** Module exit function
*/
static void __exit etx_driver_exit(void)
{
device_destroy(dev_class,dev);
class_destroy(dev_class);
cdev_del(&etx_cdev);
unregister_chrdev_region(dev, 1);
pr_info("Device Driver Remove...Done!!!\n");
}
module_init(etx_driver_init);
module_exit(etx_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("File Operations");
MODULE_VERSION("1:1.3");
写makefile文件:
1
2
3
4
5
6
7
8
9
10
11
obj-m += operate_driver.o
KDIR = /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
load驱动文件:
1
sudo insmod
然后可以通过如下命令和刚刚写好的驱动程序进行通信:
1
sudo su
1
echo 1 > /dev/operate_device
四、实际创建一个可以读写的虚拟驱动
在完成上述所有步骤后,我们可以尝试自己创建一个驱动程序,此程序能够完成与用户态程序的通信,例如读写、打开关闭等操作。
在进行块数据的传输时,需要实现分配空间,并在用户态、内核态之间来回拷贝。
例如使用如下函数完成上述功能:
1
2
3
4
5
6
#include <linux/slab.h>
#include<linux/uaccess.h>
void *kmalloc(size_t size, gfp_t flags);
void kfree(const void *objp)
unsigned long copy_from_user(void *to, const void __user *from, unsigned long n);
unsigned long copy_to_user(const void __user *to, const void *from, unsigned long n);
我们利用如上函数,完成驱动程序:real_driver.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include<linux/slab.h> //kmalloc()
#include<linux/uaccess.h> //copy_to/from_user()
#include <linux/err.h>
#define mem_size 1024 //Memory Size
dev_t dev = 0;
static struct class *dev_class;
static struct cdev etx_cdev;
uint8_t *kernel_buffer;
/*
** Function Prototypes
*/
static int __init etx_driver_init(void);
static void __exit etx_driver_exit(void);
static int etx_open(struct inode *inode, struct file *file);
static int etx_release(struct inode *inode, struct file *file);
static ssize_t etx_read(struct file *filp, char __user *buf, size_t len,loff_t * off);
static ssize_t etx_write(struct file *filp, const char *buf, size_t len, loff_t * off);
/*
** File Operations structure
*/
static struct file_operations fops =
{
.owner = THIS_MODULE,
.read = etx_read,
.write = etx_write,
.open = etx_open,
.release = etx_release,
};
/*
** This function will be called when we open the Device file
*/
static int etx_open(struct inode *inode, struct file *file)
{
pr_info("Device File Opened...!!!\n");
return 0;
}
/*
** This function will be called when we close the Device file
*/
static int etx_release(struct inode *inode, struct file *file)
{
pr_info("Device File Closed...!!!\n");
return 0;
}
/*
** This function will be called when we read the Device file
*/
static ssize_t etx_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
//Copy the data from the kernel space to the user-space
if( copy_to_user(buf, kernel_buffer, mem_size) )
{
pr_err("Data Read : Err!\n");
}
pr_info("Data Read : Done!\n");
return mem_size;
}
/*
** This function will be called when we write the Device file
*/
static ssize_t etx_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
//Copy the data to kernel space from the user-space
if( copy_from_user(kernel_buffer, buf, len) )
{
pr_err("Data Write : Err!\n");
}
pr_info("Data Write : Done!\n");
return len;
}
/*
** Module Init function
*/
static int __init etx_driver_init(void)
{
/*Allocating Major number*/
if((alloc_chrdev_region(&dev, 0, 1, "real_driver_Dev")) <0){
pr_info("Cannot allocate major number\n");
return -1;
}
pr_info("Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
/*Creating cdev structure*/
cdev_init(&etx_cdev,&fops);
/*Adding character device to the system*/
if((cdev_add(&etx_cdev,dev,1)) < 0){
pr_info("Cannot add the device to the system\n");
goto r_class;
}
/*Creating struct class*/
if(IS_ERR(dev_class = class_create(THIS_MODULE,"real_driver_class"))){
pr_info("Cannot create the struct class\n");
goto r_class;
}
/*Creating device*/
if(IS_ERR(device_create(dev_class,NULL,dev,NULL,"real_driver_device"))){
pr_info("Cannot create the Device 1\n");
goto r_device;
}
/*Creating Physical memory*/
if((kernel_buffer = kmalloc(mem_size , GFP_KERNEL)) == 0){
pr_info("Cannot allocate memory in kernel\n");
goto r_device;
}
strcpy(kernel_buffer, "Hello_World");
pr_info("Device Driver Insert...Done!!!\n");
return 0;
r_device:
class_destroy(dev_class);
r_class:
unregister_chrdev_region(dev,1);
return -1;
}
/*
** Module exit function
*/
static void __exit etx_driver_exit(void)
{
kfree(kernel_buffer);
device_destroy(dev_class,dev);
class_destroy(dev_class);
cdev_del(&etx_cdev);
unregister_chrdev_region(dev, 1);
pr_info("Device Driver Remove...Done!!!\n");
}
module_init(etx_driver_init);
module_exit(etx_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("Real Linux Device Driver");
MODULE_VERSION("1:1.4");
编写Makefile:
1
2
3
4
5
6
7
8
9
10
obj-m += real_driver.o
KDIR = /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
随后利用如下命令进行load:
1
sudo insmod real_driver.ko
完成后,在用户空间编写如下C语言程序:test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int8_t write_buf[1024];
int8_t read_buf[1024];
int main()
{
int fd;
char option;
printf("*********************************\n");
fd = open("/dev/real_driver_device", O_RDWR);
if(fd < 0) {
printf("Cannot open device file...\n");
return 0;
}
while(1) {
printf("****Please Enter the Option******\n");
printf(" 1. Write \n");
printf(" 2. Read \n");
printf(" 3. Exit \n");
printf("*********************************\n");
scanf(" %c", &option);
printf("Your Option = %c\n", option);
switch(option) {
case '1':
printf("Enter the string to write into driver :");
scanf(" %[^\t\n]s", write_buf);
printf("Data Writing ...");
write(fd, write_buf, strlen(write_buf)+1);
printf("Done!\n");
break;
case '2':
printf("Data Reading ...");
read(fd, read_buf, 1024);
printf("Done!\n\n");
printf("Data = %s\n\n", read_buf);
break;
case '3':
close(fd);
exit(1);
break;
default:
printf("Enter Valid option = %c\n",option);
break;
}
}
close(fd);
}
随后进行编译:
1
gcc -o test test.c
运行test程序,即可查看结果。
卸载时,需要输入:
1
sudu rmmod read_driver