Chapter 7 understanding device drivers
Download
1 / 70

Chapter 7: Understanding Device Drivers - PowerPoint PPT Presentation


  • 135 Views
  • Uploaded on

Chapter 7: Understanding Device Drivers. Overview. Device Driver Architecture Native Device Drivers Device Driver Memory Management Device Driver Interrupt Handling Stream Device Drivers Display Driver Architecture Primer on GDI Programming Inside Display Drivers.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 7: Understanding Device Drivers' - bella


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Overview
Overview

  • Device Driver Architecture

  • Native Device Drivers

  • Device Driver Memory Management

  • Device Driver Interrupt Handling

  • Stream Device Drivers

  • Display Driver Architecture

  • Primer on GDI Programming

  • Inside Display Drivers


Device driver architecture
Device Driver Architecture

  • Windows CE Architecture

  • Device Power Management


Windows ce architecture

RemoteConnectivity

WindowsCEShellServices

WIN32APIs

COREDLL, WINSOCK, OLE, COMMCTRL, COMMDLG, WININET, TAPI

Windows CE Architecture

OEM

ISV,OEM

Microsoft

Applications

EmbeddedShell

IrDA

Kernel

Library

TCP/IP

GWES

File

Manager

Device Manager

Filedrivers

Drivers

Devicedrivers

OAL

Bootloader

Network

drivers

OEMHardware


Device power management
Device Power Management

  • Power status notification function

  • Shutdown operations

    • Save device state to preallocated storage

    • Issue commands to the device that is shutting down

    • Set a flag within the driver to note that it has been shut down

  • Do not call Win32 API functions during a shutdown sequence


Native device drivers
Native Device Drivers

  • Overview of Native Device Drivers

  • Programming Model

  • MDD/PDD Layers

  • Battery and Notification LED Device Drivers

  • PC Card Socket Device Drivers

  • Keyboard Device Drivers


Overview of native device drivers
Overview of Native Device Drivers

  • Designed to drive built-in devices

  • Provided by OEMs

  • Supports custom interfaces

  • Loaded by GWES

  • Device Driver Interface (DDI)

    • Interface between the OS and device drivers is called DDI

    • DDI functions are defined for each device driver class

    • DDI functions are defined by Microsoft and cannot be changed


Programming model
Programming Model

GWES

DDI functions

DDI functions

Monolithic

device driver

Device Driver

MDD layer

DDSI functions

PDD layer

Hardware


Mdd pdd layers
MDD/PDD Layers

  • Model Device Driver (MDD) Layer

    • Provided by Microsoft

    • Platform-independent code

    • Communicates with GWES and Kernel

    • Handles interrupts

  • Platform-Dependent Driver (PDD) Layer

    • Provided by OEMs

    • Focus only on specific platform porting issue

    • Communicates with the hardware


Mdd pdd layers continued
MDD/PDD Layers (continued)

  • MDD layer handles the DDI interface

  • Interface between MDD and PDD is the Device Driver Service-Provider Interface (DDSI)

  • DDSI interfaces may be redefined by OEMs with corresponding changes in the MDD layer

  • When performance is an issue, the PDD layer can be bypassed - all processing can be done in MDD layer

  • MDD code is provided as source code and as libraries


Battery and notification led device drivers
Battery and Notification LED Device Drivers

  • Battery device driver

    • Provides information about power level of main and backup batteries

    • Library statically linked with GWES

    • Monolithic; uses only DDI functions

  • Notification LED driver

    • Library statically linked with GWES

    • Handles all requests to turn the system notification LED on or off


Pc card socket device drivers
PC Card Socket Device Drivers

  • Manages any PC Card sockets

  • Abstracts hardware specifics so that a PC Card driver can run unchanged on any platform

  • The MDD Layer exposes functions that can be used when developing a stream interface device driver for individual PC Cards

  • MDD functions constitute the Card Services library

  • PPD layer exposes low-level functions that constitute the Socket Services library


Keyboard device drivers
Keyboard Device Drivers

  • MDD Layer

    • Maps scan code to virtual key code

    • Generates character data associated with VK code

    • Packages keyboard messages and puts them in the system message queue

  • PDD Layer

    • Retrieves scan code from hardware


