Linux Device Drivers

Linux Device Drivers

Character drivers
IO & Memory
Linux Kernel
Process Management
Process Address space

Linux Scheduler
Memory Management
System Calls
Kernel Synchronization
Linux Inter Process Communications

Serial Ports
Parallel Ports
Introduction to Hardware
Linux Timers
DMA in Linux
Linux Threads
Linux Thread Synchronization

Linux Multi Threading
Debugging in Linux
GDB GNU Debugger
KDB Kernel Debugger
KGDB Kernel GNU Debugger
Example Ethernet Driver

Linux History


     1991: Linux kernel written from scratch in 6 months by LinusTorvaldsin his Helsinki University room, to overcome limitations of his 80386 PC.

     1991: Linus shares his kernel on the net. Programmers fromthe whole world join in and contribute to coding and testing

     1992: Linux released under the GNU General Public License

     1994: Linux 1.0 released

     1994: Red Hat founded by Bob Young and Marc Ewing, creating anew business model.

     1995: GNU/Linux and free software developing in Internet servers.

     2001: IBM invests $1 billion in Linux

     2002: GNU/Linux wide adoption starts in many industry sectors.

     2005: Linux Kernel 2.6


Linux Kernel 2.6 Features


     Performance and Determinism

     A Pre-emptible Kernel

     An Efficient Scheduler

     New Synchronization Primitives

     Sharing Memory with Less Contention

     POSIX Threads, Signals, and Timers

     Support for Custom Designs

     Devices, Busses, and I/O

     No Keyboard, No Monitor, No Wires

     Linux on 64-bit Machines

     Linux on Microcontrollers



Linux Distribution


     Consists of boot diskette and other diskettes or CD-ROM

     Popular distribution: RedHat, Debian, Slackware, Caldera and SuSe.


Linux Versioning Scheme


Releases are versioned as x.y.z

Stable versions

x.y: main release number

y: even number

z: identifies the exact release version number

Examples: 2.0.40, 2.2.26, 2.4.27, 2.6.7 ...


Development versions

y: odd number

Examples: 2.3.42, 2.5.74 ...



LINUX Architecture


     Microkernel: Minimum functionality IPC, MM and scheduler

Remaining functions as autonomous processes

Communicating to kernel via a well defined interface


     Advantage Individual components work independently

Less trouble to maintain


     Drawbacks Slow, prevents optimizations


     Trend: Microkernel architecture. E.g., Mach Kernel or kernel of Windows NTMINIX and Hurd System, examples of microkernel


     Linux Classical monolithic architecture, most components accessed via accurately defined interfaces.


Architecture of generic linux




Supported hardware architectures


     See the arch/ directory

     Minimum: 32 bit processors, with or without MMU

     32 bit architectures:alpha, arm, cris, h8300, i386, m68k, m68knommu, mips,parisc, ppc, s390, sh, sparc, um, v850

     64 bit architectures:ia64, mips64, ppc64 sh64, sparc64, x86_64

     See arch/README or Documentation/arch/README for details



Linux Key Features


     Portability and hardware support


Can run on super computers as well as on tiny devices

     Compliance to standards and interoperability



     Stability and reliability




The Kernel Source


arch - This subdirectory contains all of the architecture specific code for each supported architecture (MIPS, ARM, 386 and so on), there is subdirectory under "arch". Each supported architecture subdirectory has four major subdirectories:


kernel: which contains the architecture specific kernel code

mm: which contains the architecture specific memory mgmt code.

lib: which contains architecture specific library code (vsprintf...)target platform directory -which contains platform specific code


Note: Linux ports to processors without memory management units(MMU) are also available.


documentation: This subdirectory contains the documentation for the kernel.


Drivers: This subdirectory contains the code for the device drivers.

Each type of device has subdirectories, such as char, block, net...


fs: This directory contains the file system code. This has further subdirectories for each supported file system (ext2, proc, ...)


include: The include subdirectory contains the include files for the kernel. It has further subdirectories for common include files for all architectures), one for every architecture supported.


init-This directory contains the initialization code for the kernel.


kernel-This directory contains the main kernel code.


lib-This directory contains the library code of the kernel.


mm-This directory contains the memory management code.The directory structure of the LINUX sources/usr/src/linux




Kernel Development on x86 platform


The Linux kernel development requires following steps:


     Selecting kernel

     Configuring kernel

     Compiling kernel

     Installing the kernel



Access to the kernel sources


Download sources from


Or get a patch vsthe x.y.<z1> version:


Check the integrity of sources:

gpgverify linux2.6.7.tar.bz2.sign linux2.6.7.tar.bz2


GnuPG details:


Kernel source signature details:



Using the Patch command


     patch command: uses the output of the diff command to apply a set of changes to a source tree.


     patch basic usage:patch -pn< diff_file

