Embedded Systems – Linux Kernel Module Programming II

SAMSUNG TECHWIN DIGIMAX-340

In the previous Kernel Module Programming Tutorial, we got started with a simple ‘Hello World’ program to be loaded in the kernel. Now, let us proceed further.

Let us write another module with the name “ExampleK”. For newbies, do the following on the terminal :

nano ExampleK.c

Then write the following code :

#include <linux/module.h>  /* Needed by all module */
#include <linux/kernel.h>  /* Needed for kern info */
#include <linux/init.h>    /* Needed for the macros- will be discussed */

static int hey_init(void)
  {
     printk(KERN_INFO "How are You ? \n");
     return 0;  /* Loaded successfully */
  }

static void hey_exit(void)
  {
     printk(KERN_INFO "Okay fine, Bye \n");
  }

module_init(hey_init);
module_exit(hey_exit);

Now, as already seen in the previous tutorial, we need a Makefile for “ExampleK.c”. It is done as follows :

nano Makefile

When the file opens, enter :

obj-m := ExampleK.o

The Makefile is complete and now let us compile it. Type in the terminal following :

sudo make -C /lib/modules/$(uname -r)/build M=$PWD modules

When you do “ls”, you will  find that a “.ko” module has been created to be inserted into the kernel. To insert the module into the kernel, type in :

sudo insmod ExampleK.ko

We need the sudo command because we are inserting the module into the kernel. To view the module type in :

lsmod

screenshot-from-2016-12-16-19-57-22

Congratulations! You inserted your first module into the kernel. After this victory, let us dive deeper.

Playing with Variables

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int hey_initdata = 3;

static int hey_init(void)
  {
     printk(KERN_INFO "Hey Again Part %d \n", hey_initdata);
     return(0);
  }

static void hey_exit(void)
  {
     printk(KERN_INFO "Bye Again \n");
  }

module_init(hey_init);
module_exit(hey_exit);

Follow the same procedure as given above to get this module inserted and replace the makefile with the required name you wish to keep. You need to change the makefile.

License Documentation

Let us look at some basic before starting :

  • MODULE_DESCRIPTION : Describes what the module does.
  • MODULE_AUTHOR : Declares the module’s author.
  • MODULE_SUPPORTED_DEVICE : Declares the types of devices the module supports.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

#define DRIVER_AUTHOR "Saumitra Kapoor <Saumitra.co>"
#define DRIVER_DESC "A driver"

static int hey_init(void)
  {
    printk(KERN_INFO "Can we say anything apart from hey ? \n");
    return 0;
  }

static void hey_exit(void)
  {
    printk(KERN_INFO " You are so cruel \n");
  }

module_init(hey_init);
module_exit(hey_exit);

MODULE_LICENSE("GPL"); /* If not mentioned, certain functionalities will be disabled */
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_SUPPORTED_DEVICE("Test");

You can “rmmod” the modules to remove it from the kernel.

Final Comments

In the coming tutorials, we’ll discuss more about module spanning and passing command line arguments. If you face any difficulty, feel free to reach out to me. Thank you for the support.

Saumitra Kapoor

1 Comment

  1. woftem says: Reply

    I enjoying, will read more. Cheers!

Leave a Reply

Bitnami