Writing Your First Linux Driver in The Classroom
Writing Your First Linux Driver in The Classroom
Share
Tweet
+1
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.
To dynamically load or unload a driver, use these commands, which reside in the /sbin
directory, and must be executed with root privileges:
lsmod
insmod <module_file>
modprobe <module>
dependencies
rmmod <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 fat.ko, vfat.ko, etc., in
the fat ( vfat for older kernels) directory under /lib/modules/`uname -r`/kernel/fs. If
they are in compressed .gz format, you need to uncompress them with gunzip, before
you can insmodthem.
The vfat module depends on the fat module, so fat.ko needs to be loaded first. To
automatically perform decompression and dependency loading, use modprobe instead.
Note that you shouldnt specify the .ko extension to the modules name, when using the
modprobe command. rmmod is
destructor. The modules constructor is called when the module is successfully loaded into
the kernel, and the destructor when rmmod succeeds 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 module_init() and module_exit(), which are
defined in the kernel header module.h.
/* ofd.c Our First Driver code */
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
module_init(ofd_init);
module_exit(ofd_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("Our First Driver");
Given above is the complete code for our first driver; lets call it ofd.c. Note that there is
no stdio.h (a user-space header); instead, we use the analogous kernel.h (a kernel
space header). printk() is the equivalent of printf(). Additionally, version.h is
included for the module version to be compatible with the kernel into which it is going to be
loaded. The MODULE_* macros populate module-related information, which acts like the
modules signature.
clean:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif
With the C code ( ofd.c) and Makefile ready, all we need to do is invoke make to build our
first driver ( ofd.ko).
$ make
make -C /usr/src/linux SUBDIRS=... modules
.../ofd.mod.o
LD [M] .../ofd.ko
make[1]: Leaving directory `/usr/src/linux'
Summing up
Once we have the ofd.ko file, perform the usual steps as the root user, or with sudo.
# su
# insmod ofd.ko
# lsmod | head -10
lsmod should
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 lsmod listing showing the driver has loaded. Wheres the printk output
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
Jumpstart Linux Kernel Module Programming
A Simple guide to building your own Linux Kernel
Whats Up in Linux Graphicstopia Lately
The Semester Project-IV File Systems: Formatting a Pen
Previous Post
Kernel Tracing With Ftrace, Part 2
Next Post
Secure Upload Methods In PHP
montuviky
Away-some post will love to read all (and more :) )
anil_pugalia
Thanks for the appreciation. Go ahead & read all 24 are out in hard form.
vivek
when I run make command, i got the following error
make: Nothing to be done for `default.
I guess it could be the problem because of KERNEL_SOURCE. In my make file i have set
KERNEL_SOURCE as follows:
KERNEL_SOURCE := /usr/src/linux-headers-2.6.38-10
the_summer
I had the same problem.
For me the problem was copy and pasting the Makefile.
Make sure that in front of the ${MAKE} commands is an tab and not multiple spaces.
After replacing spaces with tabs it worked for me.
avik
thanks for the solution
the_summer
Just noticed:
I dont know if you use *buntu. But KERNEL_SOURCE := /usr/src/linux-headers-2.6.38-10
produces some errors.
KERNEL_SOURCE := /usr/src/linux-headers-2.6.38-10-generic seems to work.
Raghu
# Use this file as your Makefile
obj-m +=ofd.o
ofd-objs := ofd.o
all:
anil_pugalia
Thanks for the correction. I also corrected it.
akash
how to check kernel location
http://twitter.com/anil_pugalia Anil Pugalia
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.
akash
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.
akash
pls reply m waiting
http://twitter.com/anil_pugalia Anil Pugalia
So, as it is mentioned in the above error, do make oldconfig after going into the linuxheaders-3.2.0.29 folder.
akash
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-generic-pae
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
Pls reply any solution thnx in advance :)
http://twitter.com/anil_pugalia Anil Pugalia
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-29-generic-pae/.config should not be not set
or empty.
janakiram
I am also facing same problem as mentioned.. its 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
http://twitter.com/anil_pugalia Anil Pugalia
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.
satheesaran
Great example to start with. Thanks for the article :) Motivates to read more
http://twitter.com/anil_pugalia Anil Pugalia
Those words are really inspiring for me.
yogesh
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..
anil_pugalia
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
thats 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.
manoj
device drivers and its sources of Linux
tushar
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.
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.
Building modules, stage 2.
/usr/src/linux-headers-3.5.0-17/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.5.0-17
make: *** [default] Error 2
tushar
please reply i am waiting
http://twitter.com/anil_pugalia Anil Pugalia
Do as the message says: make oldconfig
And then, make modules_prepare
Both in the linux-headers folder.
And then retry.
tushar
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
scripts/basic/fixdep.c:433:1: fatal error: opening dependency file scripts/basic/.fixdep.d:
Permission denied
compilation terminated.
make[2]: *** [scripts/basic/fixdep] Error 1
make[1]: *** [scripts_basic] Error 2
make: *** No rule to make target `modules_prepare. Stop.
tushar@Sai:/usr/src/linux-headers-3.5.0-17$
i am using ubuntu 12.10
kernel version 3.5.0-17-generic
PLZ PROVIDE SOLUTION SIR
tushar
plz reply
http://twitter.com/anil_pugalia Anil Pugalia
Use sudo, yaar.
sudo make oldconfig
tushar
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-headers-3.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$
pls tell solution sir
http://twitter.com/anil_pugalia Anil Pugalia
How about sudo make prepare?
If not, then some issue with the headers package installation.
Sunil 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
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
scripts/kconfig/conf oldconfig Kconfig
#
# using defaults found in /boot/config-3.5.0-17-generic
#
#
# configuration written to .config
#
root@sunilshahu:/usr/src/linux-headers-3.5.0-17#
root@sunilshahu:/usr/src/linux-headers-3.5.0-17#
root@sunilshahu:/usr/src/linux-headers-3.5.0-17# make prepare
scripts/kconfig/conf silentoldconfig Kconfig
make[1]: *** No rule to make target `/usr/src/linux-headers-3.5.017/arch/x86/syscalls/syscall_32.tbl, needed by
`arch/x86/syscalls/../include/generated/asm/unistd_32.h. Stop.
tushraj
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
make: *** [default] Error 2
http://twitter.com/anil_pugalia Anil Pugalia
Check out the comments below for the solution to the same problem.
enigma
obj-m := ofd.o should be obj-m += ofd.o ?
http://twitter.com/anil_pugalia Anil Pugalia
In this particular case, even := is fine as, we are limiting the build to the drivers directory and
more to our driver, alone.
janakiram
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.
anil_pugalia
Seems like, there is some problem with the installation of the kernel headers with this
version of the distro you are using.
xpress_embedo
I too got this error, the problem was that I had created a folder Linux Device Driver and under
this HelloWorld, When i remove Space and Change Folder name as LinuxDeviceDriver,
everything works fine, Hoping you too had done same thing.
Hope this help.
Very Nice Tutorial:-)
anil_pugalia
Oh! Thats great input. Hey guys, please fix your problems, if you are having the same
problem.
Ajey
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.
anil_pugalia
You are welcome.
nandan
Thanks sir ..you have done a great job.
anil_pugalia
Thanks for reading & appreciating.
nbtsos
hi sir, i can compile on ubuntu version 8, but when i use ubuntu 12.04 lts, i tried every way to
compile but still cannot compile:
user@ubuntu:~/Study$ make
make: Nothing to be done for `default.
Then i add default: ofd.o to our Makefile but it raise another error:
cc -c -o ofd.o ofd.c
ofd.c:2:26: fatal error: linux/module.h: No such file or directory compilation terminated.
Later, i must use command (type manually to terminal) like this to compile :
make -C /lib/modules/3.5.0-37-generic/build SUBDIRS=/home/user/Study modules
anil_pugalia
Thanks for your appreciation, as well.
Ajay
Hi sir,
I am getting there error plz help me out
ajay@ubuntu:~/module$ make
make -C /usr/src/linux-headers-3.5.0-17-generic SUBDIRS-/home/ajay/module module
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-17-generic
HOSTCC scripts/basic/fixdep
scripts/basic/fixdep.c:433:1: fatal error: opening dependency file scripts/basic/.fixdep.d:
Permission denied
compilation terminated.
make[3]: *** [scripts/basic/fixdep] Error 1
make[2]: *** [scripts_basic] Error 2
make[1]: *** No rule to make target `SUBDIRS-/home/ajay/module. Stop.
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-17-generic
make: *** [all] Error 2
anil_pugalia
I hope pjms comment as mentioned above, has solved your problem.
Ajay
Hi sir,
I am getting there error plz help me out
ajay@ubuntu:~/module$ make
make -C /usr/src/linux-headers-3.5.0-17-generic SUBDIRS-/home/ajay/module module
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-17-generic
HOSTCC scripts/basic/fixdep
scripts/basic/fixdep.c:433:1: fatal error: opening dependency file scripts/basic/.fixdep.d:
Permission denied
compilation terminated.
make[3]: *** [scripts/basic/fixdep] Error 1
make[2]: *** [scripts_basic] Error 2
make[1]: *** No rule to make target `SUBDIRS-/home/ajay/module. Stop.
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-17-generic
make: *** [all] Error 2
pjmpjm
peter@Ubuntu2:~$
peter@Ubuntu2:~$ rm ofd.mod.o
peter@Ubuntu2:~$ rm ofd.ko
peter@Ubuntu2:~$
peter@Ubuntu2:~$ make -f makefile
make: Nothing to be done for `default.
peter@Ubuntu2:~$ make -f Makefile
make -C /usr/src/linux-headers-3.2.0-56-generic SUBDIRS=/home/peter modules
make[1]: Entering directory `/usr/src/linux-headers-3.2.0-56-generic
Building modules, stage 2.
MODPOST 1 modules
CC /home/peter/ofd.mod.o
LD [M] /home/peter/ofd.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-56-generic
peter@Ubuntu2:~$
makefile( indented lines have 4 leading spaces using cut&paste from this article)
ifneq (${KERNELRELEASE},)
obj-m := ofd.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
else
KERNEL_SOURCE := /usr/src/linux-headers-3.2.0-56-generic
PWD := $(shell pwd)
default:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
clean:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif
Makefile ( indented lines have 1 leading tab after editing)
ifneq (${KERNELRELEASE},)
obj-m := ofd.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
else
KERNEL_SOURCE := /usr/src/linux-headers-3.2.0-56-generic
PWD := $(shell pwd)
default:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
clean:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif
Sooooooo if you cut and paste from this tutorial to create your makefile edit it so that the
leading white space is one Tab this is with Ububtu 12.04
hope this helps
/opt/linux-cortexm-1.4.1/projects/developer/app/driver/vcom.ko
make[1]: Leaving directory `/