8a5a20b0c6b8b3832f856dfa7e2078308945082c
[cascardo/kernel/samples/hello2/.git] / hello.c
1 /*
2  *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
28 MODULE_DESCRIPTION("Hello, world");
29
30 static int size = 1;
31 module_param_named(size, size, int, S_IRUGO | S_IWUSR);
32
33 struct hello {
34         int times;
35         char greeting[0];
36 };
37
38 static struct hello *hello;
39
40 static char default_greeting[] = "Hello, world!\n";
41
42 static int hello_init(void)
43 {
44         hello = kmalloc(sizeof(*hello) + sizeof(default_greeting), GFP_KERNEL);
45         if (!hello)
46                 return -ENOMEM;
47         memcpy(hello->greeting, default_greeting, sizeof(default_greeting));
48         printk("%s\n", hello->greeting);
49         printk("%p %p %p\n", hello, hello->greeting,
50                 container_of(&hello->greeting, struct hello, greeting));
51         printk("size %d\n", ARRAY_SIZE(default_greeting));
52         return 0;
53 }
54
55 static __exit void hello_exit(void)
56 {
57         kfree(hello);
58 }
59
60 module_init(hello_init);
61 module_exit(hello_exit);