Lab4 - Linux Device Driver (version 2)

Implement Version 2 of device driver program hello1.c: with parameter

  • Try the sample device driver, you should build and install the driver, and then compile the "hello" program to verify it works.
  • To create a sample device driver (version 2) follow the steps as below.
  • I wrote following steps based on documentation of driver version 1 (Lab 3). So, first you need to complete driver version 1 to follow the following instructions.

Step 1

  • First you need administrative privilege using following command.
  • First go to the directory examples where you create hello1.c.
$ su -
$ cd /usr/src/linux-2.6.20.1/drivers/char/examples
  • Then back up the hello1.c to the file hello1.v1.c.
$ cp hello1.c hello1.v1.c
  • To see the content of examples folder use the following command.
$ ls –l examples

Snapshot 1

  • Then edit the file hello1.c file to make it for driver version 2.
$ vi hello1.c
  • And then add three lines which are in RED color in following code.
/* Example Minimal Character Device Driver */
#include <linux/module.h>
static int debug_enable = 0;             /* Added driver parameter */
module_param(debug_enable, int, 0);      /* and these 2 lines */
MODULE_PARM_DESC(debug_enable, "Enable module debug mode.");
 
 
static int __init hello_init(void)
{
    /* Now print value of new module parameter */
    printk("Hello Example Init - debug mode is %s\n",
            debug_enable ? "enabled" : "disabled");
 
    return 0;
}
 
static void __exit hello_exit(void)
{
    printk("Hello Example Exit\n");
}
 
module_init(hello_init);
module_exit(hello_exit);
 
MODULE_AUTHOR("Henry Chang (v2)");
MODULE_DESCRIPTION("Hello World Example");
MODULE_LICENSE("GPL");

Makefile for hello.c is already there in examples folder which is created during driver version 1.

Note: Three lines have been added to our previous example device driver module.

1. The first line declares a static integer to hold our debug flag.

2. The second line is a macro defined in

/usr/src/linux-2.6.20.1/include/linux/moduleparam.h

that registers the module parameter with the kernel module subsystem.

3. The third new line is a macro that registers a string description associated with the parameter with the kernel module subsystem.

Step 2

  • Then go the directory linux- 2.20.1 using following command.
$ cd /usr/src/linux-2.6.20.1

  • Then you do not have to modify Makefile or Kconfig or .config file because it is already modified during creation of driver version 1.
  • Type the following command to compile the module.
 $ make modules
CHK     include/linux/version.h
CHK     include/linux/utsrelease.h
CC [M]  drivers/char/examples/hello1.o
Building modules, stage 2.
MODPOST 1211 modules
CC      drivers/char/examples/hello1.mod.o
LD [M]  drivers/char/examples/hello1.ko

  • Type the following command to install module into kernel.
   $ make modules_install

The result is shown in following Snapshot 2.

Snapshot 2

Step 3

  • Now you can pass parameter when using insmod command.
  • If we now use insmod to insert our example module, and add the debug_enable option, we should see the resulting output, based on our modified hello1.c module.
$ insmod /lib/modules/2.6.20.1/kernel/drivers/char/examples/hello1.ko debug_enable=1

  • Hello Example Init - debug mode is enabled
  • Or, if we omit the optional module parameter:
 $ insmod /lib/modules/2.6.20.1/kernel/drivers/char/examples/hello1.ko
  • Hello Example Init - debug mode is disabled
  • Again, my experiment result does not display the message.

Hello Example Init - debug mode is enabled

OR

Hello Example Init - debug mode is disabled

  • Although I did not try, I believe this issue can be resolved by following the idea mentioned in the textbook

“If you don't see the messages on the console, either disable your syslogd logger or lower the console loglevel. We describe how to do this in Chapter 14, "Kernel Debugging Techniques".

  • So, I can only see the result as below.
$ insmod /lib/modules/2.6.20.1/kernel/drivers/char/examples/hello1.ko debug_enable=1
$ lsmod | grep hello1

Snapshot 3

  • To remove the module from kernel, write the following commands.
 $ rmmod /usr/src/linux-2.20.1/drivers/char/examples/hello1.ko

Snapshot 4