Device driver memory management
Device Driver Memory Management

  • Memory Management Functions

  • Windows CE Address Space

  • Drivers and Pointer Data

  • Driver Memory Access

  • Memory Access in Drivers


Memory management functions
Memory Management Functions

  • Device drivers are user-mode modules - it is necessary to map physical memory to virtual memory

  • VirtualAlloc, VirtualFree: reserve, free virtual memory

    • MEM_RESERVE

  • VirtualCopy: maps a physical memory range to a virtual memory range

    • PAGE_NOCACHE

    • PAGE_PHYSICAL


Memory management functions continued
Memory Management Functions (continued)

  • MapPtrToProcess

    • GetCurrentProcess

    • GetOwnerProcess

  • UnMapPtr

  • SetProcPermissions

  • Volatile qualifier


Windows ce address space

512M Non-Cached

512M Cached

Windows CE Address Space

4 GB

Not Used

Accessable via MmMapiIoSpace

3 GB

Above 2G-3G

Mapped to physical memory

0xA0000000

Virtual address space

0x80000000

2 GB

Memory mapped files

Slot 32

Slot 32

64 MB

Slot 1

32 MB

Slot 0

64 KB

NULL pointers


Drivers and pointer data
Drivers and Pointer data

  • OS manages pointers passed directly as parameters

  • Driver must map All pointers contained in structs

    • DeviceIOControl buffers often are structs that contain data, some of which may be pointers.

    • Use MapPtrToProcess( SomePtr, GetOwnerProcess())

      • GetOwnerProcess() is used only in the context of a driver and cannot be used by Apps.


Driver memory access
Driver Memory Access

APP

APP

APP

App is mapped into Slot 0

Mapped pointer

App Calls DeviceIOControl

DM mapped into Slot 0

DM

DM Sets permissions to App space

DM Calls xxx_IoControl

APP

APP

DM

Driver Calls MapPtrToProcess

Original pointer

Slot 0

Driver returns from xxx_IoControl

DM Re-Sets permissions to App space

App is mapped into Slot 0


Memory access in drivers
Memory Access in Drivers

  • If driver creates a thread to do the work. MapPtrToProcess() will not work without some help

    • You must use GetCurrentPermissions() and SetProcPermissions() to capture and reset the process permissions.


Device driver interrupt handling
Device Driver Interrupt Handling

  • Interrupt Run Time

  • Interrupt Service Thread

  • Interrupt Management Functions

  • Typical IST Start

  • Typical IST

  • Typical Stop


Interrupt run time

Driver

Kernel

Exception

Handler

Interrupt

Support Handler

IST

ISR

OAL routines

I/O Routines

OAL

OEM Hardware

Interrupt Run Time

6

5

8

4

7

2

3

9

1


Interrupt service thread
Interrupt Service Thread

  • Uses user-mode thread of device drivers for built-in devices

  • Does the actual processing of the interrupt

  • Creates an event object associated with the logical interrupt

  • IST remains idle most of the time, awakened when the kernel signals the event object

  • IST usually runs at above-normal priority


Interrupt service thread continued
Interrupt Service Thread (continued)

  • Boost IST priority with CeSetThreadPriority function

  • Create an event object with CreateEvent function

  • Associate event object with SYSINTR_ by calling InterruptInitialize function

  • Code a loop whose first operation is WaitForSingleObject

  • The kernel signals the event object, which causes WaitForSingleObject to return

  • Perform I/O in the device and process data

  • Call InterruptDone to signal interrupt handling completion


Interrupt management functions
Interrupt Management Functions

  • ISR

    • HookInterrupt

    • UnhookInterrupt

  • IST

    • InterruptInitialize

    • InterruptDone

    • InterruptDisable


Typical ist start
Typical IST Start

struct ISTData // Declare the Strucure to pass to the IST

{

HANDLE hThread; // IST Handle

DWORD sysIntr; // Logical ID

HANDLE hEvent; // handle to the event to wait for interrupt

volatile BOOL abort; // flag to test to exit the IST

};

ISTData g_KeypadISTData;

// Create event to link to IST

g_KeypadISTData.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

// Translate IRQ to an logical ID (x86 CEPC)

g_KeypadISTData.sysIntr =Mapirq2Sysintr(5);

// start the thread

