This is a demo I whipped together to illustrate a starting point (or skeleton) for developing Linux kernel modules for the Open Embedded environment. I did this for myself, and found the documentation on it to be fragmented.
I got a lot of help from the following links:
Linux Kernel Programming Guide - http://dirac.org/linux/writing/lkmpg/2.6/lkmpg-2.6.0.html#AEN40
Gumstix "hello world" example: http://www.gumstix.net/Software/view/Build-system-overview/Hello-world-t...
Bitbake user manual: http://bitbake.berlios.de/manual/
Openembedded Wiki: http://openembedded.org/wiki
Here is the file that I made, you can un-tgz it and link it into your OE tree: kmodule-test.tar.gz
Here is the bitbake file that defines how to build and package it:
DESCRIPTION = "Kernel Module Development Test"
HOMEPAGE = "http://www.example.com"
SECTION = "kernel/modules"
PRIORITY = "optional"
LICENSE = "none"
RDEPENDS = "kernel (${KERNEL_VERSION})"
DEPENDS = "virtual/kernel"
PR = "r0"
SRC_URI = " \
file://kmodule_test.c \
file://kmodule_test.h \
file://Makefile \
"
S = "${WORKDIR}"
inherit module
do_compile () {
unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS CC LD CPP
oe_runmake 'MODPATH="${D}${base_libdir}/modules/${KERNEL_VERSION}/kernel/drivers/ecu" ' \
'KERNEL_SOURCE="${STAGING_KERNEL_DIR}" ' \
'KDIR="${STAGING_KERNEL_DIR}"' \
'KERNEL_VERSION="${KERNEL_VERSION}" ' \
'CC="${KERNEL_CC}" ' \
'LD="${KERNEL_LD}" '
}
do_install () {
install -d ${D}${base_libdir}/modules/${KERNEL_VERSION}/kernel/drivers/test
install -m 0644 ${S}/kmodule_test*${KERNEL_OBJECT_SUFFIX} ${D}${base_libdir}/modules/${KERNEL_VERSION}/kernel/drivers/test
}
Then the Makefile that this calles to actually build the module:
# Names of modules to build
MODULES = kmodule_test
# List of what object files need to be compiled for this module
# add more .o files here if you are linking more than one .c file
# add more variables like "<modulename>-y" if you are building more than one kernel module
kmodule_test-y := kmodule_test.o
# Names of module objects to link
obj-m += $(MODULES:%=%.o)
# A list of kernel module objects (.ko files) we should end up with
BUILD = $(MODULES:%=%.ko)
# default rule
default: all
# all: build the kernel modules using the Linux kernel makefiles
all::
$(MAKE) -C $(KERNEL_SOURCE) ARCH=$(ARCH) \
SUBDIRS=$(CURDIR) CC=${CC} modules
# Clean target: get rid of intermediates and final build products
clean::
rm -f $(BUILD) *.o *.ko *.mod.c *.mod.o *~ .*.cmd Module.symvers
rm -rf .tmp_versions
Then there is the actual kernel module source file:
/*
* Skeleton Linux Kernel Module
*
* PUBLIC DOMAIN
*/
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk("Module init called\n");
// return sucsess
return 0;
}
void cleanup_module(void)
{
printk("Module cleanup called\n");
}
That's about it... If you untar that archive somewhere where OE knows where it is, it can then build the stub kernel module... All this module will do is print out text when it's inserted and removed from the kernel. But this is a good starting/learning point for people to get more familiar with the OE/bitbake environment.