2009年12月24日 星期四

Embedded Linux Kernel Module using Hello Wolrd

Most people thought Linux is difficult and especially embedded Linux,as they required to porting a linux driver,hey,it choked their throats. But it is really simple if you know the rule,let me show you the rope using the "Hello world" example. Let us check the code as below:
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Henry");

static int hello_int(void)
{
printk(KERN_ALERT "Hello、world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye、cruel world!\n");
}

module_init(hello_exit);
module_exit(hello_ext);

It is the simplest linux kernel module example
if you have a ARM base embedded board、you coul make it a hello.ko module and then
load the module into kernel :insmod ./hello.ko
then you can see Hello、world
you can check if the module being installed correctly or not by lsmod
then you remove the module: rmmod hello,
the message come out: Goodby cruel world

As typical Makefile you can refer as below:
ifeq($KERNELRELEASE),)
KERNELDIR ?=/lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) module_install

clean:
rm -rf *.o *~core .depend .*.cmd *.ko *.mod,c .tmp_versions

.PHONY: modules modules_install clean

else
obj -m :=hello.o
endif

沒有留言:

張貼留言