An introduction to Linux Device Drivers – #1

Today we’ll be discussing a basic c code, the famous hello.c, which will be loaded as a kernel module and discuss some of the basic aspects related to it.

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE (“GPL”); // included in module.h, tells about the license the code is having.

static int __init hello_init (void)

{

printk (KERN_ALERT “Hello, world\n”);

return 0;

}

static void __exit hello_exit (void)

{

printk (KERN_ALERT “Goodbye\n”);

}

module_init (hello_init);     // hello_init is the initialization function

module_exit (hello_exit);     //hello_exit is the exit function

The header <linux/init.h> contain various declarations and definitions related to the loading and cleaning up of modules. The macros module_init and module_exit are declared in the <linux/init.h>. The argument functions passed to module_init( ) and module_exit( ) are executed at the loading and unloading times respectively of a module. The initialization function, basically, sets up the device to be used later. The exit function cleans up the device ( opposite to initialization).

Now coming to the __init and __exit terms used in the code. The __init in static int __init hello_init (void) specifies that the hello_init function is executed only at module load time. The module loader drops the initialization function for other uses once the module is loaded. The initialization function better be static since they are not meant to be visible outside the specific file ( not mandatory though). Similar explanation goes for __exit. This makes the exit function executed only at exit time.

The printk function, at a first glance, might look identical to printf of <stdio.h>, but printk lets you set priority levels for the message, like KERN_ALERT, KERN_INFO, etc. We’ll discuss about them later.

How to compile the code and execute it. Let us assume that you named the source file above as hello.c. Use any suitable text editor to write a Makefile as below :

obj-m    += hello.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
rm -f *~

I hope you have the kernel source in your system. If not you can get the kernel source from kernel.org. If you are using Fedora, you can do:

# yum install kernel-devel

To compile, in the current directory ( where there is hello.c and the Makefile) do :

$ make

Then change to super user by : $su <enter>

and do : # insmod ./hello.ko

If you do : # tail -f /var/log/messages (You can open this in a separate terminal to view the system logs)

then you will be able to see a line saying “Hello, world”. This is what your hello_init( ) was supposed to do at module load time. You can also see the module name “hello” among the list of loaded modules doing lsmod. Now you can unload the module do :

# rmmod hello

Now in /var/log/messages, you will see “Goodbye”. This is due to hello_exit( ).

continued …

An introduction to Linux Device Drivers – #0

I’ll be honest, I am neither a kernel hacker nor a device driver writer, but I want to be one. I am studying the Linux Device Drivers 3, the LDD 3. It is an effort from my end to document what I have understood from reading the LDD 3. There might be some errors or misinterpretation on my part and I will be glad if anyone corrects me. I will be trying to keep the things as correct as possible.

rtnpro @ Ganga Sagar Mela, 2010

We ( me, Bama and Subhrodeep) left for Ganga Sagar Mela from ISKCON, Mayapur along with ISKCON devotees on the Monday morning of Jan 11, 2010. We had lots of fun in the bus. Fisrt, our bus was special. It was the bus in which there was the Gour Nitai deity. We started the journey with Narsimha Arati and we prayed for a safe journey. Then we chanted the holy names and the devotees from outside India started melodious kirtans.

We reached Govinda’s at Kolkata at around 1 PM. There we all had Prasadam. Then we resumed our journey towards the Diamond harbour. It was evening when we reached there. Then we had to take a ferry from one of the JETTYs to cross the Ganga river. I don’t exactly remember from which JETTY we got on the ferry (as we were moved from one JETTY to another a couple of times). I was tired, but the chanting of the HARE KRISHNA maha mantra kept my spirits up. Once on the ferry, we again started sankirtan. After some time ( I think more than an hour) we reached the island of Kapil Muni. After waiting for some time in long queues, we finally got on a bus to the Ganga Sagar camp site.

From there, we took the road no. 4 to ISKCON’s camp site. We met Sankarshan Prabhu ( General Manager, ISKCON, Mayapur). We took Prasadam at around 12:30 PM and then went to our tent for sleep. Next day, we woke up for Darshanarati. After Darsanarati, we got registered as volunteers for ISKCON to serve the pilgrims. ISKCON here was one of the NGOs to serve the pilgrims coming from different places. Among the various services provided by ISKCON, a few are free Prasadam distribution ( or the FOOD FOR LIFE) for 1.5 lac pilgrims, free accomodation for pilgrims, free medical support to pilgrims, Gita Donation, book distribution, lectures on spirituality and how to chant Hare Krsna to be happy. etc.

After having morning prasadam, we went for Nagar Sankirtan. We circumnavigated the island doing sankirtan and dancing. After returning from Nagar Sankirtan, we had our lunch Prasadam, and then we took some rest. From afternoon onwards, we setup some stalls for book, picture, chanting kits and other gift item distribution. It was very nice experience to help Srinivas and Tirthanga Prabhu in the stalls and interacting with many inquisitive pilgrims and answer their queries. Each day there were aratis, dramas, bhajans, lectures, nagar sankirtan, etc. We were most of the time busy serving and were not at all tired. I assisted Gauranga Chaitanya Prabhu to show pilgrims how to chant the HARE KRISHNA maha mantra and we did chanting sessions throughout the day. Similar was the schedule for the other days. I also made some new friends, one of them is Premavikasa Prabhu. He plays nice mridanga, sings great and what to speak of his dancing. It’s like there are springs in his joints. 😀 HARIBOL

On 14/01/2010, we had bath in Ganga Sagar at around 2 PM. The water was chilly though. We prayed to Ganga Mata to give us Krsna Prema. We didn’t come to Ganga Sagar to wash our sins. We came here to serve other devotees and we don’t care for sins or good karma. Wherever we are, if we are able to chant the holy name, we are liberated -> happy. The weather in the island was good, not that cold as we expected. Good water supply was arranged by the Government. The water was being pumped out continuously from under the ground, so it was warm. Nice tasty prasadam was cool. We got T-Shirts for being volunteers for Ganga Sagar 2010.

On Friday, 15/01/2010, we left for Durgapur with the devotees from Durgapur at around 12:30 PM. It was a whole day journey. It was also a solar eclipse that day. It was quite troublesome to get on the ferry. After that the journey was smooth. We reached Namkhana at around 4:30 PM. There the bus from Durgapur, ISKCON was waiting. We board the bus, and soon I fell asleep. We reached the ISKCON temple, Durgapur at 1:15 PM.

The trip to Ganga Sagar was indeed sanctifying and I hope I will be able to chant HARE KRISHNA with more devotion.

Now I am back to my mess and I have to work. Have to read LDD 3, top in the semesters, need to code and chant.