Monday, November 28, 2011

How to create a simple linux device driver

Here, I would show you how to create a simple linux driver that does nothing! This just a demo to show you how to compile, install and remove the device driver.
Before we get started, you need to make sure that you have the kernel source installed. The following instructions will guide you through the steps to install the kernel source for RedHat Enterprice 4 (RHEL4). Issue the following command to get the kernel source.up2date –get-source kernel
This would dump the kernel-.src.rpm in the /var/spool/up2date folder. Then you must install the source using the following command.
rpm -Uvh kernel-.src.rpm
This would install the kernel source in the /usr/src/redhat. You need to change the directory to /usr/src/redhat/SPECS/ and issue the following command.
rpmbuild -bp –target= kernel-2.6.spec where <arch> is the desired target architecture.
Then copy the configuration file from the /usr/src/redhat/BUILD/kernel-2.6.9/linux-2.6.9/configs/kernel-.config to

Then issue the following command
make oldconfig
Build all the modules before you could compile your linux device drivers.
make modules
Now that we have the proper kernel source and modules compiled, create a file ‘nothing.c’ as follows:
and save it your home folder ~/nothing/nothing.c
In ~/nothing, create Makefile as follows:
obj-m    := nothing.o

KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := $(shell pwd)

   $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
Change directory to ‘~/nothing‘ and then issue the ‘make‘ command, this will create the ‘nothing.ko’ file in the current folder.
You can load the module in the kernel space using the following command.
insmod nothing.ko
and remove the module using the command.
rmmod nothing

No comments:

Post a Comment