Unix Device Drivers

Embed Size (px)

Citation preview

  • 7/30/2019 Unix Device Drivers

    1/22

    UNIX Device Drivers What is a Device Driver? 1

    What is a Device Driver ?

    1. The Design

    2. UNIX device driver3. Types of UNIX device drivers

    Chapter 1

  • 7/30/2019 Unix Device Drivers

    2/22

    UNIX Device Drivers What is a Device Driver? 2

    The Design

    A device driver is a bridge between an OS and its I/Odevices.

    Device drivers act as translators, converting the genericrequests received from OS into commands that specificcontrollers can understand.

  • 7/30/2019 Unix Device Drivers

    3/22

    UNIX Device Drivers What is a Device Driver? 3

    ApplicationsSoftware

    UNIX

    OS Kernel

    Screen

    Device DriverKeyboard

    Device Driver

    Floppy

    Device Driver

    Hard disk

    Device Driver

    Screen

    InterfaceKeyboard

    Interface

    Floppy

    Interface

    Hard disk

    Interface

    Screen

    Keyboard

    Floppy

    Hard disk

    Software Hardware

  • 7/30/2019 Unix Device Drivers

    4/22

    UNIX Device Drivers What is a Device Driver? 4

    The application software makes system calls to the OSrequesting services ( for example, write data to file x ).

    The OS analyzes these requests and when necessary

    issues requests to the appropriate device drivers ( forexample, write this data to disk 2, block 23654 ).

    The device driver in turn issues commands to the

    hardware interface to perform the operations needed toservice the request. ( e.g transfer 1024 bytes starting ataddress 23500 to disk2, cylinder 173, head 7, sector 8 ).

  • 7/30/2019 Unix Device Drivers

    5/22

    UNIX Device Drivers What is a Device Driver? 5

    Although this process seems complex, but withoutdevice drivers, the OS would be responsible for talkingdirectly to the hardware.

    This would mean that OS designer would have toinclude support for all devices that a user may connect.

    Also it means that adding support for a new devicewould demand modifying the OS itself.

  • 7/30/2019 Unix Device Drivers

    6/22

    UNIX Device Drivers What is a Device Driver? 6

    By separating device drivers functions from OS itself, detailsrelated to individual hardware can be ignored and generic requestsfor 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 hardwarein order to fulfill the request.

    Thus the result is the clean separation of responsibilities and theability to add device drivers for new devices without changing theOS.

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

  • 7/30/2019 Unix Device Drivers

    7/22

    UNIX Device Drivers What is a Device Driver? 7

    UNIX device driver

    A UNIX device driver is a collection of functions, usuallywritten in C, that can be called by UNIX OS usingstandard 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 filecontaining the bootable OS with all the device drivers.

  • 7/30/2019 Unix Device Drivers

    8/22

    UNIX Device Drivers What is a Device Driver? 8

    To understand drivers in UNIX better, consider thefollowing example:

    echo Hello World > /dev/lp

    The C code that is executed for opening the file /dev/lpis like:

    fileds = open( /dev/lp, O_WRONLY );

  • 7/30/2019 Unix Device Drivers

    9/22

    UNIX Device Drivers What is a Device Driver? 9

    UNIX first examines the file /dev/lp and determines that it is, infact, 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 characterdevice driver.

    The number 6(major) and 0(minor) are the device numbers for thisspecial file.

    The major number specifies the device driver while the minornumber is used by the driver to distinguish between differentdrivers under the control of a single driver.

  • 7/30/2019 Unix Device Drivers

    10/22

    UNIX Device Drivers What is a Device Driver? 10

    When UNIX OS goes to process the open system call, it uses the majordevice number to index into a table of all of the character drivers installedon 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

  • 7/30/2019 Unix Device Drivers

    11/22

    UNIX Device Drivers What is a Device Driver? 11

    Every member of the table is a structure containing pointers to eachof 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 ourexample ).

    If the kernel has stored the major device number in a variable calleddev_major, it can invoke the drivers open entry point using the

    expression

    cdevsw[ dev_major ].d_open( .. )

  • 7/30/2019 Unix Device Drivers

    12/22

    UNIX Device Drivers What is a Device Driver? 12

    Types of UNIX device

    drivers

    1. Block Drivers

    2. Character Drivers

    3. Terminal Drivers

    4. STREAMS Drivers

  • 7/30/2019 Unix Device Drivers

    13/22

    UNIX Device Drivers What is a Device Driver? 13

    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 satisfyuser requests for data by accessing buffers in cache.

    The driver is invoked only when the requested data is not in thecache, or when buffers in the cache have been changed and must bere 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 containfile system ( hard disks ).

  • 7/30/2019 Unix Device Drivers

    14/22

    UNIX Device Drivers What is a Device Driver? 14

    Read / Write

    system calls

    Read / write system call handler

    Buffer management routines

    Buffer cache headers

    Buffer cache data

    Strategy

    Driver

    KernelUser Process

  • 7/30/2019 Unix Device Drivers

    15/22

    UNIX Device Drivers What is a Device Driver? 15

    Character Drivers

    Character drivers can handle I/O requests of arbitrary size and canbe used to support any type of device.

    Usually, they are used for devices that either deal with data a byteat a time ( such as a line printer ) or work with data chunks ( smaller

    or larger ) than the standard fixed size buffers used by blockdrivers ( such as an analog to digital converter or tape drivers ).

    Unlike block drivers, in character drivers user processes interactwithout any buffer cache.

    The I/O request is passed unchanged to the driver to process andcharacter driver is responsible for data transfer to and from userprocesss memory.

  • 7/30/2019 Unix Device Drivers

    16/22

  • 7/30/2019 Unix Device Drivers

    17/22

    UNIX Device Drivers What is a Device Driver? 17

    Terminal Drivers

    Terminal drivers are really just character drivers specializedto deal with communication terminals that connect users tocentral UNIX computer system.

    They are responsible not only for transferring data to andfrom users terminals, but also for handling line editing, tabexpansion and many other terminal functions.

    Because of this additional processing that terminal driversmust perform, they are considered as a separate type of driveraltogether.

  • 7/30/2019 Unix Device Drivers

    18/22

    UNIX Device Drivers What is a Device Driver? 18

    Read / Write

    system callsRead / write system call handler

    Line discipline routines

    Read / writeentry points

    Driver

    KernelUser Process

    Proc routine

  • 7/30/2019 Unix Device Drivers

    19/22

    UNIX Device Drivers What is a Device Driver? 19

    STREAMS Drivers

    STREAMS drivers are used to handle high speed communicationsdevices such as networking adapters that deal with unusual sizedchunks of data and that need to handle protocols.

    Versions of UNIX prior to System V Release 3 supported networkdevices using character drivers.

    This was unsatisfactory because the character model assumes that asingle driver sits between the user process and the device.

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

  • 7/30/2019 Unix Device Drivers

    20/22

    UNIX Device Drivers What is a Device Driver? 20

    Networking devices, however, usually support a number oflayered 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.

  • 7/30/2019 Unix Device Drivers

    21/22

    UNIX Device Drivers What is a Device Driver? 21

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

    This new type of driver was introduced by AT&T in UNIX

    System V Release 3 and makes it possible to stack protocolprocessing modules between the user process and the driver.

    This can be seen in the diagram.

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

  • 7/30/2019 Unix Device Drivers

    22/22

    UNIX Device Drivers What is a Device Driver? 22

    Read / Write

    system calls

    Driver

    KernelUser Process

    STREAMSdriver

    STREAMSmodule

    (optional)

    Streamhead