Compiling Linux Device Drivers

The bible for who wants to write Linux Device Drivers is the mythical book Linux Device Driver 3rd Edition written by Jonathan Corbet, Alessandro Rubini and Greg Kroah-Hartman for O'Reilly (ISBN 0-596-00590-3).

To have the printed version of this book is a must but if you prefer you can obtain it in PDF format directly from this link.

This article illustrates how to compile the “Hello World” example on page 16 chapter II of this book and run it on the FOX Board G20.

Before doing that you must have a properly configured and built kernel tree as explained in the Lee's tutorial available here.

The Hello World Module

This is the source code of the basic Hello World Module on page 16 chapter II of the LDD3 book:

#include <linux/init.h>
#include <linux/module.h>
 
MODULE_LICENSE("Dual BSD/GPL");
 
static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
 
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

Create a directory to store your module in your home page on your Linux Ubuntu PC.

$ cd ~
$ mkdir ldd3
$ cd ldd3

and save here the example code hello.c.

Create the Makefile

Create a file called Makefile in the same source directory with a single line:

obj-m:= hello.o

The fully explanation on how it works is illustrated on page 23 chapter II or in Documentation/kbuild in the kernel directory.

Now issue the kernel module compilation typing:

$ make -C ~/linux-2.6.32.2 M=`pwd` modules
make: Entering directory `/home/tanzilli/linux-2.6.32.2'
make -w \
		-fMakefile \
		ARCH=arm \
		CROSS_COMPILE=arm-linux-gnueabi- \
		INSTALL_MOD_PATH=./FoxModules \
		modules
make[1]: Entering directory `/home/tanzilli/linux-2.6.32.2'
  CC [M]  /home/tanzilli/ldd3/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/tanzilli/ldd3/hello.mod.o
  LD [M]  /home/tanzilli/ldd3/hello.ko
make[1]: Leaving directory `/home/tanzilli/linux-2.6.32.2'
make: Leaving directory `/home/tanzilli/linux-2.6.32.2'
$

The hello.ko file you will obtain is the kernel module ready to be launched on the FOX Board G20.

Copy this file to the FOX Board G20 filesystem typing for example:

$ scp hello.ko root@192.168.1.28:/root

(change 192.168.1.28 with the IP address of your FOX)

Then open an SSH session to your board and load your new module:

debarm:~# insmod hello.ko 
debarm:~# lsmod                                                                 
Module                  Size  Used by                                           
hello                    628  0                                                 
ipv6                  217755  12                                                
i2c_gpio                1819  0                                                 
i2c_algo_bit            4549  1 i2c_gpio                                        
i2c_core               15127  2 i2c_gpio,i2c_algo_bit

To read the module init message type:

debarm:~# dmesg
...
Hello, world                                                                    
debarm:~#

To remove the module type:

debarm:~# rmmod hello
debarm:~# lsmod                                                                 
Module                  Size  Used by                                           
ipv6                  217755  12                                                
i2c_gpio                1819  0                                                 
i2c_algo_bit            4549  1 i2c_gpio                                        
i2c_core               15127  2 i2c_gpio,i2c_algo_bit      
debarm:~#
debarm:~# dmesg
...
Hello, world                                                                    
Goodbye, cruel world                                                            
debarm:~#

Links

 
tutorial/compiling_linux_device.txt · Last modified: 2010/03/05 23:00 by tanzox
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki