Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.

Similar presentations


Presentation on theme: "COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ."— Presentation transcript:

1 COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.

2 Overview of the Subject (COMP 3438) Overview of Unix Sys. Prog. ProcessFile System Overview of Device Driver Development Character Device Driver Development Introduction to Block Device Driver Overview of Complier Design Lexical Analysis Syntax Analysis Part I: Unix System Programming (Device Driver Development) Part II: Compiler Design Course Organization (This lecture is in red)

3 Outline Lecture 4: Introduction to Device Drivers 1. What is a device driver? 2. Related kernel data structures from a generic view 3. Devices and Files 4. Major design issues 5. Types of device drivers Lecture 5-I: Char device driver I A simple device driver – “Hello world” Lecture 5-II: Char device driver II A device driver for LED Lecture 6: Block Device Driver A device driver for RAM Disk Lecture 5-II: Char device driver III Interrupt-driven driver (Button)

4 Part I: Intro. to Device Drivers

5 What is a device driver? A device driver is a computer program that glues an O.S. and its I/O devices together. Device drivers act as translators Converting generic requests received from an O.S. into commands that specific peripheral controllers can understand. Example Generic File Requests: write(fd, buf, 100); Kernel – Device Driver: write to harddisk 2, block 12345

6 Related Kernel Data Structures In Unix, from a generic view, the following structures are related to device drivers: Unix System Architecture File Subsystem and its relations with Char/Block device driver tables Char/Block device driver tables “from a generic view” – what that means? In a particular Unix O.S., these structures or table names may be different from we mention here. But the basic idea should be the same.

7 7 UNIX System Architecture

8 File Subsystem & Char/Block Device Driver Tables File Subsystem open close read write ioctl open close read write mount unmount Character Device Driver TableBlock Device Driver Table Buffer Cache open close read write ioctl Char Device Driver Device Interrupt Handling open close Strategy Block Device Driver Device Interrupt Handling Interrupt Vectors Interrupts

9 Char/Block Device Driver Tables conopenconcloseconreadconwriteconioctl testopentestclosetestreadtestwritenodev dzbopendzbclosedzbreaddzbwritedzbioctl syopennulldevsyreadsywritesyioctl nulldev mmreadmmwritenodev Character Device Driver Table Block Device Driver Table open close read write ioctl open close Strategy gdopengdclosegdstrategy rdopenrdcloserdstrategy 0 1 3 4 5 Major Number: The index (id) of a device driver in char/block device driver tables. Minor Number: A number is used inside a device driver, e.g., one driver may control many devices then minor number can be used to distinguish them.

10 10 Advantages to Separate Device Drivers from O.S. Designers for O.S. Devices may not be available when an O.S. is designed Do not need to worry about how to operate devices (set up registers, check statuses, … ). Focus on O.S. itself by providing a generic interface for device driver development Designers for Device Drivers Do not need to worry about how I/O is managed in O.S. (how to design kernel data structures so as to efficient operate devices, … ) Focus on implementing functions of devices with device-related commands following the generic I/O interface

11 11 Devices and Files In Unix, devices are treated as special files. A file is associated with an inode. We can use mknod file_name c/b major_number minor_number to create a special file for a device file. When we create a special file (an inode) for a device file, we associate major/minor numbers with the file (the inode). The major number associated with a special file is used to identify its corresponding device drivers in the kernel.

12 12 Basically, there are two types of device files: character device file and block device file. Example: ls -l /dev/null crw-rw-rw-r 1 root root 1 3 June 1, 2013 null ls -l /dev/bon br--r--r-- 1 root root 97 0 June 1, 2013 0 br--r--r-- 1 root root 97 1 June 1, 2013 1 Character/Block Device Files character device filemajor number minor number block device filemajor number minor number

13 13 Major Design Issues O.S. / Device Driver Communication Device Driver / Hardware Communication Device Driver Operations Interpret commands received from O.S. Schedule multiple requests for services Manage data transfer across both interfaces Accept & process hardware interrupts Maintain the integrity of the device ’ s and kernel ’ s data structures

14 14 Types of Device Drivers Based on the differences in the way that device drivers communicate with Unix O.S. Block Character Terminal STREAMS

15 Block Device Drivers Block drivers – Communicate with O.S. through a collections of fixed-sized buffers. User Process Kernel Drivers read/write system calls read/write system call handler buffer management routines buffer cache headers buffer cache data Strategy

16 16 Block Device Drivers O.S. Manage a buffer cache Satisfy user requests for data by accessing buffers in the cache Driver is invoked when Requested data is not in the cache When buffers in the cache have been changed and must be written out (write back to the devices) By using buffer cache, block drivers be insulated from many of the details of users ’ requests only need to handle requests from O.S. to fill or empty fixed-size buffer Mainly support devices that contain file systems

17 Character Device Drivers Character devices can handle I/O requests of arbitrary size support almost any type of devices Be used to handle data a byte at a time (e.g. keyboard) or work best with data in chunks smaller or larger than the standard fixed-size buffer used by device driver (e.g. ADC)

18 Character Device Drivers The communication structure of character device driver User Process Kernel Drivers read/write system calls read/write system call handler read/write entry points

19 Major differences between Block/Char drivers Major difference with block driver Block driver – only interact with buffer cache Char driver – direct interact with user requests from user processes I/O requests are directly passed (essentially unchanged) to the drivers from processes Char driver is responsible for transferring data directly to and from between kernel memory space and user memory space

20 Terminal Drivers Special character drivers to deal with communication terminals that connect users to the central Unix systems Responsibilities Transfer data to and from users’ terminals Handle line editing and other terminal functions that are part of the standard Unix terminal interface described by the TERMIO manual page

21 Terminal Drivers The communication structure of Terminal Drivers User Process Kernel Drivers read/write system calls read/write system call handler line discipline routines read/write entry points proc routines (The additional processing and the additional kernel routines that are provided to handle terminal services)

22 STREAM Drivers Handle high-speed communication devices such as network adapters that deal with unusual-sized chunk of data and that need to handle protocols. To handle these, especially for protocols, we need to kernel’s help (character driver is not good enough). The kernel provides some routines/modules to support the processing of a number of layered protocols.

23 STREAM Drivers The communication structure of Terminal Drivers User Process Kernel Drivers read/write system calls Stream Head STREAMS Modules (Optional) STREAMS Drivers

24 General Programming Considerations Device drivers are parts of the kernel and not normal user processes, which means We can only use the kernel routines C library functions or system calls provided for users cannot be used Some kernel routines may have the same names as C library functions, but they are totally different in implementation Make frugal use of stack (local arrays & recursive functions) The stack space in the kernel is limited and not expandable Don’t use floating-point arithmetic – May cause incorrect results Don’t do busy wait that will prevent the whole system from doing nothing but responding to interrupts

25 Summary A device driver is a computer program that glues O.S. and its I/O devices together A device is treated as a special file in Unix. There are four types of device drivers based on the different communication manner between device drivers and O.S. Block Character Terminal STREAMS


Download ppt "COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ."

Similar presentations


Ads by Google