|
| 1 | +/** |
| 2 | + * @file hello.c |
| 3 | + * @author Derek Molloy |
| 4 | + * @date 4 April 2015 |
| 5 | + * @version 0.1 |
| 6 | + * @brief An introductory "Hello World!" loadable kernel module (LKM) that can display a message |
| 7 | + * in the /var/log/kern.log file when the module is loaded and removed. The module can accept an |
| 8 | + * argument when it is loaded -- the name, which appears in the kernel log files. |
| 9 | + * @see http://www.derekmolloy.ie/ for a full description and follow-up descriptions. |
| 10 | +*/ |
| 11 | + |
| 12 | +#include <linux/init.h> // Macros used to mark up functions e.g., __init __exit |
| 13 | +#include <linux/module.h> // Core header for loading LKMs into the kernel |
| 14 | +#include <linux/kernel.h> // Contains types, macros, functions for the kernel |
| 15 | + |
| 16 | +MODULE_LICENSE("GPL"); ///< The license type -- this affects runtime behavior |
| 17 | +MODULE_AUTHOR("Derek Molloy"); ///< The author -- visible when you use modinfo |
| 18 | +MODULE_DESCRIPTION("A simple Linux driver for the BBB."); ///< The description -- see modinfo |
| 19 | +MODULE_VERSION("0.1"); ///< The version of the module |
| 20 | + |
| 21 | +static char *name = "world"; ///< An example LKM argument -- default value is "world" |
| 22 | +module_param(name, charp, S_IRUGO); ///< Param desc. charp = char ptr, S_IRUGO can be read/not changed |
| 23 | +MODULE_PARM_DESC(name, "The name to display in /var/log/kern.log"); ///< parameter description |
| 24 | + |
| 25 | +/** @brief The LKM initialization function |
| 26 | + * The static keyword restricts the visibility of the function to within this C file. The __init |
| 27 | + * macro means that for a built-in driver (not a LKM) the function is only used at initialization |
| 28 | + * time and that it can be discarded and its memory freed up after that point. |
| 29 | + * @return returns 0 if successful |
| 30 | + */ |
| 31 | +static int __init helloBBB_init(void){ |
| 32 | + printk(KERN_INFO "EBB: Hello %s from the BBB LKM!\n", name); |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +/** @brief The LKM cleanup function |
| 37 | + * Similar to the initialization function, it is static. The __exit macro notifies that if this |
| 38 | + * code is used for a built-in driver (not a LKM) that this function is not required. |
| 39 | + */ |
| 40 | +static void __exit helloBBB_exit(void){ |
| 41 | + printk(KERN_INFO "EBB: Goodbye %s from the BBB LKM!\n", name); |
| 42 | +} |
| 43 | + |
| 44 | +/** @brief A module must use the module_init() module_exit() macros from linux/init.h, which |
| 45 | + * identify the initialization function at insertion time and the cleanup function (as |
| 46 | + * listed above) |
| 47 | + */ |
| 48 | +module_init(helloBBB_init); |
| 49 | +module_exit(helloBBB_exit); |
0 commit comments