n: number of directory levels to skip


     Linux patches:

Always to apply to the x.y.<z1> version

Always produced for n=1

patch -p1 < linux_patch


Kernel Configuration


The Linux kernel configuration is usually found in the kernel source in the file: /usr/src/linux/.config. The kernel can be configured using text mode or graphical configurator.


     make config-starts a character based questions and answer session

     make menuconfig-starts a terminal-oriented configuration tool (using ncurses)

     make xconfig-starts a X based configuration tool


Kernel Configuration


Makefile edition

Setting the version and target architecture if needed

Kernel configuration: defining what features to include in the kernel:

make xconfig or

make menuconfig or

make oldconfig


Kernel configuration file (Makefile syntax) stored in the .config file at the root of the kernel sources

Distribution kernel config files usually released in /boot/




To identify your kernel image with others build from the same sources, use the EXTRAVERSION variable:







unamer will return: 2.6.7-acme1


Kernel Configuration


When the kernel configuration runs, it reads the main kernel configuration file, located in arch/i386/Kconfig.

This configuration file also includes other configuration files as needed.


For example

arch/i386/Kconfigfile includes

source "sound/core/Kconfig"

Which includes

source "sound/core/Kconfig"

source "sound/drivers/Kconfig"


Compiling and installing Kernel



make install

make modules_install


Building Kernel


The "make" command (with no arguments) now automatically (and silently) generates dependency information, compiles the kernel, and compiles any drivers that you have selected for installation as modules.

Once you have configured your kernel, simply issuing the make command, followed by the make install and make modules_install commands, is the most common build and install procedure for 2.6-based kernels.


Makes changes in the grub.conf file

Copies modules to /lib/modules/<kernel version>/


Linux Initialization for Intel Platforms Boot Process

1.     A boot loader finds the kernel image on the disk, loads it into memory, and starts it.

2.     The kernel initializes the devices and its drivers.

3.     The kernel mounts the root filesystem.

4.     The kernel starts a program called init.

5.     init sets the rest of the processes in motion.

6.     The last processes that init starts as part of the boot sequence allow you to log in.







Boot Loaders


Before the kernel runs init, a boot loader starts the kernel. Sometimes, you need to tell the boot loader to load different kernels or operating systems, and to start in different modes. The boot loader loads a kernel image into memory and hands control of the CPU to the new image, possibly supplying it with some parameters.




LILO can load the kernel from a variety of media, including floppy disks, hard disks and some flash devices (configured to operate as hard disks).


LILO can be configured to pass startup parameters to the Linux kernel during the bootstrap process. Depending on the configuration, you can customize the startup screens for embedded device, and control other aspects of the initialization and runtime configuration that LILO boots.


LILO can also be configured to load a special filesystem, referred to as an initial RAM disk, or initrd, that can augment the bootstrap sequence in certain ways




GRUB stands for Grand Unified Bootloader, a system that is slowly replacing LILO.


The most important feature is its ability to navigate filesystems, so you can read files without loading a kernel.


GRUB has a menu interface thats easy enough to navigate, but ifyou need to boot from a different kernel, change the root partition,or supply extra kernel parameters, you should get into the mini-shell.


Initialization Overview


When the operating system starts, the kernel runs /sbin/init. This can be either a script or a program, but typically it is a program that processes the statements in /etc/initab.

The default inittab file tells init to run the commands in the file/etc/rc.d/rcS. In addition, init spawns programs to present login prompts on two virtual terminals.


Init Runlevels:


The idea behind operating different services at different runlevelsessentially revolves around the fact that different systems can be used in a different ways.Thefollowing runlevelsare defined in Red Hat Linux :


     0 -Halt

     1 -Single-user mode

     2 -Multi-user mode, without networking

     3 -Full multi-user mode

     4 -Not used

     5 -Full multi-user mode (with an X-based login screen)

     6 -Reboot; The default runlevelfor a system to boot to and stop is configured in /etc/inittab.


Adding a new module (hello) to the Build process


1.     Make a directory -drivers/hello

Create or Copy file -Kconfig, Makefile, hello.c

hello.c-A simple kernel module

Kconfig-Add following statements


# hello module configuration


menu "Hello Module support"

config HELLO

tristate "Include Hello module?"


Linux Kernel Module demonstration Hello


Makefile-Add following statement


obj-$(CONFIG_HELLO) += hello/


Adding a new module (hello) to the Build process


2.     Add the following statement before endmenu statement:

source "drivers/hello/Kconfig"

And add the following statement at the end of file in Makefile:

obj-$(CONFIG_HELLO) += hello.o


3.     Config, Build and Deploy the kernel.

make xconfig


Look at the menu item displayed under drivers.

Select and save the configuration and exit.

Look for CONFIG_HELLO in .configfile