-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscull.c
302 lines (264 loc) · 6.36 KB
/
scull.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <asm/uaccess.h>
#include <linux/semaphore.h>
#include "scull.h"
//#define DEBUG
#define NUM 4
#define DEVNAME "scull"
//DEFINE_SEMAPHORE(sem);
dev_t scull_devnum;
uint scull_major,scull_minor;
struct scull_dev scull_dev;
static void scull_trim(struct scull_dev *dev)
{
int i;
struct scull_qset *dptr ,*next;
dptr = dev->data;
for (; dptr != NULL ;dptr = next) {
if (dptr->data) {
for (i = 0 ;i < dev->qset ;i++) {
kfree(dptr->data[i]);
}
kfree(dptr->data);
dptr->data = NULL;
}
next = dptr->next;
kfree(dptr);
}
dev->qset = SCULL_QSET;
dev->quantum = SCULL_QUANTUM;
dev->data = NULL;
dev->size = 0;
}
static struct scull_qset *scull_follow(struct scull_dev *dev,unsigned int item)
{
struct scull_qset *dptr = dev->data;
if (!dptr) {
dptr = dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
if (!dptr) {
printk(KERN_DEBUG "kmalloc failed\n");
return NULL;
}
memset(dptr, 0, sizeof(struct scull_qset));
}
while(item--) {
if (!dptr->next) {
dptr->next = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
if (!dptr->next) {
printk(KERN_DEBUG "kmalloc failed\n");
return NULL;
}
memset(dptr->data, 0, sizeof(struct scull_qset));
}
dptr = dptr->next;
continue;
}
return dptr;
}
#ifdef DEBUG
static void scull_mem_debug(struct scull_dev *dev)
{
int i;
struct scull_qset *dptr,*next;
for (dptr = dev->data; dptr ;dptr = next) {
if ((char *)dptr->data) {
for (i = 0; i < dev->qset ;i++) {
if ((char *)dptr->data[i]) {
printk(KERN_DEBUG "[i]: %s\n",(char *)dptr->data[i]);
}
}
}
next = dptr->next;
continue;
}
}
#endif
static int scull_open(struct inode *inode, struct file *filp)
{
struct scull_dev *dev;
dev = container_of(inode->i_cdev, struct scull_dev, cdev);
filp->private_data = dev;
if ((filp->f_flags & O_ACCMODE) == O_WRONLY) {
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
scull_trim(dev);
up(&dev->sem);
}
return 0;
}
static ssize_t scull_read(struct file *filp, char __user *buffer, size_t count, loff_t *f_ops)
{
struct scull_dev *dev;
struct scull_qset *dptr;
unsigned long itemsize;
unsigned int qset,quantum;
unsigned int s_pos,q_pos,reset,item;
ssize_t retval = 0;
dev = filp->private_data;
qset = dev->qset;
quantum = dev->quantum;
itemsize = qset * quantum;
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
if (*f_ops > dev->size) {
printk(KERN_DEBUG "can not read beyond data length\n");
goto out;
}
if (*f_ops + count > dev->size) {
count = dev->size - *f_ops;
}
item = *f_ops / itemsize;
reset = *f_ops % itemsize;
s_pos = reset / quantum;
q_pos = reset % quantum;
#ifdef DEBUG
scull_mem_debug(dev);
#endif
dptr = scull_follow(dev,item);
if (!dptr || !dptr->data || !dptr->data[s_pos]) {
goto out;
}
if (count > quantum - q_pos) {
count = quantum - q_pos;
}
if(copy_to_user(buffer, dptr->data[s_pos] + q_pos, count)) {
printk(KERN_DEBUG "copy_to_user failed\n");
goto out;
}
*f_ops += count;
retval = count;
out:
up(&dev->sem);
return retval;
}
static ssize_t scull_write(struct file *filp, const char __user *buffer, size_t count, loff_t *f_ops)
{
struct scull_dev *dev;
struct scull_qset *dptr;
unsigned long itemsize;
unsigned int qset,quantum;
unsigned int s_pos,q_pos,reset,item;
ssize_t retval = -ENOMEM;
dev = filp->private_data;
qset = dev->qset;
quantum = dev->quantum;
itemsize = qset * quantum;
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
item = *f_ops / itemsize;
reset = *f_ops % itemsize;
s_pos = reset / quantum;
q_pos = reset % quantum;
dptr = scull_follow(dev, item);
if (!dptr)
goto out;
if (!dptr->data) {
dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
if (!dptr->data)
goto out;
memset(dptr->data, 0, qset * sizeof(char *));
}
if (!dptr->data[s_pos]) {
dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
if (!dptr->data[s_pos])
goto out;
memset(dptr->data[s_pos], '\0', quantum);
}
if (count > quantum - q_pos)
count = quantum - q_pos;
if (copy_from_user(dptr->data[s_pos] + q_pos, buffer, count)) {
retval = -EFAULT;
goto out;
}
*f_ops += count;
retval = count;
if (dev->size < *f_ops)
dev->size = *f_ops;
#ifdef DEBUG
scull_mem_debug(dev);
#endif
out:
up(&dev->sem);
return retval;
}
static long scull_ioctl(struct file *filp, unsigned int command, unsigned long arg)
{
return (long)0;
}
static loff_t scull_llseek(struct file *filp, loff_t offset, int where)
{
return 0;
}
static int scull_release(struct inode *inode, struct file *filp)
{
return 0;
}
struct file_operations scull_ops = {
.owner = THIS_MODULE,
.open = scull_open,
.read = scull_read,
.write = scull_write,
.unlocked_ioctl = scull_ioctl,
.llseek = scull_llseek,
.release = scull_release,
};
static int scull_init_dev(void)
{
int ret;
if (scull_major) {
ret = register_chrdev_region(MKDEV(scull_major,0), 1, DEVNAME);
scull_devnum = MKDEV(scull_major,0);
} else {
ret = alloc_chrdev_region(&scull_devnum, 0, 1, DEVNAME);
scull_major = MAJOR(scull_devnum);
scull_minor = MINOR(scull_devnum);
}
if (ret)
printk(KERN_ERR "register device failed\n");
printk(KERN_DEBUG "register_chrdev_region success\n");
return ret;
}
static void scull_setup_dev(struct scull_dev *dev)
{
if (scull_init_dev()) {
printk(KERN_DEBUG "scull_init_dev failed\n");
unregister_chrdev_region(scull_devnum, 1);
}
dev->data = NULL;
dev->qset = SCULL_QSET;
dev->quantum = SCULL_QUANTUM;
dev->size = 0;
sema_init(&dev->sem, 1);
cdev_init(&dev->cdev, &scull_ops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &scull_ops;
if (cdev_add(&dev->cdev, scull_devnum, 1)) {
printk(KERN_DEBUG "cdev add failed\n");
unregister_chrdev_region(scull_devnum, 1);
}
printk(KERN_DEBUG "cdev_add success\n");
}
static int __init scull_init(void)
{
scull_major = SCULL_INIT;
scull_minor = SCULL_INIT;
memset(&scull_dev,0,sizeof(struct scull_dev));
scull_setup_dev(&scull_dev);
printk(KERN_DEBUG "scull driver init\n");
return 0;
}
static void __exit scull_exit(void)
{
scull_trim(&scull_dev);
cdev_del(&scull_dev.cdev);
unregister_chrdev_region(scull_devnum, 1);
printk(KERN_DEBUG "scull driver exit\n");
}
MODULE_AUTHOR("joker");
MODULE_LICENSE("GPL v2");
module_init(scull_init);
module_exit(scull_exit);