g_KeypadISTData.hThread = CreateThread(NULL,0,&KeypadIST,

&g_KeypadISTData, 0, NULL);


Typical ist start continued
TypicalISTStart (Continued)

//Change the threads priority

CeSetThreadPriority(g_KeypadISTData.hThread,0);

//disconnect any previous event from logical ID

InterruptDisable(g_KeypadISTData.sysIntr);

// Connect Logical ID with Event

InterruptInitialize(g_KeypadISTData.sysIntr, g_KeypadISTData.

hEvent,NULL,0);


Typical ist
TypicalIST

DWORD KeypadIST(void *dat)

{

ISTData* pData= (ISTData*)dat;

// loop until told to stop

While(!pData->abort)

{ // wait for the interrupt event...

WaitForSingleObject(pData->hEvent, INFINITE)

if(pData->abort)

break;

// Handle the interrupt...

// Let OS know the interrupt processing is done

InterruptDone(pData->sysIntr);

}

Return 0;

}


Typical ist stop
TypicalISTStop

//disconnect event from logical ID

InterruptDisable(g_KeypadISTData.sysIntr);

// set abort flag to true to let thread know

// that it should exit

g_KeypadISTData.abort =TRUE;

// manually set the event to break thread

// out of wait call

SetEvent(g_KeypadISTData.hEvent);

//wait for thread to exit

WaitForSingleObject(g_KeypadISTData.hEvent,INFINITE);

CloseHandle(g_KeypadISTData.hEvent);

CloseHandle(g_KeypadISTData.hThread);


Stream device drivers
Stream Device Drivers

  • Overview of Stream Interface Device Drivers

  • Device Manager

  • Programming Model

  • Loading Mechanism

  • Entry Points

  • Programming Considerations

  • Audio Device Drivers

  • File System Drivers


Overview of stream interface device drivers
Overview of Stream Interface Device Drivers

  • Designed to support installable devices

  • Common interface for all device drivers

  • Expose device services through Win32 File I/O API functions

  • Loaded, controlled, and unloaded by the Device Manager

  • Can also be loaded directly by an application

  • Often uses services from underlying native drivers

    • Modem driver using the native serial driver


Device manager
Device Manager

The goal of a user-level process is to:

  • Load device drivers either at startup or on connection notifications

  • Register special file names in the system

  • Locate device drivers by obtaining PnP ID or by executing detection routines

  • Track device drivers by reading and writing registry values

  • Unload device drivers


Device manager continued
Device Manager (continued)

  • Device file name

    • All operations on device files are redirected to their respective drivers

  • Device file name format is XXXY: where

    • XXX is the prefix and consists of three letters in uppercase

    • Y is the index and allows several devices to be handled by the same driver

  • The names of the stream interface functions always begin with the device file name as a prefix


Programming model1

User application

Stream

interface

driver

File system code

Device

manager

Native

device

driver

Kernel

Windows CE-based platform

Peripheral device

Programming Model


Loading mechanism
Loading Mechanism

  • Load Drivers Listed in HKLM\Drivers\BuiltIn

    • Required values : Dll, Prefix

    • Useful values : Index, Order

  • Load PCMCIA drivers when detected

    • HKLM\Drivers\PCMCIA\Plug and Play ID

    • HKLM\Drivers\PCMCIA\Detect

    • HKLM\Drivers\PCMCIA\Driver


Loading mechanism continued
Loading Mechanism (continued)

  • Application can load a driver dynamically by calling the RegisterDevice function

  • For each loaded device driver, the Device Manager creates a key under HKLM\Drivers\Active

    • Argument passed to the initialization function

    • Hnd : the device handle

    • Name : the device file name

    • Key : the registry path to the device key under HKLM


Entry points
Entry Points

  • XXX_Init

    • Called when Device Manager loads the driver

    • Initializes resources that are to be used: memory mapping, etc.

  • XXX_Deinit

    • Called when Device Manager unloads the driver

    • Frees allocated resources, stops the IST


Entry points continued
Entry Points (continued)

  • XXX_Open

    • Invoked when applications call CreateFile(XXX, …)

    • Returns a context handle that will be used in subsequent calls to XXX_Read, XXX_Write, XXX_Seek, XXX_IOControl, and XXX_Close

  • XXX_Close

    • Invoked when applications call CloseHandle function


