Fitting into the OS: The Device Driver
This lesson discusses device drivers, abstraction to fit different devices with specific interfaces into the OS.
We'll cover the following
One final problem we will discuss: how to fit devices, each of which has very specific interfaces, into the OS, which we would like to keep as general as possible. For example, consider a file system. We’d like to build a file system that worked on top of SCSI disks, IDE disks, USB keychain drives, and so forth, and we’d like the file system to be relatively oblivious to all of the details of how to issue a read or write request to these different types of drives. Thus, our problem:
THE CRUX: HOW TO BUILD A DEVICE-NEUTRAL OS
How can we keep most of the OS device-neutral, thus hiding the details of device interactions from major OS subsystems?
Device driver
The problem is solved through the age-old technique of abstraction. At the lowest level, a piece of software in the OS must know in detail how a device works. We call this piece of software a device driver, and any specifics of device interaction are encapsulated within.
Let us see how this abstraction might help OS design and implementation by examining the Linux file system software stack. The figure presented below is a rough and approximate depiction of the Linux software organization.
As you can see from the diagram, a file system (and certainly, an application above) is completely oblivious to the specifics of which disk class it is using. It simply issues block read and write requests to the generic block layer, which routes them to the appropriate device driver, which handles the details of issuing the specific request. Although simplified, the diagram shows how such detail can be hidden from most of the OS.
The diagram also shows a raw interface to devices, which enables special applications (such as a file-system checker, described later, or a disk defragmentation tool) to directly read and write blocks without using the file abstraction. Most systems provide this type of interface to support these low-level storage management applications.
Downside of the encapsulation
Note that the encapsulation seen above can have its downside as well. For example, if there is a device that has many special capabilities, but has to present a generic interface to the rest of the kernel, those special capabilities will go unused. This situation arises, for example, in Linux with SCSI devices, which have very rich error reporting; because other block devices (e.g., ATA/IDE) have much simpler error handling. All that higher levels of software ever receive is a generic EIO
(generic IO error) error code; any extra detail that SCSI may have provided is thus
Interestingly, because device drivers are needed for any device you might plug into your system, over time they have come to represent a huge percentage of kernel code.
Get hands-on with 1400+ tech skills courses.