next up previous contents
Next: Polling and Interrupts Up: tlk-html.html Previous: Initializing the Interrupt Handling

Device Drivers

  One of the purposes of an operating system is to hide the peculiarities of the systme's hardware devices from its users. For example the Virtual File System presents a uniform view of the mounted filesystems irrespective of the underlying physical devices. This chapter describes how the Linux kernel manages the physical devices in the system.

The CPU is not the only intelligent device in the system, every physical device has a hardware controller. The keyboard, mouse and serial ports are controlled by a SuperIO chip, the IDE disks by an IDE controller, SCSI disks by a SCSI controller and so on. Each hardware controller has its own control and status registers (CSRs) and these differ between devices. The CSRs for an Adaptec 2940 SCSI controller are completely different from those of an NCR 810 SCSI controller. These CSRs are used to start and stop the device, to initialize it and to diagnose any problems with it. Instead of putting code to manage the hardware controllers in the system into every application, the code is kept in the Linux kernel. The software that handles or manages a hardware controller is known as a device driver. The Linux kernel device drivers are, essentially, a shared library of privileged, memory resident, low level hardware handling routines. It is Linux's device drivers which each handle the peculiarities of the devices that they are managing.

Every device in the system is represented by a device special file, for example the first IDE disk in the system is represented by /dev/hda. These device special files are created by the mknod  command and they describe the device using major and minor device numbers. All devices controlled by the same device driver have a common major device number. The minor device numbers are used to distinquish between different devices, for example each partition on the primary IDE disk has a different minor device number. So, /dev/hda2, the second partition of the primary IDE disk has a major number of 3 and a minor number of 2. One of the basic features of Unix is that it abstracts the handling of devices. All hardware devices look like regular files; they can be opened, closed, read and written using the same, standard, system calls that are used to manipulate files. Linux's Virtual File System maps the device special file passed in the system calls to the device's device driver using the major device number.

Linux supports three types of hardware devices: character, block and network. Character devices are read and written directly without buffering, for example the system's serial ports /dev/cua0 and /dev/cua1. Block devices can only be written to and read from in multiples of the block size, typically 512 or 1024 bytes. Block devices are accessed via the buffer cache and may be randomly accessed, that is any block can be read or written no matter where it is on the device. Block devices can be accessed via their device special file but more commonly they are accessed via the filesystem. Only a block device can support a mounted file system. There are many different device drivers in the Linux kernel (that is one of Linux's strengths) but they all share some common attributes:

kernel code
Device drivers are part of the kernel and, like other code within the kernel, if they go wrong they can seriously damage the system. A badly written driver may even crash the system, possibly corrupting file systems and losing data,

Interfaces
A device driver must provide a standard interface to the kernel. The kernel uses this interface to interact with the devices that this device driver controls. That interface is different depending on the class of device driver. So, for example, the interface provided by a SCSI device is different from that provided by an ethernet device. The kernel may also provide access to the system devices to processes in the system via standard interfaces. For example, the socket interface can be used to connect to the system's ethernet devices.

Kernel services
Device drivers make use of standard kernel services such as memory allocation, interrupt delivery and wait queues to operate,

Loadable
Most of the Linux device drivers can be loaded on demand as kernel modules when they are needed and unloaded when they are no longer being used. This makes the kernel very adaptable and efficient with the system's resources,

Configurable
Linux device drivers can be built into the kernel. Which devices are built is configurable when the kernel is compiled,

Dynamic
As the system boots and each device driver is initialized it looks for the hardware devices that it is controlling. It does not matter if the device being controlled by a particular device driver does not exist. In this case the device driver is simply redundant and causes no harm apart from occupying a little of the system's memory.




next up previous contents
Next: Polling and Interrupts Up: tlk-html.html Previous: Initializing the Interrupt Handling

David A. Rusling
david.rusling@reo.mts.dec.com