-
Notifications
You must be signed in to change notification settings - Fork 0
/
scullfifo.c
419 lines (386 loc) · 9.51 KB
/
scullfifo.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/sysfs.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
struct scullfifo_device {
wait_queue_head_t inq;
wait_queue_head_t outq;
struct mutex lock;
void *buf;
size_t rpos;
size_t wpos;
size_t bufsiz;
size_t alloc;
unsigned int readers;
unsigned int writers;
struct cdev cdev;
struct device base;
};
static struct scullfifo_driver {
size_t minimum_bufsiz;
size_t default_bufsiz;
size_t maximum_bufsiz;
dev_t devt;
struct file_operations fops;
struct device_type type;
struct device_driver base;
struct scullfifo_device devs[2];
} scullfifo_driver = {
.minimum_bufsiz = 1,
.default_bufsiz = PAGE_SIZE,
.maximum_bufsiz = PAGE_SIZE*2,
.base.name = "scullfifo",
.base.owner = THIS_MODULE,
};
static int is_empty(const struct scullfifo_device *const dev)
{
return dev->rpos == dev->wpos;
}
static int is_full(const struct scullfifo_device *const dev)
{
return (dev->wpos+1)%dev->bufsiz == dev->rpos;
}
static size_t datalen(const struct scullfifo_device *const dev)
{
if (is_full(dev))
return dev->bufsiz-1;
else if (is_empty(dev))
return 0;
else if (dev->wpos > dev->rpos)
return dev->wpos-dev->rpos;
else
return dev->bufsiz-dev->rpos+dev->wpos;
}
static size_t space(const struct scullfifo_device *const dev)
{
if (is_empty(dev))
return dev->bufsiz-1;
else if (is_full(dev))
return 0;
else if (dev->rpos > dev->wpos)
return dev->rpos-dev->wpos-1;
else
return dev->bufsiz-dev->wpos+dev->rpos-1;
}
static size_t allocsiz(const struct scullfifo_device *const dev)
{
/* dev->bufsiz should be greater than 0 */
return ((dev->bufsiz-1)/PAGE_SIZE+1)*PAGE_SIZE;
}
static ssize_t read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
{
struct scullfifo_device *dev = fp->private_data;
size_t ret, rem;
void *ptr;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
while (is_empty(dev)) {
if (dev->writers == 0) {
ret = 0;
goto out;
}
mutex_unlock(&dev->lock);
if (fp->f_flags&O_NONBLOCK)
return -EAGAIN;
ret = wait_event_interruptible(dev->inq, !is_empty(dev));
if (ret)
return ret;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
}
ret = datalen(dev);
if (ret > count)
ret = count;
/* no wrapped read */
if (dev->rpos+ret > dev->bufsiz)
ret = dev->bufsiz-dev->rpos;
ptr = dev->buf+dev->rpos;
rem = ret;
while (rem) {
rem = copy_to_user(buf, ptr, rem);
buf += ret-rem;
ptr += ret-rem;
}
dev->rpos = (dev->rpos+ret)%dev->bufsiz;
*pos += ret;
wake_up_interruptible(&dev->outq);
out:
mutex_unlock(&dev->lock);
return ret;
}
static ssize_t write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
{
struct scullfifo_device *dev = fp->private_data;
size_t ret, rem;
void *ptr;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
while (is_full(dev)) {
mutex_unlock(&dev->lock);
if (fp->f_flags&O_NONBLOCK)
return -EAGAIN;
ret = wait_event_interruptible(dev->outq, !is_full(dev));
if (ret)
return ret;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
}
ret = space(dev);
if (ret > count)
ret = count;
/* no wrapped write */
if (dev->wpos+ret > dev->bufsiz)
ret = dev->bufsiz-dev->wpos;
ptr = dev->buf+dev->wpos;
rem = ret;
while (rem) {
rem = copy_from_user(ptr, buf, rem);
ptr += ret-rem;
buf += ret-rem;
}
dev->wpos = (dev->wpos+ret)%dev->bufsiz;
*pos += ret;
wake_up_interruptible(&dev->inq);
mutex_unlock(&dev->lock);
return ret;
}
static int open(struct inode *ip, struct file *fp)
{
struct scullfifo_device *dev = container_of(ip->i_cdev,
struct scullfifo_device,
cdev);
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
fp->private_data = dev;
switch (fp->f_flags&O_ACCMODE) {
case O_RDWR:
dev->readers++;
/* fall through */
case O_WRONLY:
dev->writers++;
break;
default:
dev->readers++;
break;
}
mutex_unlock(&dev->lock);
return 0;
}
static int release(struct inode *ip, struct file *fp)
{
struct scullfifo_device *dev = fp->private_data;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
switch (fp->f_flags&O_ACCMODE) {
case O_RDWR:
dev->readers--;
/* fall through */
case O_WRONLY:
dev->writers--;
break;
default:
dev->readers--;
break;
}
mutex_unlock(&dev->lock);
return 0;
}
static ssize_t readers_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct scullfifo_device *dev = container_of(base,
struct scullfifo_device,
base);
unsigned int val;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
val = dev->readers;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%d\n", val);
}
static DEVICE_ATTR_RO(readers);
static ssize_t writers_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct scullfifo_device *dev = container_of(base,
struct scullfifo_device,
base);
unsigned int val;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
val = dev->writers;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%d\n", val);
}
static DEVICE_ATTR_RO(writers);
static ssize_t bufsiz_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct scullfifo_device *dev = container_of(base,
struct scullfifo_device,
base);
size_t val;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
val = dev->bufsiz;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%ld\n", val);
}
static ssize_t bufsiz_store(struct device *base, struct device_attribute *attr,
const char *page, size_t count)
{
struct scullfifo_device *dev = container_of(base,
struct scullfifo_device,
base);
struct scullfifo_driver *drv = dev_get_drvdata(base);
size_t alloc;
void *buf;
long val;
int err;
err = kstrtol(page, 10, &val);
if (err)
return err;
if (val < drv->minimum_bufsiz || val > drv->maximum_bufsiz)
return -EINVAL;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
if (dev->readers || dev->writers) {
err = -EPERM;
goto out;
}
dev->bufsiz = val;
dev->rpos = 0;
dev->wpos = 0;
err = count;
alloc = allocsiz(dev);
if (alloc <= dev->alloc)
goto out;
buf = krealloc(dev->buf, alloc, GFP_KERNEL);
if (IS_ERR(buf)) {
err = PTR_ERR(buf);
goto out;
}
dev->alloc = alloc;
dev->buf = buf;
out:
mutex_unlock(&dev->lock);
return err;
}
static DEVICE_ATTR_RW(bufsiz);
static ssize_t alloc_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct scullfifo_device *dev = container_of(base,
struct scullfifo_device,
base);
size_t val;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
val = dev->alloc;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%ld\n", val);
}
static DEVICE_ATTR_RO(alloc);
static struct attribute *scullfifo_attrs[] = {
&dev_attr_readers.attr,
&dev_attr_writers.attr,
&dev_attr_bufsiz.attr,
&dev_attr_alloc.attr,
NULL,
};
ATTRIBUTE_GROUPS(scullfifo);
static int __init init_driver(struct scullfifo_driver *drv)
{
int err;
err = alloc_chrdev_region(&drv->devt, 0, ARRAY_SIZE(drv->devs),
drv->base.name);
if (err)
return err;
memset(&drv->fops, 0, sizeof(struct file_operations));
drv->fops.owner = drv->base.owner;
drv->fops.read = read;
drv->fops.write = write;
drv->fops.open = open;
drv->fops.release = release;
return 0;
}
static int __init init(void)
{
struct scullfifo_driver *drv = &scullfifo_driver;
struct scullfifo_device *end = drv->devs+ARRAY_SIZE(drv->devs);
struct scullfifo_device *dev;
char name[11]; /* strlen(drv->base.name)+2 */
int i, err;
err = init_driver(drv);
if (err)
return err;
for (dev = drv->devs, i = 0; dev != end; dev++, i++) {
err = snprintf(name, sizeof(name), "%s%d", drv->base.name, i);
if (err < 0) {
end = dev;
goto err;
}
memset(dev, 0, sizeof(struct scullfifo_device));
device_initialize(&dev->base);
cdev_init(&dev->cdev, &drv->fops);
mutex_init(&dev->lock);
init_waitqueue_head(&dev->inq);
init_waitqueue_head(&dev->outq);
dev->bufsiz = drv->default_bufsiz;
dev->rpos = 0;
dev->wpos = 0;
dev->readers = 0;
dev->writers = 0;
dev->alloc = allocsiz(dev);
dev->buf = kmalloc(dev->alloc, GFP_KERNEL);
if (IS_ERR(dev->buf)) {
err = PTR_ERR(dev->buf);
end = dev;
goto err;
}
dev_set_drvdata(&dev->base, drv);
dev->base.init_name = name;
dev->base.groups = scullfifo_groups;
dev->base.devt = MKDEV(MAJOR(drv->devt),
MINOR(drv->devt)+i);
err = cdev_device_add(&dev->cdev, &dev->base);
if (err) {
kfree(dev->buf);
end = dev;
goto err;
}
}
return 0;
err:
for (dev = drv->devs; dev != end; dev++) {
cdev_device_del(&dev->cdev, &dev->base);
kfree(dev->buf);
}
unregister_chrdev_region(drv->devt, ARRAY_SIZE(drv->devs));
return err;
}
module_init(init);
static void __exit term(void)
{
struct scullfifo_driver *drv = &scullfifo_driver;
struct scullfifo_device *end = drv->devs+ARRAY_SIZE(drv->devs);
struct scullfifo_device *dev;
for (dev = drv->devs; dev != end; dev++) {
cdev_device_del(&dev->cdev, &dev->base);
kfree(dev->buf);
}
unregister_chrdev_region(drv->devt, ARRAY_SIZE(drv->devs));
}
module_exit(term);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kei Nohguchi <kei@nohguchi.com>");
MODULE_DESCRIPTION("Scull fifo device driver");