Writing device drivers in Linux: A brief tutorial
A quick and easy intro to writing device drivers for Linux like a true kernel developer!
Download the whole article as PDF
- 2006-04-26
- Server side | Advanced
-
Write a full post in response to this!
This content was sponsored by:
“Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?” Linus Torvalds
Pre-requisites
In order to develop Linux device drivers, it is necessary to have an understanding of the following:
- C programming. Some in-depth knowledge of C programming is needed, like pointer usage, bit manipulating functions, etc.
- Microprocessor programming. It is necessary to know how microcomputers work internally: memory addressing, interrupts, etc. All of these concepts should be familiar to an assembler programmer.
There are several different devices in Linux. For simplicity, this brief tutorial will only cover type char devices loaded as modules. Kernel 2.6.x will be used (in particular, kernel 2.6.8 under Debian Sarge, which is now Debian Stable).
User space and kernel space
When you write device drivers, it’s important to make the distinction between “user space” and “kernel space”.
- Kernel space. Linux (which is a kernel) manages the machine’s hardware in a simple and efficient manner, offering the user a simple and uniform programming interface. In the same way, the kernel, and in particular its device drivers, form a bridge or interface between the end-user/programmer and the hardware. Any subroutines or functions forming part of the kernel (modules and device drivers, for example) are considered to be part of kernel space.
- User space. End-user programs, like the UNIX
shellor other GUI based applications (kpresenterfor example), are part of the user space. Obviously, these applications need to interact with the system’s hardware . However, they don’t do so directly, but through the kernel supported functions.
All of this is shown in figure 1.

Figure 1: User space where applications reside, and kernel space where modules or device drivers reside
Interfacing functions between user space and kernel space
The kernel offers several subroutines or functions in user space, which allow the end-user application programmer to interact with the hardware. Usually, in UNIX or Linux systems, this dialogue is performed through functions or subroutines in order to read and write files. The reason for this is that in Unix devices are seen, from the point of view of the user, as files.
On the other hand, in kernel space Linux also offers several functions or subroutines to perform the low level interactions directly with the hardware, and allow the transfer of information from kernel to user space.
Usually, for each function in user space (allowing the use of devices or files), there exists an equivalent in kernel space (allowing the transfer of information from the kernel to the user and vice-versa). This is shown in Table 1, which is, at this point, empty. It will be filled when the different device drivers concepts are introduced.
| Events | User functions | Kernel functions |
| Load module | ||
| Open device | ||
| Read device | ||
| Write device | ||
| Close device | ||
| Remove module |
Table 1. Device driver events and their associated interfacing functions in kernel space and user space.
Interfacing functions between kernel space and the hardware device
There are also functions in kernel space which control the device or exchange information between the kernel and the hardware. Table 2 illustrates these concepts. This table will also be filled as the concepts are introduced.
Write a full post in response to this!
Similar articles
Do you like this post?
Vote for it!
Copyright information
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is available at http://www.gnu.org/copyleft/fdl.html.
Biography
Xavier Calbet: Xavier Calbet (xcalbet AT googlemail DOT com) is a long time free software user who started using Linux distributions in the ancient times when Slackware had to be installed using tens of floppy disks. He is currently working on his two pet projects: a meteorological field and satellite image display system, SAPO, and the best available free numerical computer language to date, PDL (Perl Data Language).
- Login or register to post comments
- 535648 reads
- Printer friendly version (unavailable!)




