meAbab meAbab - 1 month ago 6x
Linux Question

How to add my source code to Kernel source tree

I've an I2C storage CHIP attached with system, A kernel space driver program of that CHIP is functioning with insmod / rmmod from running system.

But I would like to add this program with kernel source, so that on Kernel booting (from zImage) it will read from I2C CHIP, and Print something (e.g. Serial #) from it.

My question is, is it enough to add the driver object with


obj-y += ?


while searching for Kernel module writing and so on, you'll find a hundreds of thousands "hello_world.c" program, that will describe how you can print hello_world at kernel log, when your kernel and system all working and up. Which are called loadable kernel module, using program like insmod to load and rmmod to unload.

But, I was searching how to load my own script (i.e. that hello_world.c) when kernel is booting, and wanna see something on console that exactly coming from kernel at booting time, then google is almost silent.

Well, its just not difficult, and quite easy. What I've done by myself to get the printk() message at kernel booting is -

Kept the hello_world.c program inside ./kernel/driver (You can keep it almost anywhere inside Kernel source folder also can make your own folder, but that will require your own Makefile).

And edit the ./kernel/driver/Makefile to add obj-y += hello_world.o at the end of the Makefile.

Then compile the full kernel and generated zImage transferred to sd-card. While booting, just after USB HID core driver, I am able to see my custom message from hello_world printk().

[    3.652944] usbcore: registered new interface driver usbhid
[    3.657253] usbhid: USB HID core driver

NOTE:- Unlike, loadable module, function maked with __exit would never called by compile when subjected code is in-built with kernel source.

i.e. in Such case hello_world.c is "Programmed to received (read include) but can't never leave"