Writing a Linux Driver
The concept of an operating system (OS) must be well understood before any attempt to navigate inside it is made. Several definitions are available for an OS:
An OS is the set of manual and automatic procedures which allow a set of users to share a computing system in an efficient manner.
The dictionary defines an OS as a program or set of programs which manage the processes of a computing system and allow the normal execution of the other jobs.
The definition from the Tanenbaum book (see Resources): An operating system is [the program] which controls all the resources of the computer and offers the support where users can develop application programs.
It is also very important to clearly distinguish a program from a process. A program is a block of data plus instructions, which is stored in a file on disk and is ready to be executed. On the other hand, a process is an image in memory of the program which is being executed. This difference is highly important, because usually the processes are running under OS control. Here, our program is the OS, so we cannot speak about processes.
We will use the term kernel to refer to the main body of the OS, which is a program written in the C language. The program file may be named vmlinuz, vmlinux or zImage, and has some things in common with the MS-DOS files COMMAND.COM, MSDOS.SYS and IO.SYS, although their functionality is different. When we discuss compilation of the kernel, we mean that we will edit the source files in order to generate a new kernel.
Peripheral or internal devices allow users to communicate with the computer. Examples of devices are: keyboards, monitors, floppy and hard disks, CD-ROMs, printers, mice (serial/parallel), networks, modems, etc. A driver is the part of the OS that manages communication with devices; thus, they are usually called device drivers.

Figure 1. Software/Hardware Scheme
Figure 1 shows the relation between user programs, the OS and the devices. Differences between software and hardware are clearly specified in this scheme. At the left side, user programs may interact with the devices (for example, a hard disk) through a set of high-level library functions. For example, we can open and write to a file of the hard disk calling the C library functions fopen, fprintf and close:
FILE *fid=fopen("filename", "w");
fprintf(fid, "Hello, world!");
fclose(fid);
The user can also write to a file (or to another device such as a printer) from the OS shell, using commands such as:
echo "Hello, world!" > echo "Hello, world!" > /dev/lpTo execute this command, both the shell and the library functions perform a call to a low level function of the OS, e.g., open(), write() or close():
fid = open("/dev/lp", O_WRONLY);
write(fid, "Hello, world!");
close(fid);
Each device can be referred to as a special file named /dev/*.
Internally, the OS is composed of a set of drivers, which are
pieces of software that perform the low-level communication with
each device. At this execute level, the kernel calls driver
functions such as lp_open() or
lp_write().
On the right side of Figure 1, the hardware is composed of the device (a video display or an Ethernet link) plus an interface (a VGA card or a network card). Finally, the device driver is the physical interface between the software and the hardware. The driver reads from and writes to the hardware through ports (memory addresses where the hardware links physically), using the internal functions out_p and in_p:
out_p(0x3a, 0x1f); data = in_p(0x3b);
Note that these functions are not available to the user. Since the Linux kernel runs in protected mode, the low memory addresses, where the ports addresses reside, are not user accessible. Functions equivalent to the low-level functions in and out do not exist in the high-level library, as in other operating systems such as MS-DOS.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- Readers' Choice Awards
- RSS Feeds
- Automatically updating Guest Additions
21 min 59 sec ago - I like your topic on android
1 hour 8 min ago - Reply to comment | Linux Journal
1 hour 29 min ago - This is the easiest tutorial
7 hours 44 min ago - Ahh, the Koolaid.
13 hours 22 min ago - git-annex assistant
19 hours 22 min ago - direct cable connection
19 hours 44 min ago - Agreed on AirDroid. With my
19 hours 55 min ago - I just learned this
19 hours 59 min ago - enterprise
20 hours 29 min ago




Comments
I like this one
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
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 ?
where is the sidebar & port table that were discussed in this article..?
links and sidebars
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