Hello everybody. Today I will explain how we can write our own driver for Linux. I will use the kernel 2.6.38.5 and you can get more information about how you can download and compile it in Installing Kernel 2.6.38.5 in the Ubuntu 11.04 (Natty Narwhal). However, if you would like to use another kernel version, you will need to download the kernel source through Ubuntu repository (case you have Ubuntu installed) or accessing the site www.kernel.org.
So, let’s start it. In your home folder, create a folder named MyDriver. Inside of it, create a file named mydriver.c with the following content:
#include <linux/module.h> // modules
#include <linux/kernel.h> // KERN_INFO
#include <linux/init.h> // macros
static int __init mydriver_start(void)
{
printk(KERN_INFO "***********************************\n");
printk(KERN_INFO "Loading my first kernel driver...\n");
printk(KERN_INFO "Welcome to the mydriver\n");
printk(KERN_INFO "***********************************\n");
return 0;
}
static void __exit mydriver_end(void)
{
printk(KERN_INFO "***********************************\n");
printk(KERN_INFO "Unloading mydriver...\n");
printk(KERN_INFO "***********************************\n");
}
//Defining the function that will be called when the module is loaded
module_init(mydriver_start)
//Defining the function that will be called when the module is unloaded
module_exit(mydriver_end)
Now, in the same folder create a file named Makefile with the content below:
obj-m = mydriver.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
After that, in the terminal, go to directory MyDriver and run:
make
This command will compile our driver and will be generated a file called mydriver.ko. So, it’s time to test the driver. Still in the terminal run the command bellow to load it:
sudo insmod mydriver.ko
To check if it was loaded, type:
lsmod
A list of modules will be displayed. Check if mydriver is in the list. If ok, you can see the message emitted by printk in /var/log/kern.log. To be easier to check this kind of message, open a new tab in the terminal and run:
tail -f /var/log/kern.log
Now, unload the module:
sudo rmmod mydriver.ko
Check the message emitted in the terminal where we run the command tail. Doing that, you can load and unload mydriver and check the messages in other terminal.
With the command above we loaded and unloaded the driver manually, but we can leave the Linux does that automatically at boot (load) and shutdown (unload) time. In the terminal, run:
sudo mkdir /lib/modules/$(uname -r)/kernel/drivers/mydriver
cd ~/MyDriver
sudo cp mydriver.ko /lib/modules/$(uname -r)/kernel/drivers/mydriver
We created a directory named mydriver to the standard paths and copied our driver to this directory. So, to finish, we need to inform the Linux to load it. Open the file /etc/modules with your preferred text editor and add in the end: mydriver. Come back to terminal and run:
sudo depmod -a
That command will create a dependency file for loadable modules for the kernel. The command modprobe will use this file to load the right modules.
To test that, just restart your Linux and check if the module was loaded with the command lsmod and see the log messages in /var/log/kern.log.
That’s it. See you next time.