Great,
Submitted by circulus on Thu, 2006-04-27 11:03.
Vote!Very nice well written, this nice step by step tutorial is exactly for me.
---
"... Nothing Runs Like A Python ..."
Excellent job
Submitted by Anonymous visitor on Wed, 2006-10-04 11:31.
Vote!Thanks for the Excellent job of making LINUX drivers easy to understand in a very very short time.
Great................
Submitted by deepak george (not verified) on Thu, 2007-09-13 09:50.
Vote!Great................
Thank you
Submitted by Anonymous visitor on Mon, 2006-08-14 20:13.
Vote!Great tutorial!
Amazing
Submitted by Anonymous visitor on Wed, 2006-08-16 13:10.
Vote!I am amazed!! How have you managed to get this thing so simple? A real good job.
Sundip Sharma
WOW !!!!!! simply fantastic work
Submitted by Anonymous visitor on Wed, 2006-08-30 08:12.
Vote!Thanks
a great article with ample scope of learning the skeleton of linux device drivers and the commonly used functions in them.........so simple and presented in quite an interesting way, so never lets u feel bored.
great work
Great simple tutorial
Submitted by Anonymous visitor on Thu, 2006-08-31 00:29.
Vote!After kernel 2.4 the kernel compilation and adding up of modules has changed drastically. This tutorial helped me compile and insert my module. Thanks. Effort whole heartedly appreciated.
Kabeer Ahmed.
WOW!!!!!!!!!!!1 Great.....
Submitted by Anonymous visitor on Thu, 2006-08-31 17:30.
Vote!A great article with ample scope of learning with
nice step by step procedure..
Thank's
Prakash.
Its a good
Submitted by Anonymous visitor on Fri, 2006-09-08 12:40.
Vote!Its a good information.
Regards, Rajesh
No word's to say
Submitted by Anonymous visitor on Sun, 2006-09-17 17:37.
Vote!Great Work !!!
Linux is not an OS
Submitted by Anonymous visitor on Fri, 2006-09-22 11:41.
Vote!"Linux's kernel" has no sense, because Linux is a kernel
Query on reading from the device
Submitted by Vishwanatha K on Fri, 2006-09-29 10:16.
Vote!Hi All,
I actually tried memory.ko as below:
$echo -n abcdef > /dev/memory
$cat /dev/memory
f
But I've got only the last written character i.e., 'f'.
I changed the code so that the buffer can hold upto 10 bytes (by giving 10 as a parameter to kmalloc and read,write calls) but still I'm getting the last input character. Can any one post teh answer here...
--
Regards,
Vishwa
Character driver...
Submitted by bzamora on Tue, 2008-01-08 23:32.
Vote!Hi Vishwa,
On page 4 of the tutorial, where the author shows the mknod command, he states that the c identifies this driver as a character driver. My guess is that is the reason that you only see the last letter. I believe character drivers will only allow you to receive the most recent character that was written... ie the 'f'.
A really Helpful link
Submitted by Anonymous visitor on Tue, 2006-10-03 10:08.
Vote!A really good and Helpful link for newbies!!!!
To start my career as programmer in device drivers
Submitted by jadhav_raghav on Thu, 2006-10-12 14:48.
Vote!hi,
This one is good article.
I want to learn more on this, and can u help me get more info about device drivers.
Thanks,
Raghavendra
WOW A MASTERPIECE
Submitted by Anonymous visitor on Fri, 2006-10-20 11:06.
Vote!Gud work by u.A gud guide for a layman in the fiels of device drivers
Heartly appreciated
problem with driver software
Submitted by Anonymous visitor on Tue, 2006-10-31 10:23.
Vote!hello sir whhen i have compiled my sorce code then i face an error that "fops size is not known " pleee send me some information to remove this error
mu email id is erabhinandan@gmail.com
Linux extra-versioning problem and another useful link
Submitted by Anonymous visitor on Tue, 2006-10-31 11:10.
Vote!Thank you for your excellent tutorial!
That's true that the first step is always the more hard to achieve, and guides like this really simplify the achievement of the basic know-how necessary to get real job done.
When I first tried the tutorial I get this error when trying to insmod my module nothing.ko:
insmod: error inserting nothing.ko: -1 Invalid module format
while dmesg gave me:
nothing: version magic '2.6.17.11.061003.eramil SMP mod_unload PENTIUM4 REGPARM gcc-4.1' should be '2.6.17.11.061003.eramil SMP mod_unload PENTIUM4 REGPARM gcc-4.0'
It was that I like to add a customized extra version to my kernel compiling it, while the extra version in the source tree Makefile was still ".11".
To fix this I changed manually the Makefile, updating the extra version line with the current linux extra version, then I started to compile with make (but I control-C-ed quite soon, after the compilation of the version.h file, that you can eventually change by hand).
Then, since I compiled the kernel with gcc-4.0 and I had installed the gcc-4.1 version, I changed the gcc symlink with:
sudo ln -sf /usr/bin/gcc-4.0 /usr/bin/gcc
Now the gcc symlink (used to compile the module) points to the same version of gcc used to compile the kernel: trick done!
Another useful link:
http://www.openaddict.com/documents/lkmpg/x30.html
HTH
Cheers
Compiling with Kernel Version 2.4.20-8
Submitted by Anonymous visitor on Thu, 2006-11-02 04:54.
Vote!hi,
This is Priya here. I have installed Redhat Linux verion 9 on my system. Kernel version 2.4.20-8. Could I Please know how to compile and insert the modules given in this tutorial, with the associated makefiles.Please help me. My mail id :priyasophy_1982@yahoo.com
Reply
Submitted by Anonymous visitor on Fri, 2007-01-26 15:37.
Vote!Its better if you use kernel 2.6.x.x.2.Because the Linux systems now supports 2.6 kernel and the interface for 2.6 is simple and totally different than 2.4.Mention the topic on which you are working.
If I have a knowldge about that i will surely help you.Do post ur queries on prem38_kamble @rediffmail.com
This makefile worked for me.
Submitted by Zafeer (not verified) on Thu, 2007-11-01 05:22.
Vote!This makefile worked for me. I got it off another site:
obj-m += memory.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
So the only difference is you only compile memory.c, although you can just add more files after memory.o like hello.o, etc to compile hello.c
Hope this helps.
Works with CentOS 5...
Submitted by bzamora on Tue, 2008-01-08 23:25.
Vote!Hi...
Thanks for this sample. This worked great with CentOS 5.
very nice work !!!!!!!!!!
Submitted by Anonymous visitor on Mon, 2006-11-06 08:35.
Vote!I appreciate for the effort took for explaining device driver writing in step by step manner...
really great work...
Thank you,
Murali
thanks alot .............
Submitted by Anonymous visitor on Tue, 2006-11-14 10:50.
Vote!thanks man .. .. nice tutorial
regards
SuLtAn
REALLY A GREAT HELP FOR
Submitted by Anonymous visitor on Tue, 2006-11-21 11:43.
Vote!REALLY A GREAT HELP FOR STARTERS...
ARUN
Cannot unload module
Submitted by Anonymous visitor on Sat, 2006-11-25 06:31.
Vote!Dear sir,
This is a very good tutorial you have here. It is very cool as it explains the fundamental really well.
I tried out the first 2 section of the tutorial but am stuck.
I have created the module nothing.ko and inserted it into the kernel.
I am able to see the module using lsmod
When I tried to rmmod, the system keeps giving me the following error:
ERROR: Removing 'nothing': Device or resource busy
My lsmod | grep nothing gives me
nothing 2688 0 [permanent]
Is there a way to unload this module
Very much appreciate your help
Thanks,
Alex
Not accurate article!
Submitted by Anonymous visitor on Sat, 2006-11-25 19:21.
Vote!My comments:
To be complete tutorial it must be accurate. It is not so.
For example, memory driver part is completely hide extensions of files and creating makefile in case if you build module from multiply files.
I think it should be added directly in article or,as minimum, in comments.
d BEST
Submitted by Anonymous visitor on Tue, 2006-11-28 12:22.
Vote!Thank you very much for posting such an astonish article for all..
God Bless all..
GOOD
Submitted by Anonymous visitor on Tue, 2006-11-28 13:10.
Vote!Thank u for giving an easy to learn tutorial about device drivers..
U can also add some more details about functions which will be more helpful...
Very Good
Submitted by Anonymous visitor on Tue, 2006-12-05 10:01.
Vote!The Tutorial help me lot
good article
Submitted by Anonymous visitor on Wed, 2006-12-20 01:47.
Vote!As the title says,....
Simple to understand and grasp the important things.
Thanks a lot m8
George
NICE ONE
Submitted by Anonymous visitor on Wed, 2006-12-20 13:39.
Vote!VERY NICE ARTICLE.
THANKS A LOT.
K.RAJASEKAR
good job
Submitted by Anonymous visitor on Sun, 2006-12-24 15:25.
Vote!good job
Its the Best that I have come accross
Submitted by Anonymous visitor on Mon, 2006-12-25 06:52.
Vote!It motivates a non-driver-writer to jump into the arena that is always thought to be only for the real techies.
Very well done
Submitted by Anonymous visitor on Tue, 2006-12-26 00:02.
Vote!Really good tutorial on drivers. I challenge us to do the exercises, specifically a device that doesn't work yet.
Thanks for this important contribution!
Regards,
Help on Kernel Keyboard Driver
Submitted by Anonymous visitor on Tue, 2006-12-26 05:33.
Vote!Hai everyone,
IN LINUX KERNEL VERSION-2.6.8-24 THE KEYBOARD DRIVER FILE KEYBOARD.C IS USED.I WOULD LIKE TO MODIFY THIS FILE SUCH THAT ON A TOGGLE KEY I WOULD LIKE TO READ DATA FROM A FILE.I USED SYSTEM CALLS LIKE OPEN,CLOSE,READ.BUT AN ERROR MESSAGE SAYING THAT THE HEADER FILE UNISTD.H INCLUDING THIS CALLS IS NOT AVAILABLE IS ENCOUNTERED.CAN ANY BODY HELP ME IN THIS CONTEXT.
After you've done that
Submitted by Anonymous visitor (not verified) on Fri, 2007-04-27 22:07.
Vote!After you've done that you could write a driver that prevents your caps-lock key from getting stuck.
hi
Submitted by Anonymous visitor on Tue, 2007-01-02 17:12.
Vote!good job done
Only bit-1 lights up after module loads
Submitted by Anonymous visitor on Fri, 2007-01-19 01:42.
Vote!First off, thanks for the great tutorial, Xavier.
I am trying to complete the LED section of the tutorial. I am running Ubuntu Server 6.10, kernel 2.6.17-10, and I removed lp, parport_pc, and parport. I have successfully compiled parlelport.ko and I can verify that the module has loaded with:
$ cat /proc/ioports
..
0378-0378 : parlelport
The circuit shows many lights blinking at boot time. After boot, bit-2 and bit-3 stay lit. They remain that way until the computer is rebooted -- or until the monitor goes to sleep. All lights go black while the monitor sleeps. The only response I can generate is that bit-1 lights after inserting parlelport.ko. It stays lit until the computer is rebooted.
The only thing that looks suspicious is this:
$:~/dev$ make -C /usr/src/linux-source-2.6.17 M=`pwd` modules
make: Entering directory `/usr/src/linux-source-2.6.17'
CC [M] /home/sam/dev/parlelport.o
/home/sam/dev/parlelport.c:31: warning: initialization from incompatible pointer type
..
Line 31 corresponds to:
write: parlelport_write
But my parlelport_write() is the same as the tutorial's.
Any pointers?
Thanks,
Sam
I have more or less the same
Submitted by Anonymous visitor on Sun, 2007-02-11 12:00.
Vote!I have more or less the same issue.
after booting the LED is OFF but when I remove all parport related modules, the LED gets ON and the parlelport driver does not work. If I manage to avoid parport module at boot and insmod parlelport after boot, things seems OK (I can set On or Off the LED).
I guess the parport driver when manualy unloaded (rmmod) let the chipset in a state that prevent from outputting data from the port (may in input mode in place of output mode)
I tried to force the output mode (0x37A control reg) with no success
how did you remove lp, parport_pc, and parport
Submitted by leneth (not verified) on Wed, 2007-08-22 10:19.
Vote!Hi!
How did you remove lp, parport_pc, and parport ? I think i need to do this becuase i will get a device or resource busy error.
Thanks,
Leneth
Very Well Explained
Submitted by Anonymous visitor on Thu, 2007-01-25 23:29.
Vote!Fantastic Job. You have described the driver structure in simple and best manner.
Thanks.AJ
Damn useful !!! Well done
Submitted by Anonymous visitor on Thu, 2007-02-01 19:41.
Vote!Damn useful !!! Well done buddy :-*
Good one.
Submitted by Anonymous visitor on Mon, 2007-02-05 07:18.
Vote!Hello,
Really nice tutorial...it helped me a lot..Practical Egs are good..
Keep the good work going..
Excellent, Quick & Handy..,
Submitted by Anonymous visitor on Thu, 2007-02-08 07:05.
Vote!This is really nice & handy tutorial.
Can this guy give similar tutorials for GCC,Shell Scripting..? It would be really good to have such tutorials..
Very Helpful!
Submitted by Anonymous visitor on Mon, 2007-02-12 22:14.
Vote!A really handy overview!
write a kernel for linux
Submitted by Anonymous visitor on Sun, 2007-03-04 20:06.
Vote!how we write a kernel for linux? what is code of this problem?
SIMPLE & COOL
Submitted by Anonymous visitor on Mon, 2007-03-05 10:50.
Vote!The tutorial has been simple, I had no idea where to start with.
This has given me a good introduction to DD programming & kernel modules.
-Arjun
Compilation problem
Submitted by Anonymous visitor on Thu, 2007-03-08 21:58.
Vote!I am using Fedora core 5. I could not compile the very first version of "nothing" with just licence line in it.
Here is what I got.
make -C /usr/src/linux/kernel M=`pwd` modules
make: Entering directory `/usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15.i686/kernel'
make: *** No rule to make target `modules'. Stop.
make: Leaving directory `/usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15.i686/kernel'
Is the procedure different for Fedora? Thanks in advance.
Great
Submitted by kirz on Fri, 2007-03-16 12:42.
Vote!Its really simple and informative.....
Good work
Submitted by Anonymous visitor on Fri, 2007-03-16 17:19.
Vote!A really interesting tutorial. A little short but the main explanations are present.
Thanks.
KirgliZ
tutorial(device driver)
Submitted by Anonymous visitor on Thu, 2007-03-22 05:31.
Vote!its really excellent....
amazing...
His presentation is too good and simple....
Yeah
Submitted by kirz on Thu, 2007-03-29 11:44.
Vote!