-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleepy.c
360 lines (296 loc) · 9 KB
/
sleepy.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
/* cfake.c - implementation of a simple module for a character device
* can be used for testing, demonstrations, etc.
*/
/* ========================================================================
* Copyright (C) 2010-2011, Institute for System Programming
* of the Russian Academy of Sciences (ISPRAS)
* Authors:
* Eugene A. Shatokhin <spectre@ispras.ru>
* Andrey V. Tsyvarev <tsyvarev@ispras.ru>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
======================================================================== */
/* =========================================================================
======================================================================== */
#include <linux/version.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include "sleepy.h"
MODULE_AUTHOR("Eugene A. Shatokhin, John Regehr");
MODULE_LICENSE("GPL");
#define SLEEPY_DEVICE_NAME "sleepy"
/* parameters */
static int sleepy_ndevices = SLEEPY_NDEVICES;
static int flags[SLEEPY_NDEVICES];
static int sleeping[SLEEPY_NDEVICES];
static DECLARE_WAIT_QUEUE_HEAD(myQueue);
module_param(sleepy_ndevices, int, S_IRUGO);
/* ================================================================ */
static unsigned int sleepy_major = 0;
static struct sleepy_dev *sleepy_devices = NULL;
static struct class *sleepy_class = NULL;
/* ================================================================ */
int
sleepy_open(struct inode *inode, struct file *filp)
{
unsigned int mj = imajor(inode);
unsigned int mn = iminor(inode);
struct sleepy_dev *dev = NULL;
if (mj != sleepy_major || mn < 0 || mn >= sleepy_ndevices)
{
printk(KERN_WARNING "[target] "
"No device found with minor=%d and major=%d\n",
mj, mn);
return -ENODEV; /* No such device */
}
/* store a pointer to struct sleepy_dev here for other methods */
dev = &sleepy_devices[mn];
filp->private_data = dev;
if (inode->i_cdev != &dev->cdev)
{
printk(KERN_WARNING "[target] open: internal error\n");
return -ENODEV; /* No such device */
}
return 0;
}
int
sleepy_release(struct inode *inode, struct file *filp)
{
return 0;
}
ssize_t
sleepy_read(struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
struct sleepy_dev *dev = (struct sleepy_dev *)filp->private_data;
ssize_t retval = 0;
int devNum = iminor(filp->f_path.dentry->d_inode);
if (mutex_lock_killable(&dev->sleepy_mutex))
return -EINTR;
/* YOUR CODE HERE */
printk(KERN_DEBUG "Read from sleepy device %d\n", devNum);
flags[devNum] = 0;
wake_up_interruptible(&myQueue);
/* END YOUR CODE */
mutex_unlock(&dev->sleepy_mutex);
return retval;
}
// Condition function that acquires lock, checks flag, and releases lock
int myFlagSet(struct sleepy_dev * dev, int devNum)
{
mutex_lock_killable(&dev->sleepy_mutex);
int result = flags[devNum] == 0 ? 1 : 0;
mutex_unlock(&dev->sleepy_mutex);
return result;
}
ssize_t
sleepy_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct sleepy_dev *dev = (struct sleepy_dev *)filp->private_data;
ssize_t retval = 0;
int devNum = iminor(filp->f_path.dentry->d_inode);
char mybuf[5];
long delay = 0;
int result = 0;
if (mutex_lock_killable(&dev->sleepy_mutex))
return -EINTR;
/* YOUR CODE HERE */
// If argument length is not 4 bytes...
if (count != 4) {
printk(KERN_DEBUG "Invalid number of bytes. User entered %d bytes\n", (int) count);
mutex_unlock(&dev->sleepy_mutex);
return -EINVAL;
}
// Copy data from buffer
delay = (int) *buf;
// If argument is negative, don't sleep
if(delay < 0)
{
printk(KERN_DEBUG "Negative sleep value. No sleep\n");
mutex_unlock(&dev->sleepy_mutex);
return -EINVAL;
}
// If argument is positive, sleep for appropriate time
sleeping[devNum]++;
mutex_unlock(&dev->sleepy_mutex);
retval = wait_event_interruptible_timeout(myQueue, myFlagSet(dev, devNum), delay * HZ);
retval = retval / HZ; // Convert to seconds
mutex_lock_killable(&dev->sleepy_mutex);
// Reset flag if need be
sleeping[devNum]--;
if(sleeping[devNum] == 0)
{
flags[devNum] = -1;
}
if(retval != 0)
{
printk(KERN_DEBUG "Woken up with %d seconds remaining\n", retval);
}
else
{
printk(KERN_DEBUG "Woken up after full sleep\n");
}
/* END YOUR CODE */
mutex_unlock(&dev->sleepy_mutex);
return retval;
}
loff_t
sleepy_llseek(struct file *filp, loff_t off, int whence)
{
return 0;
}
struct file_operations sleepy_fops = {
.owner = THIS_MODULE,
.read = sleepy_read,
.write = sleepy_write,
.open = sleepy_open,
.release = sleepy_release,
.llseek = sleepy_llseek,
};
/* ================================================================ */
/* Setup and register the device with specific index (the index is also
* the minor number of the device).
* Device class should be created beforehand.
*/
static int
sleepy_construct_device(struct sleepy_dev *dev, int minor,
struct class *class)
{
int err = 0;
dev_t devno = MKDEV(sleepy_major, minor);
struct device *device = NULL;
BUG_ON(dev == NULL || class == NULL);
/* Memory is to be allocated when the device is opened the first time */
dev->data = NULL;
mutex_init(&dev->sleepy_mutex);
cdev_init(&dev->cdev, &sleepy_fops);
dev->cdev.owner = THIS_MODULE;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
{
printk(KERN_WARNING "[target] Error %d while trying to add %s%d",
err, SLEEPY_DEVICE_NAME, minor);
return err;
}
device = device_create(class, NULL, /* no parent device */
devno, NULL, /* no additional data */
SLEEPY_DEVICE_NAME "%d", minor);
if (IS_ERR(device)) {
err = PTR_ERR(device);
printk(KERN_WARNING "[target] Error %d while trying to create %s%d",
err, SLEEPY_DEVICE_NAME, minor);
cdev_del(&dev->cdev);
return err;
}
return 0;
}
/* Destroy the device and free its buffer */
static void
sleepy_destroy_device(struct sleepy_dev *dev, int minor,
struct class *class)
{
BUG_ON(dev == NULL || class == NULL);
device_destroy(class, MKDEV(sleepy_major, minor));
cdev_del(&dev->cdev);
kfree(dev->data);
return;
}
/* ================================================================ */
static void
sleepy_cleanup_module(int devices_to_destroy)
{
int i;
/* Get rid of character devices (if any exist) */
if (sleepy_devices) {
for (i = 0; i < devices_to_destroy; ++i) {
sleepy_destroy_device(&sleepy_devices[i], i, sleepy_class);
}
kfree(sleepy_devices);
}
if (sleepy_class)
class_destroy(sleepy_class);
/* [NB] sleepy_cleanup_module is never called if alloc_chrdev_region()
* has failed. */
unregister_chrdev_region(MKDEV(sleepy_major, 0), sleepy_ndevices);
return;
}
static int __init
sleepy_init_module(void)
{
int err = 0;
int i = 0;
int devices_to_destroy = 0;
dev_t dev = 0;
if (sleepy_ndevices <= 0)
{
printk(KERN_WARNING "[target] Invalid value of sleepy_ndevices: %d\n",
sleepy_ndevices);
err = -EINVAL;
return err;
}
/* Get a range of minor numbers (starting with 0) to work with */
err = alloc_chrdev_region(&dev, 0, sleepy_ndevices, SLEEPY_DEVICE_NAME);
if (err < 0) {
printk(KERN_WARNING "[target] alloc_chrdev_region() failed\n");
return err;
}
sleepy_major = MAJOR(dev);
/* Create device class (before allocation of the array of devices) */
sleepy_class = class_create(THIS_MODULE, SLEEPY_DEVICE_NAME);
if (IS_ERR(sleepy_class)) {
err = PTR_ERR(sleepy_class);
goto fail;
}
/* Allocate the array of devices */
sleepy_devices = (struct sleepy_dev *)kzalloc(
sleepy_ndevices * sizeof(struct sleepy_dev),
GFP_KERNEL);
if (sleepy_devices == NULL) {
err = -ENOMEM;
goto fail;
}
/* Construct devices */
for (i = 0; i < sleepy_ndevices; ++i) {
err = sleepy_construct_device(&sleepy_devices[i], i, sleepy_class);
if (err) {
devices_to_destroy = i;
goto fail;
}
}
/* Initialize flags and sleeping arrays */
for (i = 0; i < sleepy_ndevices; i++) {
flags[i] = -1;
sleeping[i] = 0;
}
printk ("sleepy module loaded\n");
return 0; /* success */
fail:
sleepy_cleanup_module(devices_to_destroy);
printk ("sleepy module failed to load\n");
return err;
}
static void __exit
sleepy_exit_module(void)
{
sleepy_cleanup_module(sleepy_ndevices);
printk ("sleepy module unloaded\n");
return;
}
module_init(sleepy_init_module);
module_exit(sleepy_exit_module);
/* ================================================================ */