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
|
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
/* used for kmalloc */
#include <linux/slab.h>
#include <linux/fs.h>
#include <debug.h>
char *debugmsg = "Tesla coil is a hi freq transformer";
struct dentry *debug_dir = NULL;
struct dentry *debug_file = NULL;
dev_t devnum;
/* __init is a hint that the func is only used at initialization time, then module
* loader drops the function after module is loaded making its memory available for other uses
*/
static int __init hello_init(void)
{
int err = 0;
char *msg = (char *) kmalloc_array(128, sizeof(char), GFP_KERNEL /* kernel ram */);
printk("module named hello inserted\n");
strcpy(msg, "photon is the energy carrier particle for EM waves within light spectrum");
printk("%s\n", msg);
kfree(msg);
/* device numbers, major will be picked for us */
err = alloc_chrdev_region(&devnum, 0 /* first minor */, 1, "skull");
printk("major: %d, minor: %d\n", MAJOR(devnum), MINOR(devnum));
/* modifies passed in ptrs */
err = debug_init(&debug_dir, &debug_file);
if (err)
printk("debugfs might not have been mounted\n");
return 0;
}
static void __exit hello_exit(void)
{
unregister_chrdev_region(devnum, 1);
debug_destroy(debug_dir, debug_file);
printk("module named hello removed\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
|