Before writing driver for linux system we have to have knowledge of C programming and some Linux Os knowledge. basically drivers are known as kernel modules. modules are a collection of code which loaded and unloaded into the kernel upon demand.

Basic Commands:

lsmod: By running lsmod command on terminal we can see the loaded modules into the kernel.

Linux Kernel Programming Hello World

insmod: install loadable kernel module
insmod tries to link a module into the running kernel.
rmmod:unload loadable modules
rmmod unloads loadable modules from the running kernel.

Development environment Set up.
Three are lots of linux version, like , Debian, Ubuntu, Redhat, Fedora, CentOs etc. i am showing environ ment setup for ubuntu desktop.

  1. open terminal run below command

               apt-get install linux-source linux-header build-essensial fakeroot

               it will install linux source in /usr/src directory and build directory.

Hello world Example
open a simple text editor and paste this below given code and save this as a hello.c.

Code explanation:
kernel drivers have at least two functions ,one is called when we insert the module in kernel by command insmod and seconde one is called when we remove the module.
above written code has two functions named as init_module and cleanup_module.
from the kernel.
printk() is similar to printf function in c but it is always available in kernel space. in above code in printk KER_ALERT is priority.

paste below code and save as a Makefile

obj-m = hello.o
KVERSION = $(shell uname -r)
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Open ternimal and go to the directory in which Makefile is and type make and hit enter. hello.ko file will be created in the directory.
when you insert the hello.ko in the kernel hello world will be printed to terminal and during removal it says goodbye world.