Escolar Documentos
Profissional Documentos
Cultura Documentos
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
Subscribe to Print Edition Search
HOME
REVIEWS
HOW-TOS
CODING
INTERVIEWS
FEATURES
OVERVIEW
BLOGS
SERIES
IT ADMIN
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom
By Anil Kumar Pugalia on December 1, 2010 in Coding, Developers 71 Comments and 0 Reactions
Search for:
Search
Get Connected
This article, which is part of the series on Linux device drivers, deals with the concept of dynamically loading drivers, first writing a Linux driver, before building and then loading it.
Shweta and Pugs reached their classroom late, to find their professor already in the middle of a lecture. Shweta sheepishly asked for his permission to enter. An annoyed Professor Gopi responded, Come on! You guys are late again; what is your excuse, today? Pugs hurriedly replied that they had been discussing the very topic for that days class device drivers in Linux. Pugs was more than happy when the professor said, Good! Then explain about dynamic loading in Linux. If you get it right, the two of you are excused! Pugs knew that one way to make his professor happy was to criticise Windows. He explained, As we know, a typical driver installation on Windows needs a reboot for it to get activated. That is really not acceptable; suppose we need to do it on a server? Thats where Linux wins. In Linux, we can load or unload a driver on the fly, and it is active for use instantly after loading. Also, it is instantly disabled when unloaded. This is called dynamic loading and unloading of drivers in Linux. This impressed the professor. Okay! Take your seats, but make sure you are not late again. The professor continued to the class, Now you already know what is meant by dynamic loading and unloading of drivers, so Ill show you how to do it, before we move on to write our first Linux driver.
RSS Feed
Follow
+2,394
Figure 1.
www.linuxforu.com/2010/12/writing-your-first-linux-driver/
1/11
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
Find us on Facebook
Popular
Comments
Tag cloud
To dynamically load or unload a driver, use these commands, which reside in the / s b i n directory, and must be executed with root privileges:
l s m o d lists currently loaded modules i n s m o d< m o d u l e _ f i l e > inserts/loads the specified module file m o d p r o b e< m o d u l e > inserts/loads the module, along with any dependencies r m m o d< m o d u l e > removes/unloads the module
Lets look at the FAT filesystem-related drivers as an example. Figure 2 demonstrates this complete process of experimentation. The module files would be f a t . k o ,v f a t . k o , etc., in the f a t(v f a tfor older kernels) directory under / l i b / m o d u l e s / ` u n a m er ` / k e r n e l / f s . If they are in compressed . g zformat, you need to uncompress them with g u n z i p , before you can i n s m o d them.
The v f a tmodule depends on the f a tmodule, so f a t . k oneeds to be loaded first. To automatically perform decompression and dependency loading, use m o d p r o b einstead. Note that you shouldnt specify the . k oextension to the modules name, when using the m o d p r o b e command. r m m o dis used to unload the modules.
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
Before we write our first driver, lets go over some concepts. A driver never runs by itself. It is similar to a library that is loaded for its functions to be invoked by a running application. It is written in C, but lacks a m a i n ( )function. Moreover, it will be loaded/linked with the kernel, so it needs to be compiled in a similar way to the kernel, and the header files you can use are only those from the kernel sources, not from the standard / u s r / i n c l u d e . One interesting fact about the kernel is that it is an object-oriented implementation in C, as we will observe even with our first driver. Any Linux driver has a constructor and a destructor. The modules constructor is called when the module is successfully loaded into the kernel, and the destructor when r m m o dsucceeds in unloading the module. These two are like normal functions in the driver, except that they are specified as the init and exit functions, respectively, by the macros m o d u l e _ i n i t ( )and m o d u l e _ e x i t ( ) , which are defined in the kernel header
m o d u l e . h .
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 / *o f d . cO u rF i r s tD r i v e rc o d e* / # i n c l u d e< l i n u x / m o d u l e . h > # i n c l u d e< l i n u x / v e r s i o n . h > # i n c l u d e< l i n u x / k e r n e l . h > s t a t i ci n t_ _ i n i to f d _ i n i t ( v o i d )/ *C o n s t r u c t o r* / { p r i n t k ( K E R N _ I N F O" N a m a s k a r :o f dr e g i s t e r e d " ) ; r e t u r n0 ; } s t a t i cv o i d_ _ e x i to f d _ e x i t ( v o i d )/ *D e s t r u c t o r* / { p r i n t k ( K E R N _ I N F O" A l v i d a :o f du n r e g i s t e r e d " ) ; } m o d u l e _ i n i t ( o f d _ i n i t ) ; m o d u l e _ e x i t ( o f d _ e x i t ) ; M O D U L E _ L I C E N S E ( " G P L " ) ; M O D U L E _ A U T H O R ( " A n i lK u m a rP u g a l i a< e m a i l _ a t _ s a r i k a p u g s _ d o t _ c o m > " ) ; M O D U L E _ D E S C R I P T I O N ( " O u rF i r s tD r i v e r " ) ;
Given above is the complete code for our first driver; lets call it o f d . c . Note that there is no s t d i o . h(a user-space header); instead, we use the analogous k e r n e l . h(a kernel space header). p r i n t k ( )is the equivalent of p r i n t f ( ) . Additionally, v e r s i o n . his included for the module version to be compatible with the kernel into which it is going to be loaded. The M O D U L E _ *macros populate module-related information, which acts like the modules signature.
With the C code (o f d . c ) and M a k e f i l eready, all we need to do is invoke m a k eto build our first driver (o f d . k o ).
$m a k e m a k eC/ u s r / s r c / l i n u xS U B D I R S = . . .m o d u l e s m a k e [ 1 ] :E n t e r i n gd i r e c t o r y` / u s r / s r c / l i n u x ' C C[ M ] . . . / o f d . o B u i l d i n gm o d u l e s ,s t a g e2 . M O D P O S T1m o d u l e s C C . . . / o f d . m o d . o L D[ M ] . . . / o f d . k o m a k e [ 1 ] :L e a v i n gd i r e c t o r y` / u s r / s r c / l i n u x '
Summing up
www.linuxforu.com/2010/12/writing-your-first-linux-driver/ 3/11
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
Once we have the o f d . k ofile, perform the usual steps as the root user, or with s u d o .
#s u #i n s m o do f d . k o #l s m o d|h e a d1 0
While the students were trying their first module, the bell rang, marking the end of the session. Professor Gopi concluded, Currently, you may not be able to observe anything other than the l s m o dlisting showing the driver has loaded. Wheres the p r i n t koutput gone? Find that out for yourselves, in the lab session, and update me with your findings. Also note that our first driver is a template for any driver you would write in Linux. Writing a specialised driver is just a matter of what gets filled into its constructor and destructor. So, our further learning will be to enhance this driver to achieve specific driver functionalities.
Related Posts:
Device Drivers, Part 17: Module Interactions Device Drivers, Part 4: Linux Character Drivers Device Drivers, Part 5: Character Device Files Creation & Operations Device Drivers, Part 3: Kernel C Extras in a Linux Driver Device Drivers, Part 16: Kernel Window Peeping through /proc
Tags: C, device drivers, dynamic loading, insmod, kernel source tree, LFY December 2010, linux device drivers, Linux Device Drivers Series, Loadable kernel modules, loading drivers, lsmod, modprobe, rmmod
Previous Post
Next Post
What's this?
Citizens Over 50 May Qualify to Get $20,500 this Year Moneynews 6 Simple Tips to Help You Lose Belly Fat Stack Fun Visits With Senior Citizens
Suite101
What it Takes to be an Open Source Expert 1 comment PHP Development: A Smart Career Move 4 comments A Simple guide to building your own Linux Kernel 1 comment
9 Things You Can Do Today to Boost Your Retirement Income Citi Women & Co.
Share
a month ago
After I upgraded to Ubuntu 13.04, the problem got rectified, only thing is, in the sample Makefile, I had to change Kernel_Source := /lib/modules//3.8.0-23-generic/build. Thanks for this article.
Reply Share
anil_pugalia
> Ajey
a month ago
A jey
www.linuxforu.com/2010/12/writing-your-first-linux-driver/
a month ago
4/11
7/17/13
A jey
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
a month ago
Namaste, When I used Kernel_Source as KERNEL_SOURCE := /usr/src/linuxheader-3.5.0-30-generic --> I get an error No such file or directory and when I change Kernel_Source as KERNEL_SOURCE := /lib/modules/3.5.0-30-generic/build (this was recommended in one of comments below), then I get following error. Not able to proceed beyond this .... make -C /lib/modules/3.5.0-30-generic/build SUBDIRS=/ modules make[1]: Entering directory `/usr/src/linux-headers-3.5.0-30-generic' scripts/Makefile.build:128: kbuild: Makefile.build is included improperly make[2]: *** No rule to make target `kernel/bounds.c', needed by `kernel/bounds.s'. Stop. make[1]: *** [_module_] Error 2 make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-30-generic' make: *** [default] Error 2
Reply Share
anil_pugalia
> Ajey
a month ago
uday
Seems like, there is some problem with the installation of the kernel headers with this version of the distro you are using.
Reply Share
4 months ago
sir,i have created a makefile and added obj-m := ofd.o in it. After saving and exiting the terminal,i have given command like this make -C /usr/src/linux-headers-generic-pae-3.2.0-12 SUBDIRS=$PWD modules but it is showing "no rule to make"...pls help me. regards, uday.
Reply Share
anil_pugalia
> uday
4 months ago
janak iram
Make sure that you use *only tabs* to indent your makefile. You get such errors, if you have used spaces instead.
Reply Share
4 months ago
I use your code and compile by make command then i got following errors make -C /usr/src/linux-headers-3.2.0-29 SUBDIRS=/home/shivaram/jani modules make[1]: Entering directory `/usr/src/linux-headers-3.2.0-29' ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/config/auto.conf are missing. Run 'make oldconfig && make prepare' on kernel src to fix it. WARNING: Symbol version dump /usr/src/linux-headers-3.2.0-29/Module.symvers is missing; modules will have no dependencies and modversions. Building modules, stage 2. /usr/src/linux-headers-3.2.0-29/scripts/Makefile.modpost:42: include/config/auto.conf: No such file or directory make[2]: *** No rule to make target `include/config/auto.conf'. Stop. make[1]: *** [modules] Error 2 make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-29' make: *** [default] Error 2s And also i tried with make oldconfig&&make prepare then i got problem like make: *** No rule to make target `oldconfig'. Stop. i installed ubuntu through vmware. one of my friend said it's the problem u should install normal way..
Reply Share
anil_pugalia
> janakiram
Share
4 months ago
www.linuxforu.com/2010/12/writing-your-first-linux-driver/
5/11
7/17/13
enigma
Reply
Share
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
4 months ago
anil_pugalia
> enigma
4 months ago
t us hraj
In this particular case, even := is fine as, we are limiting the build to the drivers directory and more to our driver, alone.
Reply Share
4 months ago
I use your code and compile by make command then i got following error tushar@tushar-SVE14112ENW:~/devdrv$ make make -C /usr/src/linux-headers-3.5.0-17 SUBDIRS=/home/tushar/devdrv modules make[1]: Entering directory `/usr/src/linux-headers-3.5.0-17' WARNING: Symbol version dump /usr/src/linux-headers-3.5.0-17/Module.symvers is missing; modules will have no dependencies and modversions. Building modules, stage 2. MODPOST 0 modules /bin/sh: 1: scripts/mod/modpost: not found make[2]: *** [__modpost] Error 127 make[1]: *** [modules] Error 2 make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-17'
anil_pugalia
> tushraj
4 months ago
Check out the comments below for the solution to the same problem.
Reply Share
darc y . diao@gmail. c om
5 months ago
I use your code and compile successfully. But when insmod by "sudo insmod ofd.ko", error prompt-" insmod: error inserting 'ofd.ko': -1 Invalid module format". Can you tell me why? Thx.
Reply Share
darc y . diao@gmail. c om
> darcy.diao@gmail.com
5 months ago
t us har
anil_pugalia
> darcy.diao@gmail.com
Share
5 months ago
Make sure that the kernel headers you used to compile the driver has the same version as the running kernel.
Reply
5 months ago
when i do make this error comes please tell solution sai@ubuntu:~/tushar$ make make -C /usr/src/linux-headers-3.5.0-17 SUBDIRS=/home/sai/tushar modules make[1]: Entering directory `/usr/src/linux-headers-3.5.0-17' ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/config/auto.conf are missing.
www.linuxforu.com/2010/12/writing-your-first-linux-driver/ 6/11
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
Run 'make oldconfig && make prepare' on kernel src to fix it. WARNING: Symbol version dump /usr/src/linux-headers-3.5.0-17/Module.symvers is missing; modules will have no dependencies and modversions.
see more
Reply t us har
Share
> tushar
Reply
5 months ago
anil_pugalia
> tushar
5 months ago
Do as the message says: make oldconfig And then, make modules_prepare Both in the linux-headers folder. And then retry.
Reply t us har Share
1> when i do make oldconfig in linux-headers-3.5..0-17 folder i got this error HOSTCC scripts/basic/fixdep scripts/basic/fixdep.c:433:1: fatal error: opening dependency file scripts/basic/.fixdep.d: Permission denied compilation terminated. make[1]: *** [scripts/basic/fixdep] Error 1 make: *** [scripts_basic] Error 2 tushar@Sai:/usr/src/linux-headers-3.5.0-17$ 2> and after that if i do make modules_prepare i got this error HOSTCC scripts/basic/fixdep
> anil_pugalia
5 months ago
t us har
Reply
Share
> tushar
Reply
5 months ago
t us har
plz reply
Share
anil_pugalia
> tushar
5 months ago
> anil_pugalia
5 months ago
1)when i do sudo make oldconfig then it is fine 2) but when i do sudo make modules_prepare i got fooliowing error tushar@Sai:/usr/src/linux-headers-3.5.0-17$ sudo make modules_prepare scripts/kconfig/conf --silentoldconfig Kconfig make[1]: *** No rule to make target `/usr/src/linux-headers3.5.0-17/arch/x86/syscalls/syscall_32.tbl', needed by `arch/x86/syscalls/../include/generated/asm/unistd_32.h'. Stop. make: *** [archheaders] Error 2 tushar@Sai:/usr/src/linux-headers-3.5.0-17$
www.linuxforu.com/2010/12/writing-your-first-linux-driver/ 7/11
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
Reply
Share
anil_pugalia
> tushar
5 months ago
How about sudo make prepare? If not, then some issue with the headers package installation.
Reply Share
S unil S
hi.. i am facing the same problem Tushar was facing and got exact same error. I desperately want to move forward to next tutorial but can get rid of this compile issue... I know u said about headers package. But can you please point out exact issue.. Here is my system info: Linux sunilshahu 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 athlon i686 GNU/Linux and after doing what you suggested here is the error log i got: root@sunilshahu:/usr/src/linux-headers-3.5.0-17# make oldconfig HOSTCC scripts/basic/fixdep HOSTCC scripts/kconfig/conf.o SHIPPED scripts/kconfig/zconf.tab.c SHIPPED scripts/kconfig/zconf.lex.c
see more
> anil_pugalia
18 days ago
Reply
Share
anil_pugalia
> Sunil S
17 days ago
Does make modules_prepare also give the same problem? If yes, then as I said earlier, you also have the some issue with the headers package installation. Try reinstalling the linux-headers package using apt-get. Or, upgrade to some other version of Ubuntu. Ajey upgraded to Ubuntu 13.04 and it worked fine - see the comment at top.
Reply Share
S unil S
> anil_pugalia
17 days ago
ok. I did what Ajey said int the above discussion and changed make file as below KERNEL_SOURCE := /lib/modules/3.5.0-17-generic/build And it worked.. It compiled the ofd.c and i loaded and unloaded it successfully on my ubuntu 12.10. So, does it means my linux sources or headers are at above directory and not at /usr/src/linux-headers-3.5.0-17-generic and /usr/src/linux-source-3.5.0 , because i tried both earlier?
Reply Share
anil_pugalia
> Sunil S
17 days ago
Typically /lib/modules/.../build is a link to the corresponding source, these modules are built from - making it work with high probability. Please check the link and post here what does it point to. That would definitely give insight into this common problem.
Reply Share
S unil S
hi, i have to change the system after above comment and missed to see the link on that system. now i am using Linux sunils 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux my code compiled with this with make file www.linuxforu.com/2010/12/writing-your-first-linux-driver/
8/11
> anil_pugalia
15 days ago
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
my code compiled with this with make file obj-m += ofd.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 and when looking at the lib/modules/.../buld i find as below. but source link (/build/buildd/linux-3.2.0) was red colored(dont know why) and when looking what it points to i found that there is no such directory(but still i got my module compiled)
see more
Reply
Share
anil_pugalia
> Sunil S
15 days ago
What output have you sent? Please send the output of: ls -l /lib/modules/`uname -r`/
Reply Share
S unil S
> anil_pugalia
5 days ago
root@sunils:~# ls -l /lib/modules/`uname -r`/ total 4276 lrwxrwxrwx 1 root root 39 May 31 16:56 build -> /usr/src/linuxheaders-3.2.0-23-generic drwxr-xr-x 2 root root 4096 Apr 25 2012 initrd drwxr-xr-x 10 root root 4096 Apr 25 2012 kernel -rw-r--r-- 1 root root 685840 Apr 25 2012 modules.alias -rw-r--r-- 1 root root 667167 Apr 25 2012 modules.alias.bin -rw-r--r-- 1 root root 5724 Apr 11 2012 modules.builtin -rw-r--r-- 1 root root 7132 Apr 25 2012 modules.builtin.bin
see more
Reply
Share
anil_pugalia
> Sunil S
4 days ago
manoj
I hope that clarifies all the doubts. The link is correctly pointing to the headers, which are working for you. Pasting it here for reference: lrwxrwxrwx 1 root root 39 May 31 16:56 build -> /usr/src/linuxheaders-3.2.0-23-generic
Reply Share
5 months ago
Share
y oges h
6 months ago
why every function in the device driver is named as static. As static fuction are not visible outside of the file scope. Then, How these driver function gets called by user space application..
Reply Share
anil_pugalia
> yogesh
6 months ago
s at hees aran
You are correct that when static, functions are not visible outside of the file scope, but only by their names. Note that, they could be still accessed anywhere by their addresses, and that's what we do with all these driver functions - populate their addresses into the struct file_operations and make it available to VFS & hence userspace, through the struct cdev.
Reply Share
7 months ago
www.linuxforu.com/2010/12/writing-your-first-linux-driver/
9/11
7/17/13
s at hees aran
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
7 months ago
Great example to start with. Thanks for the article :) Motivates to read more
Reply Share
anil_pugalia
> satheesaran
Share
7 months ago
ak as h
7 months ago
i was trying to build my driver ...i got this error ... :root@ubuntu:/home/ayush/device# make -C /usr/src/linux-headers-3.2.0-29-genericpae SUBDIRS=$PWD modules make: Entering directory `/usr/src/linux-headers-3.2.0-29-generic-pae' Building modules, stage 2. MODPOST 0 modules make: Leaving directory `/usr/src/linux-headers-3.2.0-29-generic-pae'
anil_pugalia
> akash
7 months ago
I believe you current directory /home/ayush/device contains the C files which you are trying to build. And your current kernel configuration is set to build & use drivers, i.e. "grep CONFIG_MODULE /usr/src/linux-headers-3.2.0-29generic-pae/.config" should not be not set or empty.
Reply janak iram Share
> anil_pugalia
4 months ago
I am also facing same problem as mentioned.. it's showing CONFIG_MODULES=y # CONFIG_MODULE_FORCE_LOAD is not set CONFIG_MODULE_UNLOAD=y # CONFIG_MODULE_FORCE_UNLOAD is not set CONFIG_MODULE_SRCVERSION_ALL=y
1
Reply
Share
anil_pugalia
> janakiram
4 months ago
ak as h
Use a makefile as suggested in the article. Basically you need the obj-m ... line for a module to be built, i.e. obj-m := ofd.o. So, just giving make on command line may not work.
Reply Share
8 months ago
root@ubuntu:/usr/src# make -C /usr/src/linux-headers-3.2.0-29 SUBDIRS=$PWD modules. I run above command & found this error pls help as soon as possible .. pls make: Entering directory `/usr/src/linux-headers-3.2.0-29' ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/config/auto.conf are missing. Run 'make oldconfig && make prepare' on kernel src to fix it.
Reply ak as h Share
> akash
Reply
7 months ago
anil_pugalia
> akash
7 months ago
ak as h
So, as it is mentioned in the above error, do make oldconfig after going into the linux-headers-3.2.0.29 folder.
Reply Share
8 months ago
www.linuxforu.com/2010/12/writing-your-first-linux-driver/
10/11
7/17/13
Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom - LINUX For You
anil_pugalia
> akash
8 months ago
ex plorer
I understand you mean the kernel source location. Typically, it would be under /usr/src (linux or build or ...). Also, it may not be installed by default. So, you may have to install it first.
Reply Share
8 months ago
when ubuntu is installed on desktop what does /usr/src/ dir contains ? i.e kerner source OR kernel headers? in my pc linux-headers-2.6.38-10-generic and linux-headers-2.6.38-10 directories are there. what are they.? they dont have any c source . if they are headers why there are two directories i.e what is generic means?
Reply Share
anil_pugalia
> explorer
8 months ago
They are headers. Sources can also be installed in the same folder using aptget install linux-source. Two headers are just the two variants created by the particular distro - ubuntu here
Reply c alix t o Share
> anil_pugalia
Reply c alix t o
8 months ago
> calixto
Reply
8 months ago
anil_pugalia
> calixto
8 months ago
C o m m e n t fe e d
Su b s cri b e vi a e m a i l
Reviews
How-Tos
Coding
Interviews
Features
Overview
Blogs
Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems
All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.
www.linuxforu.com/2010/12/writing-your-first-linux-driver/
11/11