Tuesday, June 25, 2013

Creating the Module

First, find the source that your current Linux kernel was compiled from. You can save the mymodule.c in any one of the folder under staging or drivers directory


mymodule.c


#include <linux/module.h>
#include <linux/config.h>
#include <linux/init.h>

static int __init mymodule_init(void)
{
 printk ("My module worked!\n");
        return 0;
}

static void __exit mymodule_exit(void)
{
 printk ("Unloading my module.\n");
        return;
}

module_init(mymodule_init);
module_exit(mymodule_exit);

MODULE_LICENSE("GPL");

Edit the Makefile in the same directory. Add this line
obj-m += mymodule.o
Compile your module:
 # make -C [top directory of your kernel source] SUBDIRS=$PWD modules
Load the module:
 # insmod ./mymodule.o
And check to see if your message printed out:
# dmesg
At the end of the output:
My module worked!
Now remove the kernel module:
 # rmmod mymodule
Check the output of dmesg again, you could see the output:
 Unloading my module.


Wednesday, June 12, 2013

Makefile and Kconfig


make file

Define the files to be built and and link to other files,  Makefiles within the kernel are kbuild Makefiles that use the kbuild infrastructure, these make  evaluates to either y (for built-in) or m (for module) If it is neither y nor m, then the file will not be compiled nor linked.


Kconfig

Every entry has its own dependencies. These dependencies are used
to determine the visibility of an entry. Any child entry is only
visible if its parent entry is also visible.

Menu entries
------------

Most entries define a config option; all other entries help to organize
them. A single configuration option is defined like this:

config MODVERSIONS
bool "Set version information on all module symbols"
depends on MODULES
help

 Usually, modules have to be recompiled whenever you switch to a new kernel.

Every line starts with a key word and can be followed by multiple
arguments.  "config" starts a new config entry. The following lines
define attributes for this config option. Attributes can be the type of
the config option, input prompt, dependencies, help text and default
values. A config option can be defined multiple times with the same
name, but every definition can have only a single input prompt and the
type must not conflict.

https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt