Add Loadable Kernel module to Linux source code

  • Loadable kernel module (or LKM) is an object file that contains code to extend the running kernel, or so-called base kernel, of an OS.
  • LKMs are typically used to add support for new hardware and/or filesystem, or for adding system callls. When the functionality provided by a LKM is no longer required, it can be unloaded in order to free memory and other resources
  • Without LKM,need to add all functionality into base kernel.
  • LKM loads into kernel when device connects to system and LKM unload automatically when device is disconnected from system.

Add Simple Kernel module to kernel Source code

  • To add simple kernel module,Need to write simple driver code,make change into Makefile and Kconfig file.
  • Rebuild kernel source code.
  • take a simple example,add driver file mydrv.c into /linux/drivers/gpio/ path.suppose mydrv.c file as below:

mydrv_file

  • For configuration like “make menuconfig” add entry into ./driver/gpio/Kconfig file and ./driver/gpio/Makefile.
  • Add entry in ./driver/gpio/Makefile with obj -y += mydrv.o or obj-$(MY_DRV_MODULE) += mydrv.o to load as module.

makefile

  • Add entry in file : ./driver/gpio/Konfig.This entry shows configuration while “make menuconfig” command occurs.

Kconfig

  • Make sure that here given entry MACRO must be same as given in Makefile.In Kconfig file depends on ARM means MY_DRV_MODULE depends upon ARM macro.
  • Now we have successfully created kernel module.Its time to build using Toolchain,To do that,first need to give toolchain name  using CROSS_COMPILE and Architecture name ARCH.

compile

  • When you give “make menuconfig” it opens UI.which shows configuration options depends upon all Kconfig and Makefiles.
  • Goto Device Driver -> GPIO support -> My driver module.It should be enable.Your module is ready to use with kernel.
  • This is basic idea how linux kernel manages Linux kernel module…So simple is that 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s