forked from aakbar5/handy-kernel_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkthread_advanced.c
332 lines (278 loc) · 8.51 KB
/
kthread_advanced.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
// Create a character device and manipulation of kthread
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/sched.h>
enum state {
RUNNING = 0,
SUSPEND = 1,
STOP = 2,
SLEEP = 3,
RESUME = 4
};
// Structure having module related data
struct cddev_device {
unsigned minor;
unsigned count;
char name[50];
dev_t dev_num;
struct cdev cdev;
struct class *class;
struct device *device;
struct task_struct *task;
enum state task_state;
u64 time;
} cddev_info = {
.minor = 0,
.count = 1,
.name = "chdev"
};
// Callback to perform work in the context of kthread
static int kt_callback(void *data) {
struct cddev_device *info = (struct cddev_device *)data;
int show = 0;
pr_info("kt_callback -- START\n");
while (kthread_should_stop() == false) {
if (jiffies_to_msecs(jiffies_64) - jiffies_to_msecs(info->time) > (1000 * 3)) {
show = 1;
}
if (info->task_state == RUNNING) {
if (show == 1) pr_info("task_state -- running\n");
msleep_interruptible(2);
}
else if (info->task_state == SUSPEND) {
if (show == 1) pr_info("task_state -- suspend\n");
set_current_state(TASK_INTERRUPTIBLE);
schedule();
}
else if (info->task_state == STOP) {
if (show == 1) pr_info("task_state -- stop\n");
break;
}
else if (info->task_state == SLEEP) {
if (show == 1) pr_info("task_state -- sleep\n");
msleep_interruptible(500);
}
else {
pr_info("kt_callback -- unknown state\n");
}
if (show == 1) {
info->time = jiffies_64;
show = 0;
}
// pr_info("kt_callback -- Doing working (%lld)\n", jiffies_64);
}
pr_info("kt_callback -- END\n");
do_exit(0);
return 0;
}
static int cd_open(struct inode *inodep, struct file *filep) {
struct cddev_device *dev = container_of(inodep->i_cdev, struct cddev_device, cdev);
// pr_info("cd: open\n");
if (imajor(inodep) != MAJOR(dev->dev_num) || iminor(inodep) != MINOR(dev->dev_num)) {
pr_err("Unable to find device\n");
return -ENODEV;
}
filep->private_data = dev;
return 0;
}
static int cd_release(struct inode *inodep, struct file *filep) {
struct cddev_device *dev = container_of(inodep->i_cdev, struct cddev_device, cdev);
// pr_info("cd: release\n");
if (imajor(inodep) != MAJOR(dev->dev_num) || iminor(inodep) != MINOR(dev->dev_num)) {
pr_err("Unable to find device\n");
return -ENODEV;
}
return 0;
}
static int cd_flush(struct file *filep, fl_owner_t id) {
// pr_info("cd: flush\n");
(void)filep;
(void)id;
return 0;
}
static ssize_t cd_read(struct file *filep, char __user *buf, size_t count, loff_t *pos) {
char data[10];
size_t data_limit;
unsigned long ret;
size_t ipos = *pos;
struct cddev_device *dev = filep->private_data;
if (IS_ERR(dev)) {
pr_err("Unable to find private data\n");
return PTR_ERR(dev);
}
sprintf(data, "%d", dev->task_state);
data_limit = strlen(data);
pr_info("cd: read -- [data:%s][len:%ld]\n", data, data_limit);
if (ipos > data_limit)
return -EINVAL;
if (ipos + count > data_limit)
count = data_limit - ipos;
ret = copy_to_user(buf, data + ipos, count);
if (ret != 0) {
pr_err("Unable to copy data to user (%ld -- %ld)\n", ret, count);
return -EFAULT;
}
*pos += count;
return count;
}
static ssize_t cd_write(struct file *filep, const char __user *buf, size_t count, loff_t *pos) {
char data[10];
unsigned long ret;
long val;
struct cddev_device *dev = filep->private_data;
if (IS_ERR(dev)) {
pr_err("Unable to find private data\n");
return PTR_ERR(dev);
}
//pr_info("cd: write (count=%ld)\n", count);
ret = copy_from_user(data, buf, count);
if (ret != 0) {
pr_err("Unable to copy data from user (%ld -- %ld)\n", ret, count);
return -EFAULT;
}
ret = kstrtol(data, 10, &val);
if (ret != 0) {
pr_err("Unable to convert data\n");
return -EFAULT;
}
// pr_info("cd: write (val=%ld)\n", val);
switch(val)
{
case SUSPEND:
pr_info("task_state -- suspend\n");
dev->task_state = SUSPEND;
break;
case STOP:
pr_info("task_state -- stop\n");
dev->task_state = STOP;
break;
case SLEEP:
pr_info("task_state -- sleep\n");
dev->task_state = SLEEP;
break;
case RESUME:
pr_info("task_state -- resume\n");
ret = wake_up_process(cddev_info.task);
dev->task_state = RUNNING;
break;
case RUNNING:
default:
pr_info("task_state -- running\n");
dev->task_state = RUNNING;
break;
}
// pr_info("cd: write -- state = %d\n", dev->task_state);
return count;
}
//
// File operation implemented by the kernel module
//
static struct file_operations cddev_ops = {
.owner = THIS_MODULE,
.open = cd_open,
.release = cd_release,
.read = cd_read,
.write = cd_write,
.flush = cd_flush,
.llseek = default_llseek /* Use default llseek implementation */
};
//
// Module entry point
//
static int __init cd_init(void) {
int err = 0;
int ret;
pr_info("cd: init\n");
/* Step # 1: Reverse device major and minor number */
ret = alloc_chrdev_region(&cddev_info.dev_num, cddev_info.minor, cddev_info.count, cddev_info.name);
if (ret < 0) {
pr_err("Unable to allocate character device number\n");
err = ret;
goto err_err;
}
/* Step # 2: Create class for our device */
cddev_info.class = class_create(THIS_MODULE, cddev_info.name);
if (IS_ERR(cddev_info.class)) {
pr_err("Unable to create class\n");
err = PTR_ERR(cddev_info.class);
goto err_chrdev;
}
/* Step # 3: Create cdevice */
cdev_init(&cddev_info.cdev, &cddev_ops);
/* Step # 4: Setup ownership of the device */
cddev_info.cdev.owner = THIS_MODULE;
cddev_info.cdev.ops = &cddev_ops;
/* Step # 4: Add device to the system */
ret = cdev_add(&cddev_info.cdev, cddev_info.dev_num, cddev_info.count);
if (ret < 0) {
pr_err("Unable to add cdev\n");
err = ret;
goto err_class;
}
/* Step # 5: Create a device and register it with sysfs */
cddev_info.device = device_create(cddev_info.class, NULL, cddev_info.dev_num, NULL, cddev_info.name);
if (IS_ERR(cddev_info.device)) {
pr_err("Unable to create device\n");
err = PTR_ERR(cddev_info.device);
goto err_cdev;
}
pr_info("Setup kthread...\n");
cddev_info.task = kthread_create(kt_callback, &cddev_info, "kt_thread");
if (IS_ERR(cddev_info.task)) {
pr_err("Fail to create kthread\n");
ret = PTR_ERR(cddev_info.task);
goto err_device;
}
cddev_info.time = jiffies_64;
cddev_info.task_state = RUNNING;
// Above created kthread is not running yet
pr_info("Start kthread...\n");
ret = wake_up_process(cddev_info.task);
pr_err(" - Task: # %d\n", ret); // 1 means kthread is started; 0 means already running
pr_info("cd: device(/dev/%s) is create\n", cddev_info.name);
pr_info(" - Major number # %d\n", MAJOR(cddev_info.dev_num));
pr_info(" - Minor number # %d\n", MINOR(cddev_info.dev_num));
return 0;
err_device:
device_destroy(cddev_info.class, cddev_info.dev_num);
err_cdev:
cdev_del(&cddev_info.cdev);
err_class:
class_destroy(cddev_info.class);
err_chrdev:
unregister_chrdev_region(cddev_info.dev_num, cddev_info.count);
err_err:
return err;
}
//
// Module exit point
//
static void __exit cd_exit(void) {
pr_info("cd: exit\n");
if (cddev_info.task_state != STOP)
kthread_stop(cddev_info.task);
device_destroy(cddev_info.class, cddev_info.dev_num);
cdev_del(&cddev_info.cdev);
class_destroy(cddev_info.class);
unregister_chrdev_region(cddev_info.dev_num, cddev_info.count);
}
//
// Setup module entry and exit points
//
module_init(cd_init);
module_exit(cd_exit);
//
// Setup module info
//
MODULE_VERSION("0.0.1");
MODULE_DESCRIPTION("cd: A character device with kthread");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("aakbar5 <[email protected]>");