15
An Example Device Driver
It’s hard to find a good driver these days, one with
character and style.
— Unknown
15.1 Introduction
Chapters in this section of the text explore the general structure of an I/O system,
including interrupt processing and real-time clock management. The previous chapter
presents the organization of the I/O subsystem, a set of abstract I/O operations, and an
efficient implementation using a device switch table.
This chapter continues the exploration of I/O. The chapter explains how a driver
can define an I/O service at a high level of abstraction that is independent of the under-
lying hardware. The chapter also elaborates on the conceptual division of a device
driver into upper and lower halves by explaining how the two halves share data struc-
tures, such as buffers, and how they communicate. Finally, the chapter shows the de-
tails of a particular example: a driver for an asynchronous character-oriented serial de-
vice.
15.2 The Tty Abstraction
Xinu uses the name tty to refer to the abstraction of an interface used with
character-oriented serial devices such as a serial interface or a keyboard and text win-
dow.† In broad terms, a tty device supports two-way communication: a process can
send characters to the output side and/or receive characters from the input side.
Although the underlying serial hardware mechanism operates the input and output in-
†The name tty is taken from early Unix systems that used an ASCII Teletype device that consisted of a
keyboard and an associated printer mechanism.
267
268 An Example Device Driver Chap. 15
dependently, the tty abstraction allows the two to be connected. For example, our tty
driver supports character echo, which means that the input side of the driver can be
configured to transmit a copy of each incoming character to the output. Echo is espe-
cially important when a user is typing on a keyboard and expects to see characters
displayed on a screen as keys are pressed.
The tty abstraction illustrates an important feature of many device drivers: multiple
modes that can be selected at run-time. In our tty driver, the three modes focus on how
the driver processes incoming characters before delivering them to an application. Fig-
ure 15.1 summarizes the three modes and gives their characteristics.
Mode Meaning
The driver delivers each incoming character as it arrives
raw without echoing the character, buffering a line of text,
performing translation, or controlling the output flow
The driver buffers input, echoes characters in a readable
cooked form, honors backspace and line kill, allows type-ahead,
handles flow control, and delivers an entire line of text
The driver handles character translation, echoing, and
cbreak flow control, but instead of buffering an entire line of text,
the driver delivers each incoming characters as it arrives
Figure 15.1 Three modes supported by the tty abstraction.
Cooked mode is intended to handle interactive keyboard input. Each time it re-
ceives a character, the driver echoes the character (i.e., transmits a copy of the character
to the output), which allows a user to see characters as they are typed. Echo is not
mandatory. Instead, the driver has a parameter to control character echoing, which
means an application can turn off echo to prompt for a password. Cooked mode sup-
ports line buffering, which means that the driver collects all characters of a line before
delivering them to a reading process. Because the tty driver performs character echo
and other functions at interrupt time, a user can type ahead, even if no application is
reading characters (e.g., a user can type the next command while the current command
is running). The chief advantage of line buffering arises from the ability to edit the
line, either by backspacing or typing a special character that erases the entire line and
allows the user to begin entering the line again.
Cooked mode provides two additional functions. First, it handles output flow con-
trol, allowing a user to temporarily stop and later restart output. When flow control is
enabled, typing control-s stops output and typing control-q restarts output. Second,
cooked mode handles input mapping. In particular, some computers or applications use
a two-character sequence of carriage return (cr) and linefeed (lf) to terminate a line of

Get Operating System Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.