Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the
functionality of the kernel without the need to reboot the system. If you want to add your code(module) to a Linux
kernel, the most basic way to do that is to add your source files to the kernel source tree, add entry of your module in
appropriate Makefile and Kconfig file and recompile the kernel. In fact, the kernel configuration process consists
mainly of choosing which files to include in the kernel to be compiled. All the device drivers are written using these
loadable modules.Let us add a very basic sample kernel module. Add this file to drivers/gpio directory in Linux kernel
source tree.
vim ./drivers/gpio/helloWorld.c helloWorld
Now we need to add configuration setting, so that we can add or remove this module from the By the time of Linux
kernel configuration(You can configure it from “make menuconfig”). To do that add your modules entry in ./driver
/gpio/Kconfig file. Add below codes to ./drivers/gpio/Kconfig file.
Line 1 is the macro with using which our module can be configured.Line 3 states that this option can only be enabled if
CONFIG_ARM is enabled . Next We have to inform kernel to compile hello_world.c when HELLO_WORLD_MODULE
configuration is enabled.Add this to ./drivers/gpio/Makefile.
We have successfully added a new module to Linux kernel. Now lets test our new module with Linux kernel.To do that
first we need to give tool chain name to CROSS_COMPILE flag and architecture name to ARCH flag in Linux terminal.
Now configuration menu will appear. Navigate to Device Drivers—>GPIO Support —>Hello World Module and enable
it. Now start compiling kernel and modules.
Your module will get inserted as a part of kernel.
Enjoy !!!
After successfully making this driver where to check the output of this driver
All prints will go to kernel messages. You can print it using ‘dmesg’ command.
There you can check your logs/output.
i tried adding a module to the source.after compilation,the module is not appearing under /dev. can you suggest me what the issue would be
Are you using the source code which is present in on this post ?
If you are using the same source code which is this post then you will be get any entry in /dev.
You can check the output of your printk on dmesg.
For entry in /dev, we need to register one device with file_operation structure.
Pingback: Device driver to interface shift reg with Raspberry pi 2 | Linux hacks
I am compiling the kernel with same settings, it compiled successfully. After that I have uImage created in arch/arm/boot.I copied uImage file to sdcard. Now this sdcard explored and I found hello world.o is at the location. Then I puup this sdcard in my arm board sdcard slot and powered up the board ,after successfully boot and again try to find the location of the helloWorld.o but I am unable to locate the file. Can u help me.?er
Are u trying to find hello world.o file from your present file system on your ARM board ?
There will be two partition in your SD card. One is with ext3 file system and other will be with FAT. Your uImage will be in in FAT pat partition.
To access your hello world.o, you need to put it in ext3 partition. That contains Linux file system and that will be mounted as root FS.
I have used the command
Make ARCH=arm MOD_PATH_INSTALL=/media/abhinandan/rootfs module_install
It install the module in rootfs.
But do not found anywhere in rootfs partition.
After using INSTALL_MOD_PATH=/media/abhinandan/rootfs command your module should be compiled at / media/abhinandan/rootfs/lib/modules/ location.
First you need add your module in and kconfig file and select to compile as module as separate module.
Don’t select it to build as part of your kernel.
When you want to add some driver into linux kernel, you have to follow the below steps.
1) You have to put the *.c file in appropriate folder of linux structure (char/misc/helloworld.c)
2) Then you have to modify the Makefile and Kconfig of that folder (ex: char/misc/Makefile & char/misc/Kconfig)
Need to mention file name with macro in Makefile
Ex:
obj-(CONFIG_HELLO_DRIVER) += helloworld.o
Need to mention about the driver details and dependencies of driver etc.,
Ex:
config HELLO_DRIVER
tristate “Sample linux hello world driver”
default n
help
If you say “Y” here, you will enable the sample driver support in kernel
3) Now you can see the “Sample linux hello world driver” option in menuconfig.
4) Now you can choose the driver as module (M) then build it.
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
Make ARCH=arm MOD_PATH_INSTALL=/media/abhinandan/rootfs module_install
Still it is not appearing at anywhere in rootfs partition.
In above code default value is No. So this will be compile. You have to select it via make menuconfig. Then it will be compiled.
You can try one thing, put some grammatical error in your module and then compile your code to check it is compiling or not.
It is compiling, I am very sure but when transfer the image to sdcard and install_modules. It transfer to card’s rootfs partition.but when I boot from this sdcard it boot successfully. Then I explore the for my compiled module ,I never find any where.
You can do one thing. First check .o file is created for your module ?
If it is created then do make_install to sdcard. Then mount that card in to your any computer and chek in to /lib/modules/ path.
There u ur .ko should be present .
if it is present u can insert that card in to your board after booting check to /lib/modules/ path in RFS.