I’ve decided to make a short tutorial and present the way I compile kernel modules (outside the kernel sources).

I’ve built few kernel modules (mainly governors – ineractive and smartass, but also cifs / nls-utf8, etc) and I started receiving private messages asking how I did it.

For kernel modules that come with the kernel itself – cifs / tun for example – they just work if you compile the kernel and activate correct config parameters.
Some other modules (such as the smartass governor that doesn’t come with the kernel) you compile outside the kernel source. However they require changes since kernel does not export the symbols the module needs to use – so you have to know what k_all_syms are needed, grab them from the phone and update the kernel module.

So there will be changes there. However, the main steps are:

a) follow tutorials to get the kernel / android ndk to compile. People seem able to do this.
b) then take the module you want (For example cpufreq_smartass.c from here: http://pastebin.com/rR4QUCrk ) and copy it in a new folder on the disk.
c) create a Makefile like the one below, but with your paths of course:

d) execute make

Of course, the module source code needs to be adjusted as you need to put in the CPU frequencies, and also update the k_all_syms pointers .. But you can retrieve them from /proc/kallsyms on the device itself – just look for the method name, and use the address you see in the log.

If you still can’t get it to compile, try to compile a very basic hello_world kernel module. I used the code below when testing:

It is not perfect, but if you manage to insmod and then check dmesg, you will see “Hello world” written there.

One more thing, Linux kernel is fussy about the module versions. Even if nothing is changed between two kernel versions related to what a module needs, is enough a small difference in module’s modinfo value to make the kernel to refuse the module.

For this, you need to trick your local kernel and adjust EXTRAVERSION value in kernel’s main Makefile to have the exact version of the one on the device:

In X10 stock kernel (GB 2.3.3 release), the kernel version is 2.6.29-00054-g5f01537 visible in phone settings.

This means that the kernel on the phone will only accept modules that are compiled for that exact version. But the kernel version is just a string in the module .ko, so is a string comparison – the module might work perfectly, but is not loaded.
There is luck though, the string value comes from a define in kernel’s Makefile, which you can change before you compile!

The Makefile in the kernel you are going to use to build the module will have to include these lines at the top:

Other than that, it should work ..

Expect phone reboots and difficulty to debug if stuff goes wrong. Android kernel doesn’t come with syslog functionality, kernel printk output is found in /proc/kmsg. Dmesg works, but you can’t execute if if phone reboots.
I usually had to keep another adb shell opening with ‘cat /proc/kmsg’ which showed as much as possible from the module’s outputs.

Happy compiling on your risk!

PS: I’ve posted the same tutorial on XDA’s website here: http://forum.xda-developers.com/showthread.php?p=17020258#post17020258