Writing a Linux Driver

The main goal of this article is to learn what a driver is, how to implement a driver for Linux and how to integrate it into the operating system—an article for the experienced C programmer.
Driver Integration in the Kernel

The task of integrating the driver into the kernel includes several steps:

  • Insert kernel calls to the new driver.

  • Add the driver to the list of drivers.

  • Modify compilation scripts.

  • Re-compile the driver.

The insertion of the OS call to mrv4_init() is done in the /usr/src/linux/kernel/mem.c file. The other driver function calls (open, read, write, ioctl, release, etc.) are user transparent. They are carried out through the file_operations structure. A driver major number must be added to the list located at /usr/include/linux/major.h. Search for a free driver number; for example, if number 62 is free, you must add one or both of the following lines to the file, depending on the Linux release:

/dev/mrv4 62
#define MRV4_MAJOR 62

Each device is referenced by one major and one minor number. The major number represents the number of the driver. The minor number distinguishes between several devices which are controlled by the same device (e.g., several hard disks controlled by the same IDE driver: hd0, hd1, hd2).

The next step is to create a logical device to access the driver. You must use the command mknod in this way:

mknod -m og+rw /dev/mrv4 c 62 0

where 62 is the major, 0 the minor (only one physical device) and c indicates a character device. Set the permissions as necessary, although you can modify them later with the command chmod. For example, enable rw if you want to allow all users to access the device:

crw-rw-rw-2 bin bin 62, 0 Mar 12 1997 /dev/mrv4

Driver Compilation and Testing

To allow driver compilation within the kernel, the following lines must be added to the script file /usr/src/linux/arch/i386/config.in:

comment 'MRV 4'
bool 'MRV 4 card support' CONFIG_MRV4

and the following lines to /usr/src/linux/drivers/char/Makefile:

ifdef CONFIG_MRV4
  L_OBJS += mrv4.o
  endif
It is recommended that the driver be compiled alone, before linking the kernel. This method will save time testing syntax errors:
cd /usr/src/linux/drivers/char
gcc -c mrv4.c -Wall -D__KERNEL__
And when all is well, delete the object file:
rm -f mrv4.o
Next, configure the kernel by typing:
cd /usr/src/linux
make config
Answer yes when the script asks you about installing the MRV-4 driver (this sets the constant CONFIG_MRV4). Finally, insert an empty floppy disk and re-build the kernel by typing the following commands:
make zdisk    # generate a bootable
# floppy disk
dev -R /dev/fd0 1   # disable writes to<\n>
# floppy
Once you are sure that the kernel works, you can overwrite the file vmlinuz with the new kernel. To test the new kernel, restart the system (type reboot) and ... good luck! There are no debuggers available.

If the kernel seems to work, you might test the driver by writing one or more user programs, i.e., mrv4test.c which call the driver functions:

fid = open("/dev/mrv4", ...);
read(fid, ...);
write(fid, ...);
ioctl(fid, ...);
close(fid);
How to Obtain Additional Information

You can obtain privileged documentation at sunsite.unc.edu (see Resources). But of course, you will never be able to write your own driver using only the general guidelines of this article. To facilitate this task, we supply the source files for a dummy driver for Linux 2.0.24, which is a model for character driver development. It simulates the equation y = a x and includes an example of interrupt management (which does not work since it is not associated with any hardware). Its name is foo, since Linux already has a driver called dummy. These files are:

  • README: summary of instructions to install it

  • foo.c: driver source file

  • foo.h: driver header file

  • footest.c: program for driver testing

You can obtain these files via anonymous FTP at ftp://ftp.linuxjournal.com/pub/lj/listings/issue48/2476.tgz.

Guidelines

Resources

Fernando Matía is an Associate Professor at the Universidad Politecnica de Madrid (UPM). He was born in Madrid, Spain, in 1966. He became an Industrial Engineer at UPM in 1990 and received his Ph.D. degree at UPM in 1994 in the area of Control Engineering. He works at the Systems and Automatic Control Engineering Division (DISAM). His main activities are Intelligent Control, Fuzzy Control, Robotics and Computer Sciences. He can be reached at matia@disam.upm.es.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I like this one

Anonymous's picture

Good introduction, I think, for those who is not familiar with Linux drivers at all.
Thanks a lot to the author. I enjoyed the article.

This article twice uses the

Anonymous's picture

This article twice uses the term "protected mode" where it should be using the term "supervisor mode".

Protected mode is a mode of the Intel x86 processor which provides various protection features, such as memory protection and the ability to disable privileged instructions. Under Linux, all software runs in protected mode, but user applications run at a different privilege level to the kernel.

Of course this article is quite out of date (though surprisingly much of it is still relevant) but on this point it was wrong even back when it was written.

sidebar & port table ?

ARJUN's picture

where is the sidebar & port table that were discussed in this article..?

links and sidebars

Keith Daniels's picture

This is not the magazine so there are no "sidebars" Check the links in the articles they have what you are looking for.

"I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone."
-- Bjarne Stroustrup

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix