“Hello World” driver
In this article we will be writing simple character driver and will hook this character driver in our running kernel of your Linux machine.
As we discussed about multiple component of a character driver. If we put all component together our code looks like below image and save this code as a file name “hello.c”
This is our hello world driver Which at the time of init prints “hello world” and at the time of exit it prints “Goodby”. Now our driver is ready and now we need to build it and hook it with our linux kernel.
Building or compiling driver
We can build our driver with our linux kernel directly by putting your hello.c in any directory of your linux kernel.
we can also build our character driver separately. For that we need to write Makefile which will build our “.c” file. Below is the example of Makefile.
after building driver as an output we get “test.ko” in same directory.
now next task is to load our driver in current running kernel in your machine. We can do this work by insmod commend.
when we insert driver by using insmod then hello_init function of driver will get called and print “hello world” in kernel logs.
we can remove driver by rmmod command.
we can see kernel logs by issuing dmesg command.