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

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

Embed Size (px)

Citation preview

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

COMP 3438 – Part I - Lecture 4Introduction to Device Drivers

Dr. Zili Shao

Department of Computing

The Hong Kong Polytechnic Univ.

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

Overview of the Subject (COMP 3438)

Overview of Unix Sys. Prog.

Process File 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)

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

OutlineLecture 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 IA simple device driver – “Hello world”

Lecture 5-II: Char device driver IIA device driver for LED

Lecture 6: Block Device Driver A device driver for RAM DiskLecture 5-II: Char device driver III

Interrupt-driven driver (Button)

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

Part I: Intro. to Device Drivers

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

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

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

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.

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

7

UNIX System Architecture

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

File Subsystem & Char/Block Device Driver Tables

File Subsystemopen close read write ioctl open close read write

mount unmount

Character Device Driver Table Block Device Driver Table

Buffer Cache

open close read write ioctlChar Device Driver

Device Interrupt Handling

open close StrategyBlock Device Driver

Device Interrupt Handling

Interrupt Vectors

Interrupts

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

Char/Block Device Driver Tables

conopen conclose conread conwrite conioctl

testopen testclose testread testwrite nodev

dzbopen dzbclose dzbread dzbwrite dzbioctl

syopen nulldev syread sywrite syioctl

nulldev nulldev mmread mmwrite nodev

Character Device Driver Table Block Device Driver Table

open close read write ioctl open close Strategygdopen gdclose gdstrategy

rdopen rdclose rdstrategy

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.

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

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

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

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.

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

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 file major number

minor number

block device file major number

minor number

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

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

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

14

Types of Device Drivers

Based on the differences in the way that device drivers communicate with Unix O.S. Block Character Terminal STREAMS

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

Block Device Drivers

Block drivers – Communicate with O.S. through a collections of fixed-sized buffers.

User Process Kernel Drivers

read/writesystem calls

read/write system call handler

buffer management routines

buffer cache headers

buffer cache dataStrategy

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

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

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

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)

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

Character Device Drivers

The communication structure of character device driver

User Process KernelDrivers

read/writesystem calls read/write system call handler read/write

entry points

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

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

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

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

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

Terminal Drivers

The communication structure of Terminal Drivers

User Process Kernel

Driversread/writesystem calls

read/write system call handler

line discipline routines

read/writeentry points

proc routines(The additional processing and theadditional kernel routines that areprovided to handle terminal services)

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

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.

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

STREAM Drivers

The communication structure of Terminal Drivers

User Process Kernel

Drivers

read/writesystem calls

Stream

Head

STREAMSModules

(Optional)

STREAMSDrivers

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

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

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

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