Entry points continued1
Entry Points (continued)

  • XXX_Read

    • Invoked when application calls ReadFile function

  • XXX_Write

    • Invoked when application calls WriteFile function

  • XXX_Seek

    • Allows moving the current I/O pointer


Entry points continued2
Entry Points (continued)

  • XXX_IOControl

    • Allows performing custom operations that do not necessarily apply to files

    • I/O control code identifies the operation

    • I/O control code is device-specific

  • XXX_PowerXXXX

    • PowerUp and PowerDown notifications

    • Should complete execution as quickly as possible


Programming considerations
Programming Considerations

  • Single or multiple access to a device

    • Multiple: XXX_Open always returns a unique context handle

    • Single: XXX_Open returns one context handle on the first call and NULL for subsequent calls

  • For unusual operations on files, use the XXX_IOControl function

  • Read-only or write-only device


Audio device drivers
Audio Device Drivers

  • Sample PCM Waveform Audio Driver

    • WAV prefix

    • Relies mostly on XXX_IOControl

    • You can use wavemdd.lib to build your own wavepdd.lib and link the two together to form the wavedev.dll

  • Audio Compression Manager driver

    • ACM prefix

    • Codec, Converter, Filter

    • You can refer to Windows NT ACM documentation


File system drivers
File System Drivers

  • Designed for drivers that allow data to be read or written in blocks of fixed size

  • Block device functionality

    • Extend the size of the Object Store

    • Store code and data

    • Interoperability between operating systems

    • Registry storage

    • Database storage


File system drivers continued
File System Drivers (continued)

  • ATA flash memory

    • Emulates the behavior of an ATA-style hard drive with a chip

  • Linear memory flash

    • Uses a software layer called flash translation layer (FTL) to emulate a disk drive

  • Block device prefix is DSK

  • All I/O are handled by the DSK_IOControl function with the same I/O control codes as the ones used by the FAT file system driver



Display driver architecture
Display Driver Architecture

  • Windows CE Graphic Pipeline

  • GPE Driver Pipeline

  • Key Display Driver Source Files


Windows ce graphic pipeline

Windows CE Graphic Pipeline

Windows CE Graphic Pipeline

DRAWPROG.EXE

GWES.EXE

DDI.DLL

COREDLL.DLL

HARDWARE


Gpe driver architecture
GPE Driver Architecture

GWES.EXE

DDI.DLL

DrvEnableDriver

GPE.LIB

DrvEnableDriver

Hardware Acceleration

EMUL.LIB

HARDWARE – DIB-Compatible Video Buffer


Key display driver source files
Key Display Driver Source Files

  • In ..\public\common\oak\inc:

    • WINDDI.H – Driver interface definitions

    • EMUL.H – Drawing helpers for raster drawing

    • GPE.H – Basic set of data structures & defines

  • GPE Source Files

    • In ..\private\winceos\ coreos\gwe\mgdi\gpe


Primer on gdi programming
Primer on GDI Programming

  • Families of Drawing Functions

  • Drawing Coordinates

  • GDI Drawing Functions

  • Drawing Text

  • Drawing Raster Data

  • Drawing Vector Objects

  • Window Drawing: WM_PAINT

  • MINGDI Display Drawing


Families of Drawing Functions

Abcdefghijklmnopqrstuvwxyz

Abcdefghijklmn



Abc

Text

Raster

Vector



Abcdefghijklmnopqr

Abc


Drawing Coordinates

(0,0)

+X

+Y


GDI Drawing Functions

  • Text Drawing - ExtTextOut, DrawText

  • Vector Drawing - Polyline, Rectangle, Ellipse, Polygon, RoundRect

  • Raster Drawing - SetPixel, PatBlt, BitBlt, MaskBlt


Drawing Text

  • Draws a line of text.

  • ExtTextOut (hdc, x, y, Options, pRect, lpstr, cch, int * lpDX);

    • HDC hdc - DC handle.

    • int x - X coordinate

    • int y - Y coordinate

    • UINT Options - NULL Ok.

    • RECT * pRect - NULL Ok.

    • LPTSTR psz - string

    • UINT cch - string length.

    • int * lpDX - NULL - Ok.

