-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.c
167 lines (140 loc) · 3.36 KB
/
hello.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("hello_world");
MODULE_DESCRIPTION("A Simple Character Device driver module");
static struct cdev cdev;
static dev_t devno;
static char tmp[128] = "This is a message from kernel character driver!";
static int
hello_open(struct inode *inodep, struct file *filep)
{
printk(KERN_INFO "open\n");
return 0;
}
static int
hello_release(struct inode *inodep, struct file *filep)
{
printk(KERN_INFO "release\n");
return 0;
}
static ssize_t
hello_read(struct file *filep, char __user *buf, size_t count, loff_t *offset)
{
//
size_t avail;
printk(KERN_INFO "read\n");
avail = sizeof(tmp) - *offset;
if (count <= avail)
{
if (copy_to_user(buf, tmp + *offset, count) != 0)
{
return -EFAULT;
}
*offset += count;
return count;
}
else
{
if (copy_to_user(buf, tmp + *offset, avail) != 0)
{
return -EFAULT;
}
*offset += avail;
return avail;
}
}
static ssize_t
hello_write(struct file *filep, const char __user *buf, size_t count,
loff_t *offset)
{
size_t avail;
printk(KERN_INFO "write\n");
avail = sizeof(tmp) - *offset;
memset(tmp + *offset, 0, avail);
if (count > avail)
{
if (copy_from_user(tmp + *offset, buf, avail) != 0)
{
return -EFAULT;
}
*offset += avail;
return avail;
}
else
{
if (copy_from_user(tmp + *offset, buf, count) != 0)
{
return -EFAULT;
}
*offset += count;
return count;
}
}
static loff_t
hello_llseek(struct file *filep, loff_t off, int whence)
{
printk(KERN_INFO "llseek\n");
loff_t newpos;
switch (whence)
{
case 0: /* SEEK_SET */
newpos = off;
break;
case 1: /* SEEK_CUR */
newpos = filep->f_pos + off;
break;
case 2: /* SEEK_END */
newpos = sizeof(tmp) + off;
break;
default:
return -EINVAL;
}
if (newpos < 0)
{
return -EINVAL;
}
filep->f_pos = newpos;
return newpos;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = hello_open,
.release = hello_release,
.read = hello_read,
.llseek = hello_llseek,
.write = hello_write,
};
static int __init hello_init(void)
{
int ret;
printk(KERN_INFO "Load hello\n");
// devno = MKDEV(111, 0);
//向系统申请设备号
// if ((ret = register_chrdev_region(devno, 1, "hello")) < 0)
if((ret = alloc_chrdev_region(&devno, 0, 1, "hello")) < 0)
{
return ret;
}
//初始化cdev
cdev_init(&cdev, &fops);
cdev.owner = THIS_MODULE;
cdev.dev = devno;
//调用cdev_add将cdev加入系统
cdev_add(&cdev, devno, 1);
return 0;
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "cleanup hello\n");
unregister_chrdev_region(devno, 1);
cdev_del(&cdev);
}
module_init(hello_init);
module_exit(hello_cleanup);
MODULE_LICENSE("GPL");