Você está na página 1de 22

What is a Device Driver ?

Chapter 1

1. 2.

3.

The Design UNIX device driver Types of UNIX device drivers

UNIX Device Drivers

What is a Device Driver?

The Design

A device driver is a bridge between an OS and its I/O devices. Device drivers act as translators, converting the generic requests received from OS into commands that specific controllers can understand.

UNIX Device Drivers

What is a Device Driver?

Software

Hardware

Screen Device Driver Keyboard Applications Software UNIX OS Kernel Device Driver Floppy Device Driver Hard disk Device Driver

Screen Interface Keyboard Interface Floppy Interface Hard disk Interface

Screen

Keyboard

Floppy

Hard disk

UNIX Device Drivers

What is a Device Driver?

The application software makes system calls to the OS requesting services ( for example, write data to file x ). The OS analyzes these requests and when necessary issues requests to the appropriate device drivers ( for example, write this data to disk 2, block 23654 ). The device driver in turn issues commands to the hardware interface to perform the operations needed to service the request. ( e.g transfer 1024 bytes starting at address 23500 to disk2, cylinder 173, head 7, sector 8 ).

UNIX Device Drivers

What is a Device Driver?

Although this process seems complex, but without device drivers, the OS would be responsible for talking directly to the hardware. This would mean that OS designer would have to include support for all devices that a user may connect. Also it means that adding support for a new device would demand modifying the OS itself.

UNIX Device Drivers

What is a Device Driver?

By separating device drivers functions from OS itself, details related to individual hardware can be ignored and generic requests for I/O operations can be issued by OS to the device driver. The device driver on other hand has to take only detailed device independent requests from the OS and to manipulate the hardware in order to fulfill the request. Thus the result is the clean separation of responsibilities and the ability to add device drivers for new devices without changing the OS.

Thus device drivers provide OS with a standard interface to non standard I/O devices.

UNIX Device Drivers

What is a Device Driver?

UNIX device driver

A UNIX device driver is a collection of functions, usually written in C, that can be called by UNIX OS using standard C function calling mechanism. These routines are often called entry points. The compiled code for the device driver is linked with the code for the OS itself and the result is a new file containing the bootable OS with all the device drivers.

UNIX Device Drivers

What is a Device Driver?

To understand drivers in UNIX better, consider the following example: echo Hello World > /dev/lp

The C code that is executed for opening the file /dev/lp is like:
fileds = open( /dev/lp, O_WRONLY );

UNIX Device Drivers

What is a Device Driver?

UNIX first examines the file /dev/lp and determines that it is, in fact, not a normal data file but a special file. crw------- 2 bin bin 6 0 Nov 16 2009 /dev/lp

The first character on the line is c which indicates a character device driver. The number 6(major) and 0(minor) are the device numbers for this special file. The major number specifies the device driver while the minor number is used by the driver to distinguish between different drivers under the control of a single driver.
What is a Device Driver?
9

UNIX Device Drivers

When UNIX OS goes to process the open system call, it uses the major device number to index into a table of all of the character drivers installed on the system. The declaration of this table looks like: struct cdevsw {

};

int ( *d_open )( ); int ( *d_close )( ); int ( *d_read )( ); int ( *d_write )( ); int ( *d_loctl )( ); struct tty *d_ttys; struct streamtab *d_str; char *dname;

Entry points

UNIX Device Drivers

What is a Device Driver?

10

Every member of the table is a structure containing pointers to each of the five main entry points for each character driver. The remaining three members of structure are pointer to various data structures that are not used by the line printer ( as in our example ). If the kernel has stored the major device number in a variable called dev_major, it can invoke the drivers open entry point using the expression cdevsw[ dev_major ].d_open( .. )

UNIX Device Drivers

What is a Device Driver?

11

Types of UNIX device drivers


1. 2. 3. 4.

Block Drivers Character Drivers Terminal Drivers STREAMS Drivers

UNIX Device Drivers

What is a Device Driver?

12

Block Drivers

Block drivers communicate with the OS through a collection of fixed sized buffers as shown in diagram. The OS manages a cache of these buffers and attempts to satisfy user requests for data by accessing buffers in cache. The driver is invoked only when the requested data is not in the cache, or when buffers in the cache have been changed and must be re written. Because of this buffer cache, the driver only needs to handle requests from the OS to fill or empty fixed sized buffers. Block drivers are basically used to support devices that can contain file system ( hard disks ).

UNIX Device Drivers

What is a Device Driver?

13

User Process

Kernel
Read / write system call handler

Driver Strategy

Read / Write system calls

Buffer management routines

Buffer cache headers Buffer cache data

UNIX Device Drivers

What is a Device Driver?

14

Character Drivers

Character drivers can handle I/O requests of arbitrary size and can be used to support any type of device. Usually, they are used for devices that either deal with data a byte at a time ( such as a line printer ) or work with data chunks ( smaller or larger ) than the standard fixed size buffers used by block drivers ( such as an analog to digital converter or tape drivers ). Unlike block drivers, in character drivers user processes interact without any buffer cache.

The I/O request is passed unchanged to the driver to process and character driver is responsible for data transfer to and from user processs memory.
What is a Device Driver?
15

UNIX Device Drivers

User Process

Kernel Driver

Read / Write system calls

Read / write system call handler

Read / write entry points

UNIX Device Drivers

What is a Device Driver?

16

Terminal Drivers

Terminal drivers are really just character drivers specialized to deal with communication terminals that connect users to central UNIX computer system. They are responsible not only for transferring data to and from users terminals, but also for handling line editing, tab expansion and many other terminal functions. Because of this additional processing that terminal drivers must perform, they are considered as a separate type of driver altogether.

UNIX Device Drivers

What is a Device Driver?

17

User Process

Kernel Driver

Read / Write system calls

Read / write system call handler

Read / write entry points


Line discipline routines

Proc routine

UNIX Device Drivers

What is a Device Driver?

18

STREAMS Drivers

STREAMS drivers are used to handle high speed communications devices such as networking adapters that deal with unusual sized chunks of data and that need to handle protocols. Versions of UNIX prior to System V Release 3 supported network devices using character drivers. This was unsatisfactory because the character model assumes that a single driver sits between the user process and the device.

Remember that with character drivers the user processs request is handled directly by the driver with a little processing of the kernel.

UNIX Device Drivers

What is a Device Driver?

19

Networking devices, however, usually support a number of layered protocols. The character model essentially required that each layer of the protocol be implemented within the single driver. This lack of modularity reduced the efficiency of the system.

UNIX Device Drivers

What is a Device Driver?

20

As a result, an extension of character driver STREAMS was developed.

model call

This new type of driver was introduced by AT&T in UNIX System V Release 3 and makes it possible to stack protocol processing modules between the user process and the driver. This can be seen in the diagram.

Stacking modules in the described way makes it much easier to implement network protocols.

UNIX Device Drivers

What is a Device Driver?

21

User Process

Kernel Driver

Read / Write

system calls

Stream head

STREAMS module (optional)

STREAMS driver

UNIX Device Drivers

What is a Device Driver?

22