ExtTextOut(hdc,x,y,

NULL, NULL, "hello", 5, NULL);

(x,y)

hello


Drawing Raster Data

  • Pixels

    • SetPixel

    • GetPixel

  • Fill An Area

    • PatBlt

  • Copy Device-Dependent Bitmaps

    • BitBlt

    • StretchBlt

  • Draw Device Independent Bitmaps

    • BitBlt

    • StretchDIBits


Drawing Vector Objects

  • Lines

    • Polyline

  • Filled Figures

    • Polygon

    • Ellipse

    • Rectangle


Window Drawing: WM_PAINT

LRESULT CALLBACK

WndProc(HWND hwnd, UINT msg,...){

switch (msg) {

case WM_PAINT:

{

HDC hdc;

PAINTSTRUCT ps;

hdc = BeginPaint(hwnd,&ps);

// draw, draw, draw...

TextOut (hdc,x,y,lp,cb);

EndPaint (hwnd, &ps);

}


MINGDI Display Drawing

int WINAPI

WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

HDC hdc = CreateDC(L"display", 0, 0, 0);

ExtTextOut(hdc, 10, 10, NULL, NULL,

L"Hello World", 11, 0);

DeleteDC(hdc);

return 0;

}


Inside display drivers
Inside Display Drivers

  • Display Device Drivers

  • Connecting to a Driver

  • Queries, Attributes and Drawing

  • Disconnecting

  • DDI Vector Functions

  • DDI Raster Functions

  • DDI Text Functions


Display device drivers
Display Device Drivers

  • Subset of the Windows NT DDI, with the following differences:

    • Display drivers always present the same functionality

    • Display drivers cannot reject an operation as too complex

  • Display drivers are native drivers because they have a custom interface with GWES

  • Loaded by GWES


Display device drivers continued
Display Device Drivers (continued)

  • Display drivers are usually layered

  • Graphic Primitive Engine (GPE)

    • Acts as the MDD layer

    • Optional

    • Handles all the default drawing

    • Requires the display hardware to use a flat-frame buffer

  • PDD layer

    • IHVs provide hardware-specific code


Display device drivers continued1
Display Device Drivers (continued)

  • Secondary display driver

    • Loaded by the Device Manager when calling CreateDC

    • You cannot use any window manager functions

    • You can easily display the contents of the primary display

  • Accelerating BitBlt and line drawing

    • Default GPE routines

    • Emulated library

    • Hardware acceleration


Connecting to a Driver

Application

  • CreateDC

    • Only way to access graphic devices

    • Calls driver’s initialization functions

CreateDC

COREDLL.DLL + GWES.EXE

DrvEnableDriver,

DrvEnablePDEV,

DrvCompletePDEV

Driver

Hardware


Queries, Attributes & Drawing

Application

  • GetDeviceCaps

    • Queries key values

  • SelectObject

    • Object selection function

    • No call to driver

  • Polyline

    • Request to draw

    • Calls driver to do “real-work”

GetDeviceCaps(…)

Polyline(…)

SelectObject (…)

COREDLL.DLL + GWES.EXE

DrvStrokePath

Driver

Hardware


Disconnecting

Application

  • Application disconnects from GDI

  • GDI disconnects from driver.

DeleteDC

COREDLL.DLL + GWES.EXE

DrvDisablePDEV

Driver

Hardware


DDI Vector Functions

Application

MoveToEx Polyline Ellipse PolyBezier

Polygon Rectangle

COREDLL.DLL + GWES.EXE

DrvStrokePath DrvFillPath

DrvStrokeAndFillPath

DrvBitBlt DrvPaint

DRIVER


DDI Raster Functions

App

SetPixel

BitBlt

MaskBlt

Rectangle

ExtTextOut

SetDlBitsToDevice

GetDIBits

SetDIBits

COREDLL.DLL + GWES.EXE

DrvCopyBits

DrvBitBlt

DRIVER


API

ExtTextOutDrawText

DDI

DrvBitBlt

DDI Text Functions



Review
Review

  • Device Driver Architecture

  • Native Device Drivers

  • Device Driver Memory Management

  • Device Driver Interrupt Handling

  • Stream Device Drivers

  • Display Driver Architecture

  • Primer on GDI Programming

  • Inside Display Drivers


ad