First device driver module on your custom kernel

After compiling custom kernel in UBUNTU 13.10 we can move forward to our first device driver.

Turn on your laptop after compiling kernel and at the time of boot select advance (2nd option…!).

Choose your custom kernel as per your given name.

Now first check on which kernel you logged in with uname -r command in terminal.

1

Second step is to create new directory called mymodules.

mkdir mymodules

Now change your current directory.

cd mymodules

2

create firstmodule.c

vi firstmodule.c

write following c program

/*  
 *  firstmodule.c – The simplest kernel module.
 */
#include <linux/module.h>    /* Needed by all modules */
#include <linux/kernel.h>    /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO “Hello world 1.\n”);

    /*
     * A non 0 return means init_module failed; module can’t be loaded.
     */
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO “Goodbye world 1.\n”);
}

Close After writing using (CTRL+C).

In the same directory write Makefile

vi Makefile

obj-m += mymodule.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

save and close the file .Keep holding same directory location in terminal and type make

It will compile module .

4

In module programming there is no <stdio.h>

It uses other header files.One more important thing Module programming does not have Main

function.

_init shows that it is initiation of module.

_Exit shows that it is exit of the module.

Inside module all will be executing concurrently. After Compiling mymodule.ko file will be created. Now its ready to kernel

open your terminal (current directory should be mymodules)and type

insmod mymodule.ko

Thats it.

Your first device driver is ready to work along with current kernel .

Some useful commands for Module loading and unloading

lsmod – To list out how many modules are loaded in the your kernel .

rmmod filename.ko– to unload the loaded module.

insmod filename.ko – to load the module.

modprobe finename.ko – to load the module along with dependencies.

For more information I am always here to help you.Comment your doubts.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s