Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Understanding Device Drivers.  Overview Device Driver Architecture Native Device Drivers Device Driver Memory Management Device Driver Interrupt.

Similar presentations


Presentation on theme: "Chapter 7: Understanding Device Drivers.  Overview Device Driver Architecture Native Device Drivers Device Driver Memory Management Device Driver Interrupt."— Presentation transcript:

1 Chapter 7: Understanding Device Drivers

2  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

3 Device Driver Architecture Windows CE Architecture Device Power Management

4 OEM Hardware Embedded Shell Applications WIN32 APIs COREDLL, WINSOCK, OLE, COMMCTRL, COMMDLG, WININET, TAPI Windows CE Shell Services Remote Connectivity KernelLibrary IrDA GWES Device Manager FileManager TCP/IP OALBootloaderDrivers Device drivers File drivers Microsoft OEM ISV, OEM Windows CE Architecture Networkdrivers

5 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

6  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

7 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

8 Programming Model GWES Monolithic device driver Hardware Device Driver DDSI functions MDD layer PDD layer DDI functions

9 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

10 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

11 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

12 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

13 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

14  Device Driver Memory Management Memory Management Functions Windows CE Address Space Drivers and Pointer Data Driver Memory Access Memory Access in Drivers

15 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

16 Memory Management Functions ( continued ) MapPtrToProcess GetCurrentProcess GetOwnerProcess UnMapPtr SetProcPermissions Volatile qualifier

17 Not Used Slot 0 Slot KB 32 MB 64 MB 2 GB 3 GB 4 GB Memory mapped files Above 2G-3G Mapped to physical memory Virtual address space Slot 1 Slot 32 NULL pointers Windows CE Address Space 512M Cached512M Non-Cached 0x xA Accessable via MmMapiIoSpace

18 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.

19 Driver Memory Access App Calls DeviceIOControl DM mapped into Slot 0 App is mapped into Slot 0 DM Calls xxx_IoControl DM Sets permissions to App space APP DM Slot 0 APP Mapped pointer Driver Calls MapPtrToProcess Driver returns from xxx_IoControl DM Re-Sets permissions to App space App is mapped into Slot 0 APPDMAPP Original pointer

20 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.

21  Device Driver Interrupt Handling Interrupt Run Time Interrupt Service Thread Interrupt Management Functions Typical IST Start Typical IST Typical Stop

22 Interrupt Run Time I/O Routines OEM Hardware ISROAL routines OAL Exception Handler Interrupt Support Handler Kernel IST Driver

23 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

24 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

25 Interrupt Management Functions ISR HookInterrupt UnhookInterrupt IST InterruptInitialize InterruptDone InterruptDisable

26 Typical IST Start struct ISTData // Declare the Strucure to pass to the IST { HANDLE hThread; // IST Handle HANDLE hThread; // IST Handle DWORD sysIntr; // Logical ID DWORD sysIntr; // Logical ID HANDLE hEvent; // handle to the event to wait for interrupt HANDLE hEvent; // handle to the event to wait for interrupt volatile BOOL abort; // flag to test to exit the IST 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); &g_KeypadISTData, 0, NULL);

27 Typical IST Start (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);

28 Typical IST DWORD KeypadIST(void *dat) { ISTData* pData= (ISTData*)dat; ISTData* pData= (ISTData*)dat; // loop until told to stop // loop until told to stop While(!pData->abort) While(!pData->abort) { // wait for the interrupt event... { // wait for the interrupt event... WaitForSingleObject(pData->hEvent, INFINITE) WaitForSingleObject(pData->hEvent, INFINITE) if(pData->abort) if(pData->abort) break; break; // Handle the interrupt... // Handle the interrupt... // Let OS know the interrupt processing is done // Let OS know the interrupt processing is done InterruptDone(pData->sysIntr); InterruptDone(pData->sysIntr); } Return 0; Return 0;}

29 Typical IST Stop //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);

30  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

31 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

32 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

33 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

34 Programming Model User application Kernel File system code Device manager Stream interface driver Native device driver Windows CE-based platform Peripheral device

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 Lab A: Developing a Stream Interface Device Driver

46 Display Driver Architecture Windows CE Graphic Pipeline GPE Driver Pipeline Key Display Driver Source Files

47 HARDWARE GWES.EXE DDI.DLL COREDLL.DLL DRAWPROG.EXE Windows CE Graphic Pipeline

48 GPE Driver Architecture HARDWARE – DIB-Compatible Video Buffer GWES.EXE DDI.DLL GPE.LIB DrvEnableDriver EMUL.LIBHardware Acceleration

49 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

50 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

51 Text Raster Vector  Abc Abcdefghijklmnopqr Abcdefghijklmnopqrstuvwxyz Abc Abcdefghijklmn  Families of Drawing Functions

52 (0,0) +Y +X Drawing Coordinates

53 Text Drawing - ExtTextOut, DrawText Vector Drawing - Polyline, Rectangle, Ellipse, Polygon, RoundRect Raster Drawing - SetPixel, PatBlt, BitBlt, MaskBlt GDI Drawing Functions

54 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); ExtTextOut(hdc,x,y, NULL, NULL, "hello", 5, NULL); hello (x,y)  Drawing Text

55 Pixels SetPixel GetPixel Fill An Area PatBlt Copy Device-Dependent Bitmaps BitBlt StretchBlt Draw Device Independent Bitmaps BitBlt StretchDIBits Drawing Raster Data

56 Lines Polyline Filled Figures Polygon Ellipse Rectangle Drawing Vector Objects

57 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); } 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); } Window Drawing: WM_PAINT

58 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; } 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; } MINGDI Display Drawing

59 Inside Display Drivers Display Device Drivers Connecting to a Driver Queries, Attributes and Drawing Disconnecting DDI Vector Functions DDI Raster Functions DDI Text Functions

60 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

61 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

62 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

63 CreateDC Only way to access graphic devices Calls driver’s initialization functions Application Driver CreateDC COREDLL.DLL + GWES.EXE DrvEnableDriver, DrvEnablePDEV, DrvCompletePDEV Hardware Connecting to a Driver

64 GetDeviceCaps Queries key values SelectObject Object selection function No call to driver Polyline Request to draw Calls driver to do “real- work” Application Driver Hardware COREDLL.DLL + GWES.EXE SelectObject (…) Polyline(…) DrvStrokePath GetDeviceCaps(…) Queries, Attributes & Drawing

65 Application disconnects from GDI GDI disconnects from driver. Application Driver Hardware COREDLL.DLL + GWES.EXE DeleteDC DrvDisablePDEV Disconnecting

66 Application COREDLL.DLL + GWES.EXE DRIVER MoveToExPolylineEllipsePolyBezier DrvStrokePathDrvFillPath DrvStrokeAndFillPath DrvBitBltDrvPaint PolygonRectangle DDI Vector Functions

67 App COREDLL.DLL + GWES.EXE DRIVER SetPixel BitBlt MaskBlt Rectangle SetDlBitsToDevice DrvCopyBits DrvBitBlt GetDIBitsSetDIBits ExtTextOut DDI Raster Functions

68 API ExtTextOut DrawText DDI DrvBitBlt DDI Text Functions

69 Lab B: Exploring Video Display Drivers

70 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


Download ppt "Chapter 7: Understanding Device Drivers.  Overview Device Driver Architecture Native Device Drivers Device Driver Memory Management Device Driver Interrupt."

Similar presentations


Ads by Google