-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm510_dev.c
305 lines (258 loc) · 7.44 KB
/
dm510_dev.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
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/errno.h>
#include <linux/printk.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/kfifo.h>
#include <linux/rwsem.h>
/* This would normally go in a .h file */
#define DEVICE_NAME "dm510_dev"
#define DM510_MAJOR 255
#define BASE_MINOR 0
#define DEVICE_COUNT 2
#define MAX_WRITERS 1
#define DEFAULT_MAX_READERS 16
#define DEFAULT_BUFFER_SIZE 1024
#define IOCTL_GET_MAX_READERS _IOR(DM510_MAJOR, 0, ssize_t)
#define IOCTL_SET_MAX_READERS _IOW(DM510_MAJOR, 1, size_t)
#define IOCTL_GET_BUFFER_SIZE _IOR(DM510_MAJOR, 2, ssize_t)
#define IOCTL_SET_BUFFER_SIZE _IOW(DM510_MAJOR, 3, size_t)
#define IOCTL_MAX_NR 3
struct dm510_dev {
struct mutex mutex;
struct dm510_dev *buddy;
atomic_t nreaders, nwriters;
unsigned long max_readers;
struct kfifo kfifo;
wait_queue_head_t readq, writeq;
struct cdev cdev;
};
static int dm510_open(struct inode *, struct file *);
static int dm510_release(struct inode *, struct file *);
static ssize_t dm510_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t dm510_write(struct file *, const char __user *, size_t,
loff_t *);
static long dm510_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
/* end of what really should have been in a .h file */
static struct dm510_dev *dm510_devs = NULL;
static int dm510_open(struct inode *inode, struct file *filp)
{
struct dm510_dev *dev;
dev = container_of(inode->i_cdev, struct dm510_dev, cdev);
filp->private_data = dev;
pr_info("DM510: Opening DM510-%d.", MINOR(dev->cdev.dev));
if (filp->f_mode & FMODE_READ &&
atomic_fetch_inc(&dev->nreaders) >= dev->max_readers) {
atomic_dec(&dev->nreaders);
pr_info("DM510-%d: Too many readers.", MINOR(dev->cdev.dev));
return -EBUSY;
}
/* Concurrent writers can kindly not */
if (filp->f_mode & FMODE_WRITE &&
atomic_fetch_inc(&dev->nwriters) >= MAX_WRITERS) {
atomic_dec(&dev->nwriters);
pr_info("DM510-%d: Too many writers.", MINOR(dev->cdev.dev));
return -EBUSY;
}
/* We don't implement lseek, it doesn't make much sense here... */
return nonseekable_open(inode, filp);
}
static int dm510_release(struct inode *inode, struct file *filp)
{
struct dm510_dev *dev;
dev = filp->private_data;
pr_info("DM510: Closing DM510-%d.", MINOR(dev->cdev.dev));
if (filp->f_mode & FMODE_WRITE)
atomic_dec(&dev->nwriters);
if (filp->f_mode & FMODE_READ)
atomic_dec(&dev->nreaders);
return 0;
}
static ssize_t dm510_read(struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
struct dm510_dev *dev;
int ret;
unsigned int copied;
dev = filp->private_data;
if (mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
while (kfifo_is_empty(&dev->kfifo)) {
mutex_unlock(&dev->mutex);
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(dev->readq,
!kfifo_is_empty(&dev->kfifo)))
return -ERESTARTSYS;
if (mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
}
ret = kfifo_to_user(&dev->kfifo, buf, count, &copied);
mutex_unlock(&dev->mutex);
if (unlikely(ret))
return ret;
wake_up_interruptible(&dev->buddy->writeq);
return copied;
}
static ssize_t dm510_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos)
{
struct dm510_dev *dev, *buddy;
int ret;
unsigned int copied;
dev = filp->private_data;
buddy = dev->buddy;
if (mutex_lock_interruptible(&buddy->mutex))
return -ERESTARTSYS;
while (kfifo_is_full(&buddy->kfifo)) {
mutex_unlock(&buddy->mutex);
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(dev->writeq,
!kfifo_is_full(&buddy->kfifo)))
return -ERESTARTSYS;
if (mutex_lock_interruptible(&buddy->mutex))
return -ERESTARTSYS;
}
ret = kfifo_from_user(&buddy->kfifo, buf, count, &copied);
mutex_unlock(&buddy->mutex);
if (unlikely(ret))
return ret;
wake_up_interruptible(&buddy->readq);
return copied;
}
static long resize_kfifo(struct dm510_dev *dev, unsigned long buffer_size)
{
int ret;
struct kfifo new_kfifo;
ret = kfifo_alloc(&new_kfifo, buffer_size, GFP_KERNEL);
if (ret) {
pr_err("DM510: Resizing kfifo for DM510-%d failed with error code %d.\n",
MINOR(dev->cdev.dev), ret);
return ret;
}
/* We're already holding the lock */
kfifo_free(&dev->kfifo);
dev->kfifo = new_kfifo;
pr_info("DM510: Resized kfifo for DM510-%d.\n", MINOR(dev->cdev.dev));
return 0;
}
static long dm510_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct dm510_dev *dev;
long ret = 0;
dev = filp->private_data;
if (mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
switch (cmd) {
case IOCTL_GET_MAX_READERS:
pr_info("DM510: GET_MAX_READERS ioctl called.\n");
ret = dev->max_readers;
break;
case IOCTL_SET_MAX_READERS: {
pr_info("DM510: SET_MAX_READERS ioctl called.\n");
if (!capable(CAP_SYS_ADMIN)) {
ret = -EPERM;
break;
}
dev->max_readers = arg;
break;
}
case IOCTL_GET_BUFFER_SIZE:
pr_info("DM510: GET_BUFFER_SIZE ioctl called.\n");
ret = kfifo_size(&dev->kfifo);
break;
case IOCTL_SET_BUFFER_SIZE: {
pr_info("DM510: SET_BUFFER_SIZE ioctl called.\n");
if (!capable(CAP_SYS_ADMIN)) {
ret = -EPERM;
break;
}
ret = resize_kfifo(dev, arg);
break;
}
default:
pr_info("DM510: ioctl called with unknown command.\n");
ret = -ENOIOCTLCMD;
}
mutex_unlock(&dev->mutex);
return ret;
}
static struct file_operations dm510_fops = {
.owner = THIS_MODULE,
.read = dm510_read,
.write = dm510_write,
.open = dm510_open,
.release = dm510_release,
.unlocked_ioctl = dm510_ioctl,
.llseek = no_llseek,
};
static void dm510_exit(void)
{
if (dm510_devs) {
for (int i = 0; i < DEVICE_COUNT; i++) {
cdev_del(&dm510_devs[i].cdev);
kfifo_free(&dm510_devs[i].kfifo);
}
kfree(dm510_devs);
}
unregister_chrdev_region(MKDEV(DM510_MAJOR, BASE_MINOR), DEVICE_COUNT);
pr_info("DM510: Module unloaded.\n");
}
#define CHECK_ERR(exp) \
{ \
int __ret = exp; \
if (unlikely(__ret)) { \
dm510_exit(); \
return __ret; \
} \
}
static int __init dm510_init(void)
{
int ret;
ret = register_chrdev_region(MKDEV(DM510_MAJOR, BASE_MINOR),
DEVICE_COUNT, DEVICE_NAME);
if (ret)
return ret;
dm510_devs =
kmalloc(DEVICE_COUNT * sizeof(struct dm510_dev), GFP_KERNEL);
if (!dm510_devs) {
dm510_exit();
return -ENOMEM;
}
for (int i = 0; i < DEVICE_COUNT; i++) {
cdev_init(&dm510_devs[i].cdev, &dm510_fops);
dm510_devs[i].cdev.owner = THIS_MODULE;
dm510_devs[i].buddy = &dm510_devs[(i + 1) % DEVICE_COUNT];
dm510_devs[i].max_readers = DEFAULT_MAX_READERS;
atomic_set(&dm510_devs[i].nreaders, 0);
mutex_init(&dm510_devs[i].mutex);
init_waitqueue_head(&dm510_devs[i].readq);
init_waitqueue_head(&dm510_devs[i].writeq);
CHECK_ERR(kfifo_alloc(&dm510_devs[i].kfifo, DEFAULT_BUFFER_SIZE,
GFP_KERNEL));
}
/* Add cdevs *after* initializing everything at once because we depend on buddies */
for (int i = 0; i < DEVICE_COUNT; i++) {
CHECK_ERR(cdev_add(&dm510_devs[i].cdev,
MKDEV(DM510_MAJOR, BASE_MINOR + i), 1));
}
pr_info("DM510: Module installed.\n");
return 0;
}
module_init(dm510_init);
module_exit(dm510_exit);
MODULE_AUTHOR("...Frederik List");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("DM510 module");