How-to add a loadable module to the uClinux

From Klaus' wiki
Jump to: navigation, search

First ensure that there are loadable support in "make menuconfig" - it's in the kernel section.

Second ensure that insmod, lsmod, modprobe and rmmod are included (by default are modprobe not included). Find the in the vendor/user settings part in the BusyBox configuration.

There are two options of how to develop the loadable module

1. In the uClinux distribution tree

2. In another development tree

By developing in the distribution tree the kernel module will be included in the kernel build and in the final image going onto the embedded device.

By developing outside the kernel source the kernel module can be sent to the embedded device through a nfs mount or by ftp. This may enable a quicker turn around in the development process. This process is like the process of developing the Scull examples from the Linux device Driver book. And finally this way does also not require a kernel rebuild/make.

In this how-to the second option is chosen.

Create a directory parallel to the uClinux-dist directory.

$ mkdir -p modules/hello
 
cd modules/hello

Create a source hello.c

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
 
static int hello_init(void)
{
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}
 
static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

Create a Makefile

obj-m   += hello.o

if the source for the module consists of two or more source files prepare the Makefile like this:

obj-m += hello.o
hello-objs := file1.o file2.o

Prepare a script-file e.g. build.sh, provided that the modules directory is at same level as uClinux-dist and that the code is residing in module/hello

## Change to the uClinux distribution directory
cd ../../uClinux-dist 
## Get the global settings into the environment
. settings.sh  
## Back to our working directory
cd ../modules/hello/  
## Make it happen
make -C ../../uClinux-dist/linux-2.6.x/ SUBDIRS=`pwd`

Finally the last thing to do is to build the module

$ ./build.sh

look for errors and look for hello.ko.

Load the hello.ko directly to a running board using nfs, ftp or the like.

Happy hacking with your modules.