-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokemon.c
221 lines (203 loc) · 8.55 KB
/
pokemon.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
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define DEVICE_NAME "pokemon"
#define SONG_LENGTH 87
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jarred Allen");
MODULE_DESCRIPTION("A device for printing the lyrics to the Pokemon theme song");
MODULE_VERSION("0.1");
static int dev_open(struct inode*, struct file*);
static int dev_release(struct inode*, struct file*);
static ssize_t dev_read(struct file*, char*, size_t, loff_t*);
static ssize_t dev_write(struct file*, const char*, size_t, loff_t*);
char* messages[SONG_LENGTH] = {"I want to be the very best,\n",
"Like no one ever was.\n",
"To catch them is my real test,\n",
"To train them is my cause!\n",
"\n",
"I will travel across the land,\n",
"Searching far and wide.\n",
"Each Pokemon to understand,\n",
"The power that's inside!\n",
"\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"\n",
"It's you and me,\n",
"I know it's my destiny!\n",
"\n",
"Pokemon!\n",
"Oh, you're my best friend.\n",
"In a world we must defend!\n",
"\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"A heart so true,\n",
"Our courage will pull us through!\n",
"You teach me and I'll teach you,\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
"Every challenge along the way,\n",
"With courage I will face!\n",
"I will battle every day,\n",
"To claim my rightful place!\n",
"\n",
"Come with me, the time is right,\n",
"There's no better team!\n",
"Arm in arm, we'll win the fight,\n",
"It's always been our dream!\n",
"\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"It's you and me,\n",
"I know it's my destiny!\n",
"\n",
"Pokemon!\n",
"Oh, you're my best friend.\n",
"In a world we must defend!\n",
"\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"A heart so true,\n",
"Our courage will pull us through!\n",
"You teach me and I'll teach you,\n",
"Pokemon!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
"Yeah",
"\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"It's you and me,\n",
"I know it's my destiny!\n",
"\n",
"Pokemon!\n",
"Oh, you're my best friend.\n",
"In a world we must defend!\n",
"\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"A heart so true,\n",
"Our courage will pull us through!\n",
"You teach me and I'll teach you,\n",
"Pokemon!\n",
"Gotta catch 'em all!\n",
"\n",
"Gotta catch 'em all!\n",
"\n",
};
int delays[SONG_LENGTH] = {7, 3, 3, 3, 2, 3, 2, 4, 2, 2, 3, 1, 0, 1, 2, 0, 1,
1, 3, 1, 1, 1, 2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 4, 3,
2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 2, 1,
1, 3, 3, 3, 1, 1, 1, 1, 1, 2, 4, 5, 3, 4, 2, 2, 4,
19, 1, 1, 2, 1, 1, 1, 3, 1, 2, 1, 1, 2, 4, 2, 2, 1,
1, 1};
static struct file_operations fops = {
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
static int major;
static struct class *myclass;
static struct device *mydevice;
static struct cdev mycdev;
static int chmod_uevent(struct device *dev, struct kobj_uevent_env *env) {
add_uevent_var(env, "DEVMODE=%#o", 0444);
return 0;
}
static int __init pokemon_init(void) {
major = register_chrdev(0, DEVICE_NAME, &fops);
if (major < 0) {
printk(KERN_INFO "Pokemon failed at register_chrdev.\n");
return major;
}
myclass = class_create(THIS_MODULE, DEVICE_NAME);
if (myclass == NULL) {
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "Pokemon failed at class_create.\n.");
return -1;
}
myclass->dev_uevent = chmod_uevent;
cdev_init(&mycdev, &fops);
if (cdev_add(&mycdev, major, 1) < 0) {
device_destroy(myclass, major);
class_destroy(myclass);
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "Pokemon failed at cdev_add.\n");
return -1;
}
mydevice = device_create(myclass, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
if (mydevice == NULL) {
class_destroy(myclass);
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "Pokemon failed at device_create.\n");
return -1;
}
printk(KERN_INFO "Pokemon module has been loaded: %d\n", major);
return 0;
}
static void __exit pokemon_exit(void) {
device_destroy(myclass, MKDEV(major, 0));
cdev_del(&mycdev);
class_destroy(myclass);
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "Pokemon module has been unloaded.\n");
}
static int dev_open(struct inode *inodep, struct file *filep) {
filep->private_data = (void*) kmalloc(sizeof(int), GFP_KERNEL);
if (!filep->private_data) {
printk(KERN_INFO "Error in allocating memory for pokemon device");
return -ENOMEM;
}
*((int*)filep->private_data) = 0;
printk(KERN_INFO "Pokemon device opened.\n");
return 0;
}
static ssize_t dev_write(struct file *filep, const char *buffer,
size_t len, loff_t *offset) {
printk(KERN_INFO "The pokemon song is already perfect. No changes needed.");
return -EFAULT;
}
static int dev_release(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Pokemon device closed.\n");
kfree(filep->private_data);
return 0;
}
static ssize_t dev_read(struct file* filep, char* buffer,
size_t len, loff_t* offset) {
int* pos = (int*) filep->private_data;
if (*pos == SONG_LENGTH) {
return 0;
}
else {
int errors = 0;
const char* message = messages[*pos];
int message_len = strlen(message);
msleep(delays[*pos]*1000);
errors = copy_to_user(buffer, message, message_len);
(*pos)++;
return errors == 0 ? message_len : -EFAULT;
}
}
module_init(pokemon_init);
module_exit(pokemon_exit);