A very very simple and rudimentary tutorial to know "How to write a kernel module". This is _no_ substitute for reading a good driver book/tutorial. This is just to give a launchpad for a novice to start on kernel programming.
Step 1: vi hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int hello_init(void)
{
printk(KERN_ALERT "Hello World");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye World");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("YOUR NAME");
Step 2: vi Makefile
obj-m := hello-kernel.o
hello-kernel-objs := hello.o
Step 3: make -C /lib/modules/`uname -r`/build M=`pwd`
You should see hello-kernel.ko in your current directory, by now.
Step 4: sudo /sbin/insmod hello-kernel.ko
Step 5: dmesg
You should see "Hello World" here.
Step 6: lsmod | grep hello-kernel
Step 7: modinfo hello-kernel.ko
Step 8: sudo /sbin/rmmod hello-kernel.ko
Step 9: dmesg
You should see "Goodbye World" here.