-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
88 lines (79 loc) · 2.33 KB
/
driver.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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/fs.h>
MODULE_LICENSE("Dual BSD/GPL");
struct runner_t{
int time; //stored in hundredths of seconds to avoid floats
struct list_head runners;
};
static int myopen(struct inode *, struct file *);
static int myclose (struct inode *, struct file *);
static ssize_t myread(struct file *, int *, size_t, loff_t *);
static ssize_t mywrite(struct file *, const int *,size_t, loff_t *);
void addRunner(int );
static struct file_operations fops = {
.open = myopen,
.release = myclose,
.read = myread,
.write = mywrite,
};
struct runner_t head;
void addRunner(int intime){
struct runner_t *newrunner = kmalloc(sizeof(struct runner_t), GFP_KERNEL);
newrunner->time = intime;
INIT_LIST_HEAD(&(newrunner->runners));
list_add_tail(&(newrunner->runners), &head.runners);
}
static int __init mod6_init(void){
int val = register_chrdev(65, "assignment6", &fops);
if(val < 0)
printk(KERN_ALERT "Registration of driver failed with error %d", val);
else
printk(KERN_ALERT "Device successfully registered");
INIT_LIST_HEAD(&(head.runners));
return val;
}
static int myclose(struct inode *mynode, struct file *myfile){
printk(KERN_INFO "closing driver");
return 0;
}
static int myopen(struct inode *mynode, struct file *myfile){
printk(KERN_INFO "driver opened");
return 0;
}
static void __exit mod6_exit(void){
struct runner_t *temp, *pos;
list_for_each_entry_safe(temp, pos, &(head.runners), runners){
list_del(&(temp->runners));
kfree(temp);
}
printk(KERN_INFO "removing driver");
unregister_chrdev(65, "assignment6");
}
static ssize_t myread(struct file *myfile, int *buf, size_t length, loff_t *offset){
struct runner_t *temp;
list_for_each_entry(temp, &head.runners, runners){
put_user(temp->time, buf++);
printk("value of int is %d\n", temp->time);
}
return length;
}
static ssize_t mywrite(struct file *myfile, const int *buf, size_t length, loff_t *offset){
if(*buf == 0){
struct runner_t *temp, *pos;
list_for_each_entry_safe(temp, pos, &(head.runners), runners){
list_del(&(temp->runners));
kfree(temp);
}
printk(KERN_INFO "cleared list");
}
else{
addRunner(*buf);
return 0;
}
}
module_init(mod6_init);
module_exit(mod6_exit);