Lab3 - Linux Device Driver (version 1)

Implement Linux Device Driver Version 1 of hello1.c: no parameter

Objective: Build and Install a Sample device driver and then compile "hello" program to verify it.

Requirements:

  1. Linux (CentOS) - You can download this linux flavor from link http://www.centos.org/
  2. Used linux kernel version 2.6.20
  3. Vmware Player / Vmware Server / Vmware Workstation

Procedure

Step 1

  • First go to the directory
$ cd /usr/src/kernels/linux-2.6.20.1/drivers/char
  • Then create a new directory examples in it using following command.
$ mkdir examples
  • Go into the created directory examples using following command.
$ cd examples
  • Now you need to create two files in this examples directory. One of them is Makefile and second one is hello1.c. Steps are as below.
$ vi Makefile
  • and write following line into that file.
obj-$(CONFIG_EXAMPLES) += hello1.o

  • Then save it using :wq command.

Snapshot 1

$ vi hello1.c
  • And write following sample driver programs in hello1.c.
            /* Example Minimal Character Device Driver */
#include <linux/module.h>
 
static int __init hello_init(void)
{
              printk("Hello Example Init\n");
              return 0;
}
 
static void __exit hello_exit(void)
{
              printk("Hello Example Exit\n");
}
 
module_init(hello_init);
module_exit(hello_exit);
 
MODULE_AUTHOR("Chris Hallinan");
MODULE_DESCRIPTION("Hello World Example");
MODULE_LICENSE("GPL");

Step 2

  • Go into the following directory to change the Kconfig file.
$ cd /usr/src/kernels/linux-2.6.20.1/drivers/char
  • Then first back up Kconfig file to Kconfig.old file.
$ cp Kconfig Kconfig.old
  • The following Snapshot shows the content of Kconfig.old file.

Snapshot 2

  • Then open file Kconfig using following command
$ vi Kconfig  
  • And add the following lines in Kconfig to add new configuration. Also add + sign in beginning of those lines as shown below.
              ....             
              ....             
              menu "Character devices"
 
              + config EXAMPLES
              +   tristate "Enable Examples"
              +   default M
              +   ---help---
              +   Enable compilation option for driver examples
 
              config VT
              ....             
              ....

  • Following Snapshot 3 shows the content of new Kconfig file.

Snapshot 3

Step 3

  • Modify the usr/src/linux/drivers/char/Makefile using following commands
$ cd /usr/src/linux-2.6.20.1/drivers/char
$ vi Makefile

Search Makefile for the name IPMI using -> /IPMI

Then add the following line as shown in Snapshot. Don’t put blank line after or before added line.

obj-$(CONFIG_EXAMPLES)          += examples/

Snapshot 4

Step 4

  • Then go to the directory linux-2.6.20.1 using following command.
 $ cd /usr/src/linux-2.6.20.1
  • Then first back up the .config file in .config.old using following command.
 $ cp .config .config.old
  • Then edit the .config file as below.
$ make menuconfig
  • Then go to the Device Driver -> Character Devices -> Enable Examples
  • Use spacebar to change configuration to ‘M’ of ‘*’ for Enable Examples.

Snapshot 5

Step 5

  • Enter the following command to compile modules.
$ make modules

Snapshot 6

Step 6

  • The following command installs the module.
$ make modules_install
  • It shows many warning at the end but ignore it.
  • This command creates files hello1.mod.o and hello1.ko as shown in following Snapshot 6.

Snapshot 7

  • The Following Snapshot 8 shows the newly created files in examples folder other than Makefile and hello1.c.

Snapshot 8

Step 7

  • To load the module use modeprobe or insmod but hello1.c doesn’t have any dependencies. So use commad insmode which is inside directory /sbin/insmode. So first go into the directory examples and use the insmode as shown in Snapshot 9.
$ cd /usr/src/kernels/linux-2.6.20.1/drivers/char/examples
$ /sbin/insmod hello1.ko

Snapshot 9

  • To see if module is sucessfully loaded or not, write the lsmod command and use pipeline with grep command to filter out hello word file & check the result as shown in following Snapshot 10.

Snapshot 10

Step 9

  • To remove the module from kernel use rmmod as shown below and use lsmod command to check wheher module is removed or not.

Snapshot 11

  • Use modinfo to see the module information as shown below.
$ /sbin/modinfo /usr/src/linux-2.6.20.1/drivers/char/examples/hello1.ko

Snapshot 12