64
Windows Embedded Windows Embedded CE 6.0 CE 6.0 All about All about Device Drivers Device Drivers by- by- Rahul Rahul P Shukla P Shukla

Device Driver in WinCE 6.0 R2

Embed Size (px)

DESCRIPTION

Device Driver in WinCE 6.0 R2

Citation preview

Page 1: Device Driver in WinCE 6.0 R2

Windows Windows Embedded CE 6.0Embedded CE 6.0

All about All about Device DriversDevice Drivers

by-by- Rahul P Rahul P

ShuklaShukla

Page 2: Device Driver in WinCE 6.0 R2

Topics..Topics..1.1. Device Driver BasicsDevice Driver Basics

2.2. Implementing a Stream Interface Implementing a Stream Interface DriverDriver

3.3. Configuring and Loading a DriverConfiguring and Loading a Driver

4.4. Implementing an Interrupt Implementing an Interrupt Mechanism in a Device DriverMechanism in a Device Driver

5.5. Implementing Power Management for Implementing Power Management for a Driver.a Driver.

Page 3: Device Driver in WinCE 6.0 R2

1.1. Device Driver BasicsDevice Driver Basics

Page 4: Device Driver in WinCE 6.0 R2

What is a What is a Device Driver Device Driver ??

A driver is software that provides the operating system (OS) A driver is software that provides the operating system (OS) with an interface to a physical or a virtual device. with an interface to a physical or a virtual device.

The operating system expects drivers to implement a predefined The operating system expects drivers to implement a predefined interface that creates an abstraction of a specific hardware or a interface that creates an abstraction of a specific hardware or a virtual implementation of a device.virtual implementation of a device.

The device drivers contain the hardware-specific code, and The device drivers contain the hardware-specific code, and we can implement custom drivers in addition to the we can implement custom drivers in addition to the standard drivers that ship with CE to support additional standard drivers that ship with CE to support additional peripherals.peripherals.

As device driver is a dynamic-link library (DLL) that As device driver is a dynamic-link library (DLL) that provides a layer of abstraction between the underlying provides a layer of abstraction between the underlying hardware, OS, and applications running on the target hardware, OS, and applications running on the target device.device.

Device Driver Basics Device Driver Basics

Page 5: Device Driver in WinCE 6.0 R2

Device Driver Basics Device Driver Basics

Classification of Device DriverClassification of Device Driver

Implementation architectureImplementation architecture Layered driverLayered driver Monolithic driver.Monolithic driver. Hybrid driverHybrid driver

Loading moduleLoading module Device Manager (device.dll) - Stream drivers.Device Manager (device.dll) - Stream drivers. GWES (gwes.dll) - Native drivers that are used only by the GWES (gwes.dll) - Native drivers that are used only by the

Graphics, Windowing, andGraphics, Windowing, andEvents Subsystem (GWES).Events Subsystem (GWES).

File system (filesys.dll) - drivers of file systems.File system (filesys.dll) - drivers of file systems.

Loading into memoryLoading into memory Kernel MemoryKernel Memory User MemoryUser Memory

System load time.System load time.

System StartupSystem Startup On RequestOn Request

Page 6: Device Driver in WinCE 6.0 R2

Types of supported device.Types of supported device.

Serial port.Serial port. Video adapter.Video adapter. Network card.Network card. Touch screen.Touch screen. Keyboard.Keyboard. Mouse.Mouse. Human interface device (HID), and so on..Human interface device (HID), and so on..

Device Driver Basics Device Driver Basics

Page 7: Device Driver in WinCE 6.0 R2

Device Driver BasicsDevice Driver Basics

1.1. Implementation architecture:Implementation architecture:WinCE defines also 3 types of devices drives on the basis of design:WinCE defines also 3 types of devices drives on the basis of design:

a)a) Layered DriversLayered DriversWindows Embedded CE supports a layered driver architecture based Windows Embedded CE supports a layered driver architecture based on model device driver (MDD) and platform dependent driver (PDD).on model device driver (MDD) and platform dependent driver (PDD).

Page 8: Device Driver in WinCE 6.0 R2

Device Driver Basics Device Driver Basics

MDD:MDD: It is common for a certain class of device drivers by It is common for a certain class of device drivers by providing the operating system with a required interface – providing the operating system with a required interface – (DDI) usually, as a defined set of IOCTL codes and, possibly, (DDI) usually, as a defined set of IOCTL codes and, possibly, functions.functions.

It also defines IST (Interrupt service thread) and DDSI – It also defines IST (Interrupt service thread) and DDSI – (Device Driver Service Interface) interface for interacting with (Device Driver Service Interface) interface for interacting with PDD layerPDD layer

PDD:PDD: The PDD library implements the actual functions to The PDD library implements the actual functions to communicate with the hardware and provide the information communicate with the hardware and provide the information to the MDD layer with a pre-defined set of functions (DDSI).to the MDD layer with a pre-defined set of functions (DDSI).

Advantage of using layered architecture:Advantage of using layered architecture: Code reuseCode reuse Lower development overhead and costs.Lower development overhead and costs. Time saving during QFE fixes to customers. Increase Time saving during QFE fixes to customers. Increase

development efficiencydevelopment efficiency Generally implement the PDD layer and use the common Generally implement the PDD layer and use the common

MDD layer implementation.MDD layer implementation.

Page 9: Device Driver in WinCE 6.0 R2

b)b) Hybrid Drivers:Hybrid Drivers: Use the same two-level Use the same two-level MDD/PDD model implies MDD persistence, where MDD/PDD model implies MDD persistence, where the same MDD is used for all PDDs but with extra the same MDD is used for all PDDs but with extra unique functionality (that is a logical extension of the unique functionality (that is a logical extension of the MDD/PDD implementation for a given device type) MDD/PDD implementation for a given device type)

Advantage of using Hybrid Drivers:Advantage of using Hybrid Drivers: Same as Layered Same as Layered architecture.architecture.

Device Driver Basics Device Driver Basics

Page 10: Device Driver in WinCE 6.0 R2

c)c) Monolithic DriversMonolithic Drivers

A monolithic driver relies on a single DLL to A monolithic driver relies on a single DLL to implement both the interface to the operating system implement both the interface to the operating system and applications, and the logic to the hardware.and applications, and the logic to the hardware.

A monolithic driver implements the interface with the A monolithic driver implements the interface with the operating system (e.g. DDI) and interacts directly operating system (e.g. DDI) and interacts directly with a specific hardware implementation.with a specific hardware implementation.

Device Driver BasicsDevice Driver Basics

Page 11: Device Driver in WinCE 6.0 R2

Continue..Continue..This type of architecture is usually utilized in the following This type of architecture is usually utilized in the following

cases:cases:

When there is no layered model for a device type.When there is no layered model for a device type. Device hardware is uncommon and custom hardware.Device hardware is uncommon and custom hardware. No layered driver code exists that could be reused.No layered driver code exists that could be reused. When using an MDD/PDD model, it is not possible to When using an MDD/PDD model, it is not possible to

achieve a required efficiency level.achieve a required efficiency level.

Advantage:Advantage: High efficiency by avoiding the switching High efficiency by avoiding the switching between separatebetween separate

layers and to maximize the hardware use.layers and to maximize the hardware use.

Disadvantage: Disadvantage: Building a monolithic driver is the most Building a monolithic driver is the most complex task.complex task. Higher Development cost and time.Higher Development cost and time.

Page 12: Device Driver in WinCE 6.0 R2

Monolithic and layered driver architecturesMonolithic and layered driver architectures

Continue..Continue..

Page 13: Device Driver in WinCE 6.0 R2

The directories that contain the majority of drivers included with Windows The directories that contain the majority of drivers included with Windows Embedded CE. Embedded CE.

Table: Included driver directories.Table: Included driver directories.

Device Driver Device Driver Basics Basics

Page 14: Device Driver in WinCE 6.0 R2

2.2. Loading Module:Loading Module:Windows Embedded CE the following three modules (parts of the kernel) Windows Embedded CE the following three modules (parts of the kernel) can load drivers:can load drivers:

Device.dll:Device.dll: Handled by Device Manager and referred as Handled by Device Manager and referred as Stream Stream Drivers.Drivers.

Gwes.dll:Gwes.dll: Handled by GWES and referred as Handled by GWES and referred as Native drivers.Native drivers. FileSys.dll:FileSys.dll: Handled by File System. Handled by File System.

Device ManagerDevice ManagerThe Windows Embedded CE Device Manager is the OS componentThe Windows Embedded CE Device Manager is the OS component

that manages the stream device drivers on the system. The OALthat manages the stream device drivers on the system. The OAL

(Oal.exe) loads the kernel (Kernel.dll), and the kernel loads Device(Oal.exe) loads the kernel (Kernel.dll), and the kernel loads Device

Manager during the boot process. Specifically, the kernel loads theManager during the boot process. Specifically, the kernel loads the

Device Manager shell (Device.dll), which in turn loads the actual Device Manager shell (Device.dll), which in turn loads the actual corecore

Device Manager code (Devmgr.dll), which again is in charge of Device Manager code (Devmgr.dll), which again is in charge of loading,loading,

unloading, and interfacing with stream drivers.unloading, and interfacing with stream drivers.

Device Driver Device Driver

BasicsBasics

Page 15: Device Driver in WinCE 6.0 R2

Stream Device Drivers:Stream Device Drivers: Handled by Device Manager.Handled by Device Manager.

Expose a well-known set of functions that enable Device Manager to load Expose a well-known set of functions that enable Device Manager to load and manage these drivers.and manage these drivers.

For Device Manager to interact with a stream driver, the driver must For Device Manager to interact with a stream driver, the driver must implement the Init, Deinit, Open, Close, Read, Write, Seek, and IOControl implement the Init, Deinit, Open, Close, Read, Write, Seek, and IOControl functions.functions.

Native Device Drivers: Native Device Drivers: Native CE drivers typically support input and output peripherals, such as Native CE drivers typically support input and output peripherals, such as

display drivers, keyboard drivers, and touch screen drivers.display drivers, keyboard drivers, and touch screen drivers.

The Graphics, Windowing, and Events Subsystem (GWES) loads and The Graphics, Windowing, and Events Subsystem (GWES) loads and manages these drivers directly.manages these drivers directly.

Native drivers implement specific functions according to their purpose, Native drivers implement specific functions according to their purpose, which GWES can determine by calling the GetProcAddress API.which GWES can determine by calling the GetProcAddress API.

Device Driver Device Driver

BasicsBasics

Page 16: Device Driver in WinCE 6.0 R2

GWESGWES ( (Graphics, Windowing, and Events SubsystemGraphics, Windowing, and Events Subsystem): ): Supports windows, dialog boxes, controls, menu, and other Supports windows, dialog boxes, controls, menu, and other resources related to the user interface. Controls window manager resources related to the user interface. Controls window manager and window messaging manager, including keyboard messages, and window messaging manager, including keyboard messages, mouse messages, touch screen messages, and so on.mouse messages, touch screen messages, and so on.

The GWES (GWES.dll) module loads the device drivers that are The GWES (GWES.dll) module loads the device drivers that are exclusively used by this system, which are all the following exclusively used by this system, which are all the following drivers related in any way to the user interface: keyboard, video drivers related in any way to the user interface: keyboard, video adapter, touch screen, printer, and mouse. adapter, touch screen, printer, and mouse.

File System: File System: File system is responsible for object store, registry, CEDB File system is responsible for object store, registry, CEDB database, and system initialization.database, and system initialization.

The file system (FileSys.dll) module loads the file system drivers. The file system (FileSys.dll) module loads the file system drivers. File system drivers are implemented as a DLL that implements a File system drivers are implemented as a DLL that implements a predefined set of functions and IOCTL control codes. These predefined set of functions and IOCTL control codes. These functions are called by using a standard set of file system functions are called by using a standard set of file system application programming interfaces (APIs) through the files that application programming interfaces (APIs) through the files that the file system driver registered.the file system driver registered.

Device Driver Device Driver

BasicsBasics

Page 17: Device Driver in WinCE 6.0 R2

Question Time.Question Time.Can you answer this..Can you answer this..

1.1. Generally Which layer need to be modified in Layered Driver Generally Which layer need to be modified in Layered Driver Architecture?Architecture?

2.2. Which module loads an Stream Drivers?Which module loads an Stream Drivers?

3.3. What are the name of interfaces b/w MDD layer – OS and MDD What are the name of interfaces b/w MDD layer – OS and MDD Layer – PDD layer?Layer – PDD layer?

Page 18: Device Driver in WinCE 6.0 R2

2.2. Implementing a Stream Implementing a Stream Interface DriverInterface Driver

Page 19: Device Driver in WinCE 6.0 R2

Stream Interface APIStream Interface API

For Device Manager to load and manage a stream driver successfully, For Device Manager to load and manage a stream driver successfully, the stream driver must export a common interface, generally referred the stream driver must export a common interface, generally referred to as the stream interface. to as the stream interface.

The stream interface consists of The stream interface consists of 1212 functions to initialize and open functions to initialize and open the device, read and write data, power up or down the device, and the device, read and write data, power up or down the device, and close and de-initialize the device.close and de-initialize the device.

XXX_InitXXX_Init XXX_IOControlXXX_IOControlXXX_PreDeinitXXX_PreDeinit XXX_ReadXXX_ReadXXX_DeinitXXX_Deinit XXX_WriteXXX_WriteXXX_OpenXXX_Open XXX_SeekXXX_Seek

XXX_CloseXXX_Close XXX_PowerUp XXX_PowerUp XXX_PreCloseXXX_PreClose XXX_PowerDownXXX_PowerDown

NOTE:NOTE: In the function names, the prefix XXX is a placeholder that In the function names, the prefix XXX is a placeholder that refers to the three-letter driver name.refers to the three-letter driver name.

Implementing a Stream Implementing a Stream Interface DriverInterface Driver

Page 20: Device Driver in WinCE 6.0 R2

Implementing a Stream Implementing a Stream

Interface DriverInterface Driver

Page 21: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 22: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 23: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 24: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 25: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 26: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 27: Device Driver in WinCE 6.0 R2

Driver Naming Convention:Driver Naming Convention:The Device Manager registers the following three different file The Device Manager registers the following three different file

namespaces in thenamespaces in thefile system for accessing named stream drivers:file system for accessing named stream drivers:

Legacy:Legacy: The classical naming convention for stream drivers The classical naming convention for stream drivers consists of three upper case letters, a digit, and a colon. The consists of three upper case letters, a digit, and a colon. The format is format is XXXXXX[0–9]:, where [0–9]:, where XXX XXX stands for the three-letter driver stands for the three-letter driver name and [name and [0–90–9] is the index of the driver as specified in the ] is the index of the driver as specified in the driver’s registry settings . For example, CreateFile(L"COM1:"…)driver’s registry settings . For example, CreateFile(L"COM1:"…)

Device - Based: Device - Based: A device namespace ($device) is similar to a A device namespace ($device) is similar to a legacy space but the former has no index restriction. format \legacy space but the former has no index restriction. format \$device\XXX[index]. For example, CreateFile(L"\$device\$device\XXX[index]. For example, CreateFile(L"\$device\COM11"…)COM11"…)

Bus - Based:Bus - Based: This is for bus based devices as PCMCIA or USB. This is for bus based devices as PCMCIA or USB. Format is \$bus\BUSNAME_[bus number]_[device Format is \$bus\BUSNAME_[bus number]_[device number]_[function number]number]_[function number]For example, CreateFile(L"\$ bus\PCMCIA_0_0_0"…).For example, CreateFile(L"\$ bus\PCMCIA_0_0_0"…).

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 28: Device Driver in WinCE 6.0 R2

Building a Device DriverBuilding a Device Driver

To create a device driver, you can add a subproject for a Windows To create a device driver, you can add a subproject for a Windows Embedded CEEmbedded CE

DLL to your OS design, but the most common way to do it is to add the DLL to your OS design, but the most common way to do it is to add the device device

driver’s source files inside the Drivers folder of the Board Support driver’s source files inside the Drivers folder of the Board Support Package (BSP).Package (BSP).

The following code listing shows the definition of the stream interface The following code listing shows the definition of the stream interface functions.functions.

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 29: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 30: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 31: Device Driver in WinCE 6.0 R2

Exporting Stream FunctionsExporting Stream Functions

Making the stream functions in the driver DLL accessible to external Making the stream functions in the driver DLL accessible to external applicationsapplications

requires the linker to export the functions during the build process.requires the linker to export the functions during the build process.

For driver DLLs compatible with Device Manager, you must export the For driver DLLs compatible with Device Manager, you must export the functions byfunctions by

defining them in the .def file of the DLL subproject.defining them in the .def file of the DLL subproject.

The linker uses the .def file to determine which functions to export and The linker uses the .def file to determine which functions to export and how to do so.how to do so.

For a standard stream driver, you must export the stream interface For a standard stream driver, you must export the stream interface functions usingfunctions using

the prefix that you specify in the driver’s source code and registry settings.the prefix that you specify in the driver’s source code and registry settings.

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 32: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 33: Device Driver in WinCE 6.0 R2

Dynamically Loading a DriverDynamically Loading a DriverAn application can also communicate with a stream device driver after calling An application can also communicate with a stream device driver after calling

the the ActivateDevice() or ActivateDeviceEx().ActivateDevice() or ActivateDeviceEx().

Both functions cause Device Manager to load the stream driver and call the Both functions cause Device Manager to load the stream driver and call the driver’sdriver’s

XXX_Init function.XXX_Init function.

ActivateDeviceEx function reads the driver-specific registry key specified in ActivateDeviceEx function reads the driver-specific registry key specified in thethe

function call to determine the DLL name, device prefix, index, and other function call to determine the DLL name, device prefix, index, and other values, addvalues, add

the relevant values to the active device list, and then load the device driver the relevant values to the active device list, and then load the device driver into theinto the

Device Manager process space.Device Manager process space.

Note:Note: ActivateDeviceEx replaces the older RegisterDevice function as a ActivateDeviceEx replaces the older RegisterDevice function as a method tomethod to

load a driver on demand,load a driver on demand,

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 34: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 35: Device Driver in WinCE 6.0 R2

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 36: Device Driver in WinCE 6.0 R2

NOTE: Automatic vs. dynamic loading of driversNOTE: Automatic vs. dynamic loading of drivers

Calling ActivateDeviceEx to load a driver has the same result as loading Calling ActivateDeviceEx to load a driver has the same result as loading the driver automatically during the boot process through parameters the driver automatically during the boot process through parameters defined in the HKEY_LOCAL_MACHINE\Drivers\BuiltIn key.defined in the HKEY_LOCAL_MACHINE\Drivers\BuiltIn key.

A stream driver does not necessarily have to support a named interface. If A stream driver does not necessarily have to support a named interface. If a driver does not have to interact with other drivers or applications, then a driver does not have to interact with other drivers or applications, then it may not implement the functions that are responsible for the access to a it may not implement the functions that are responsible for the access to a named interface –XXX_Open/XXX_Close.named interface –XXX_Open/XXX_Close.

Implementing a Stream Interface Implementing a Stream Interface DriverDriver

Page 37: Device Driver in WinCE 6.0 R2

Question TimeQuestion Time

Can you answer this..Can you answer this..

1.1. How many functions a Stream driver must export?How many functions a Stream driver must export?

2.2. How many max instance can be created in Legacy driver naming How many max instance can be created in Legacy driver naming convention?convention?

Page 38: Device Driver in WinCE 6.0 R2

3.3. Configuring and Loading a Configuring and Loading a DriverDriver

Page 39: Device Driver in WinCE 6.0 R2

Device Driver Load ProcedureDevice Driver Load Procedure

Device Manager uses the following procedure to load device drivers at Device Manager uses the following procedure to load device drivers at boot time:boot time:

1.1. Device Manager reads the HKEY_LOCAL_MACHINE\Drivers\Device Manager reads the HKEY_LOCAL_MACHINE\Drivers\RootKey entry to determine the location of the device driver RootKey entry to determine the location of the device driver entries in the registry. The default value of the RootKey entry is entries in the registry. The default value of the RootKey entry is Drivers\BuiltIn.Drivers\BuiltIn.

2.2. Device Manager reads the Dll registry value at the location Device Manager reads the Dll registry value at the location specified in the RootKey location HKEY_LOCAL_MACHINE\Drivers\specified in the RootKey location HKEY_LOCAL_MACHINE\Drivers\BuiltIn) to determine the enumerator DLL to load. By default, this BuiltIn) to determine the enumerator DLL to load. By default, this is the bus Enumerator BusEnum.dll).is the bus Enumerator BusEnum.dll).

3.3. The bus enumerator runs and scan the RootKey registry location The bus enumerator runs and scan the RootKey registry location for subkeys that refer to additional buses and devices and for subkeys that refer to additional buses and devices and examines the Order value in each sub key to determine the load examines the Order value in each sub key to determine the load order.order.

4.4. Starting with the lowest Order values, the bus enumerator iterates Starting with the lowest Order values, the bus enumerator iterates through the subkeys and calls ActivateDeviceEx passing in the through the subkeys and calls ActivateDeviceEx passing in the current driver’s registry path (HKEY_LOCAL_MACHINE\Drivers\current driver’s registry path (HKEY_LOCAL_MACHINE\Drivers\BuiltIn\BuiltIn\<DriverName><DriverName>).).

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 40: Device Driver in WinCE 6.0 R2

5.5. ActivateDeviceEx loads the driver DLL registered in the DLL value ActivateDeviceEx loads the driver DLL registered in the DLL value located in the driver’s subkey, and then creates a subkey for the located in the driver’s subkey, and then creates a subkey for the driver under the HKEY_LOCAL_MACHINE\Drivers\Active registry driver under the HKEY_LOCAL_MACHINE\Drivers\Active registry key to keep track of all currently loaded drivers.key to keep track of all currently loaded drivers.

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 41: Device Driver in WinCE 6.0 R2

Registry Settings to Load Device Drivers:Registry Settings to Load Device Drivers:

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 42: Device Driver in WinCE 6.0 R2

Flag Values:Flag Values:

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 43: Device Driver in WinCE 6.0 R2

Kernel-Mode and User-Mode DriversKernel-Mode and User-Mode Drivers

Drivers can be loaded into either the kernel space (kernel-mode drivers), or into aDrivers can be loaded into either the kernel space (kernel-mode drivers), or into aspecialized user-mode drivers host process-Udevice.exe (user-mode drivers). Thespecialized user-mode drivers host process-Udevice.exe (user-mode drivers). Thedrivers loaded by the GWES and FileSys subsystems can only be kernel-modedrivers loaded by the GWES and FileSys subsystems can only be kernel-modedrivers. The drivers loaded by the Device Manager (Device.dll) can be both drivers. The drivers loaded by the Device Manager (Device.dll) can be both

kernel-kernel-mode drivers and user-mode drivers. By default DM load the drivers in kernel mode drivers and user-mode drivers. By default DM load the drivers in kernel

memory. memory.

Kernel – Mode Advantage:Kernel – Mode Advantage:

Kernel-level driver has full access to user memory, as well as full access to kernelKernel-level driver has full access to user memory, as well as full access to kernelmemory, so the enhanced performance & efficiency. memory, so the enhanced performance & efficiency. Kernel mode drivers can access the internal kernel structures, and API. Kernel mode drivers can access the internal kernel structures, and API.

Disadvantage:Disadvantage:

Failure of a kernel-mode driver can impair the entire operating system. Failure of a kernel-mode driver can impair the entire operating system. Kernel-mode drivers cannot display the user interface directly. To support this Kernel-mode drivers cannot display the user interface directly. To support this

capability in the OS image, add the UI Proxy for kernel mode drivers capability in the OS image, add the UI Proxy for kernel mode drivers (SYSGEN_UIPROXY)(SYSGEN_UIPROXY)component into an OS design.component into an OS design.

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 44: Device Driver in WinCE 6.0 R2

User-Mode Drivers and the Reflector ServiceUser-Mode Drivers and the Reflector Service::

As user mode driver can not access the system memory and privileged APIs, As user mode driver can not access the system memory and privileged APIs, OSOS

facilitates a Reflector service, which runs in kernel mode, performs buffer facilitates a Reflector service, which runs in kernel mode, performs buffer marshaling, marshaling,

and calls privileged memory management APIs on behalf of the user mode and calls privileged memory management APIs on behalf of the user mode drivers.drivers.

When an application calls ActivateDeviceEx, Device Manager loads the driver When an application calls ActivateDeviceEx, Device Manager loads the driver eithereither

directly in kernel space or passes the request to the Reflector service, which directly in kernel space or passes the request to the Reflector service, which in turn startsin turn starts

a user mode driver host process (Udevice.exe) through CreateProcess(). a user mode driver host process (Udevice.exe) through CreateProcess().

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 45: Device Driver in WinCE 6.0 R2

For each user-mode driver, a reflector service object is created and is For each user-mode driver, a reflector service object is created and is responsible forresponsible for

the following functionality:the following functionality:

It loads and controls the host process.It loads and controls the host process. It reroutes calls to the driver over to the host process from the operating It reroutes calls to the driver over to the host process from the operating

system.system. It transforms pointer parameters (first-level pointers) of the calling It transforms pointer parameters (first-level pointers) of the calling

process to the driver address space.process to the driver address space. It provides the user-mode driver with access to It provides the user-mode driver with access to somesome kernel-mode kernel-mode

services and APIs.services and APIs.

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 46: Device Driver in WinCE 6.0 R2

User-mode driver loading process.User-mode driver loading process.

Configuring and Loading a DriverConfiguring and Loading a Driver

Page 47: Device Driver in WinCE 6.0 R2

Question TimeQuestion Time

Can you answer this..Can you answer this..

1.1. What is the name of ONLY required registry entry for a Stream Diver?What is the name of ONLY required registry entry for a Stream Diver?

2.2. What is the name of Host process for a user-mode driver?What is the name of Host process for a user-mode driver?

3.3. Which memory DM loads a driver by default?Which memory DM loads a driver by default?

Page 48: Device Driver in WinCE 6.0 R2

4.4. Implementing an Interrupt Implementing an Interrupt Mechanism in a Device DriverMechanism in a Device Driver

Page 49: Device Driver in WinCE 6.0 R2

Interrupts:Interrupts:

Interrupts are notifications generated either in hardware or software to Interrupts are notifications generated either in hardware or software to inform theinform the

CPU that an event has occurred that requires immediate attention, such CPU that an event has occurred that requires immediate attention, such as timeras timer

events or keyboard events.events or keyboard events.

In response to an interrupt, the CPU stops executing the current thread, In response to an interrupt, the CPU stops executing the current thread, jumps to a jumps to a

trap handler in the kernel to respond to the event, and then resumes trap handler in the kernel to respond to the event, and then resumes executing theexecuting the

original thread after the interrupt is handled.original thread after the interrupt is handled.

ISRISR: Interrupt Service Routine: Interrupt Service Routine

ISTIST: Interrupt Service thread: Interrupt Service thread

IRQIRQ: Interrupt Request: Interrupt Request

SYSINTRSYSINTR: Logical interrupt identifier: Logical interrupt identifier

Interrupt Mechanism in a Interrupt Mechanism in a Device DriverDevice Driver

Page 50: Device Driver in WinCE 6.0 R2

Interrupt Service Routine (ISR)Interrupt Service Routine (ISR)

ISRs are small blocks of code that run in response to a hardware interrupt.ISRs are small blocks of code that run in response to a hardware interrupt.ISRs examine an interrupt and determine how to handle it by returning aISRs examine an interrupt and determine how to handle it by returning aSYSINTR value, which is then associated with an IST. ISRs run in kernel SYSINTR value, which is then associated with an IST. ISRs run in kernel mode.mode.

Interrupt Service Thread (IST)Interrupt Service Thread (IST)

The IST is a thread that does most of the interrupt processing. The OS wakes The IST is a thread that does most of the interrupt processing. The OS wakes thetheIST when the OS has an interrupt to process. After each IST is associated to aIST when the OS has an interrupt to process. After each IST is associated to aSYSINTR value, the SYSINTR value can be returned from an ISR, and then SYSINTR value, the SYSINTR value can be returned from an ISR, and then thetheassociated IST runs. IST runs in user mode.associated IST runs. IST runs in user mode.

IRQ (Interrupt Request)IRQ (Interrupt Request)

Each hardware interrupt line corresponds to an IRQ value in the interrupt Each hardware interrupt line corresponds to an IRQ value in the interrupt controller registers. Each IRQ value can be associated with only one ISR, but controller registers. Each IRQ value can be associated with only one ISR, but ananISR can map to multiple IRQs.ISR can map to multiple IRQs.

SYSINTR SYSINTR (Logical Interrupt Identifier)(Logical Interrupt Identifier)

The value that corresponds to an IRQ. It is used to signal an associated event. The value that corresponds to an IRQ. It is used to signal an associated event. This value is returned by an ISR in response to an interrupt.This value is returned by an ISR in response to an interrupt.

Interrupt Mechanism in a Interrupt Mechanism in a Device DriverDevice Driver

Page 51: Device Driver in WinCE 6.0 R2

Interrupt Handling ArchitectureInterrupt Handling Architecture

Windows Embedded CE 6.0 interrupt handling is based on the following Windows Embedded CE 6.0 interrupt handling is based on the following concepts:concepts:

1.1. During the boot process, the kernel calls the During the boot process, the kernel calls the OEMInitOEMInit function in function in the OAL to register all available ISRs built into the kernel with the OAL to register all available ISRs built into the kernel with their corresponding hardware interrupts based on their interrupt their corresponding hardware interrupts based on their interrupt request (IRQ) values. request (IRQ) values.

2.2. When an interrupt occurs, CPU stops current execution thread and When an interrupt occurs, CPU stops current execution thread and jumps to the jumps to the kernel exception handlerkernel exception handler..

3.3. Exception handlerException handler masks off (disable) all interrupts of an equal or masks off (disable) all interrupts of an equal or lower priority and then calls the appropriate ISR registered to lower priority and then calls the appropriate ISR registered to handle the current interrupt.handle the current interrupt.

4.4. The ISR masks (disable) the current interrupt and performs some The ISR masks (disable) the current interrupt and performs some more necessary tasks, such as copying data from hardware more necessary tasks, such as copying data from hardware registers to memory buffers and then returns a SYSINTR value. If registers to memory buffers and then returns a SYSINTR value. If no handling is required, it returns SYSINTR_NOP. no handling is required, it returns SYSINTR_NOP.

5.5. The The exception handlerexception handler passes the SYSINTR value to the passes the SYSINTR value to the kernel’s kernel’s interrupt support handlerinterrupt support handler, which determines the event for the , which determines the event for the SYSINTR value, and, if found, signals that event for any waiting SYSINTR value, and, if found, signals that event for any waiting ISTs for the interrupt. ISTs for the interrupt. Interrupt support handlerInterrupt support handler unmasks all unmasks all interrupts, but interrupt currently in processing.interrupts, but interrupt currently in processing.

Interrupt Mechanism in a Device Driver

Page 52: Device Driver in WinCE 6.0 R2

6.6. The IST runs in response to the signaled event to perform and The IST runs in response to the signaled event to perform and finish the interrupt handling.finish the interrupt handling.

7.7. The IST calls the InterruptDone() to inform the kernel’s The IST calls the InterruptDone() to inform the kernel’s interrupt support handlerinterrupt support handler that the IST has finished its that the IST has finished its processing and is ready for another interrupt event.processing and is ready for another interrupt event.

8.8. The interrupt support handler calls the OEMInterruptDone The interrupt support handler calls the OEMInterruptDone function in the OAL to complete the interrupt handling process function in the OAL to complete the interrupt handling process and re-enable the interrupt.and re-enable the interrupt.

IRQs, ISRs, SYSINTRs, and ISTsIRQs, ISRs, SYSINTRs, and ISTs

Interrupt Mechanism in a Device Driver

Page 53: Device Driver in WinCE 6.0 R2

OAL functions for interrupt managementOAL functions for interrupt management

Interrupt Mechanism in a Device Driver

Page 54: Device Driver in WinCE 6.0 R2

Static Interrupt Mappings:Static Interrupt Mappings:

Satatic SYSINTR is hardcoded into OAL’s Satatic SYSINTR is hardcoded into OAL’s Bsp_cfg.hBsp_cfg.h file. These value file. These value resides b/wresides b/w

SYSINTR_FIRMWARE & SYSINTR_MAXIMUM.SYSINTR_FIRMWARE & SYSINTR_MAXIMUM.

Calling Calling OALIntrStaticTranslateOALIntrStaticTranslate function during system initialization function during system initialization adds a mappingadds a mapping

of static SYSINTR values to IRQs.of static SYSINTR values to IRQs.

Mapping resides in kernel’s interrupt mapping array g_oalSysIntr2Irq Mapping resides in kernel’s interrupt mapping array g_oalSysIntr2Irq andand

g_oalIrq2SysIntr.g_oalIrq2SysIntr.

Dynamic Interrupt Mappings:Dynamic Interrupt Mappings:

Calling Calling KernelIoControlKernelIoControl in with an IOCTL of in with an IOCTL of IOCTL_HAL_REQUEST_SYSINTRIOCTL_HAL_REQUEST_SYSINTR

registers the IRQ/SYSINTR mappings, and registers the IRQ/SYSINTR mappings, and IOCTL_HAL_RELEASE_SYSINTRIOCTL_HAL_RELEASE_SYSINTR

Dissociates the IRQ from the SYSINTR value.Dissociates the IRQ from the SYSINTR value.

Interrupt Mechanism in a Device Driver

Page 55: Device Driver in WinCE 6.0 R2

Sample code an ISTSample code an IST

Interrupt Mechanism in a Device Driver

Page 56: Device Driver in WinCE 6.0 R2

Sample code an IST (Cont…)Sample code an IST (Cont…)

Interrupt Mechanism in a Device Driver

Page 57: Device Driver in WinCE 6.0 R2

Installable ISRs (IISR):Installable ISRs (IISR):

It increases flexibility, adaptability and make the OAL as generic as It increases flexibility, adaptability and make the OAL as generic as possible. Apossible. A

installable ISR must be completely self-sufficient.installable ISR must be completely self-sufficient.

LoadIntChainHandler:LoadIntChainHandler: Install an ISR to handle a particular Install an ISR to handle a particular interrupt. interrupt.

FreeIntChainHandler:FreeIntChainHandler: Unloads an existing interrupt handler. Unloads an existing interrupt handler.

ISRHandler:ISRHandler: Contains the installable interrupt handler. Contains the installable interrupt handler.

CreateInstance:CreateInstance: Called when an installable ISR is loaded by using Called when an installable ISR is loaded by using the LoadIntChainHandler function.the LoadIntChainHandler function.

DestroyInstance:DestroyInstance: Called when an installable ISR is unloaded by Called when an installable ISR is unloaded by using the FreeIntChainHandler function.using the FreeIntChainHandler function.

An IISR DLL subproject should declare:An IISR DLL subproject should declare: NOMIPS16CODE=1 NOMIPS16CODE=1 (to ensure no explicit DLL linked.)(to ensure no explicit DLL linked.) NOLIBC=1NOLIBC=1 (to ensure C run time library linked.)(to ensure C run time library linked.)

Interrupt Mechanism in a Device Driver

Page 58: Device Driver in WinCE 6.0 R2

Question TimeQuestion Time

Can you answer this..Can you answer this..

1.1. In which memory ISR executes?In which memory ISR executes?

2.2. If no processing required, What SYSINTR reurned by ISR?If no processing required, What SYSINTR reurned by ISR?

3.3. How many IRSs an IRQ can be associated?How many IRSs an IRQ can be associated?

Page 59: Device Driver in WinCE 6.0 R2

5.5. Implementing Power Management for a Implementing Power Management for a Driver.Driver.

Page 60: Device Driver in WinCE 6.0 R2

Power ManagerPower Manager (PM.dll): (PM.dll):

Kernel component, works with Device Manager, optimize power Kernel component, works with Device Manager, optimize power consumption andconsumption and

enable devices to manage their own power states and applications enable devices to manage their own power states and applications to set powerto set power

requirements for certain devices.requirements for certain devices.

To receive power management notifications:To receive power management notifications: IClass registryIClass registry AdvertiseInterface function.AdvertiseInterface function.

XXX_PowerDown:XXX_PowerDown: Called by kernel right after powering down the Called by kernel right after powering down the CPU.CPU.

XXX_PowerUp:XXX_PowerUp: Called by kernel right after powering it up.Called by kernel right after powering it up.

During these power Up/Down stages, OS works in single During these power Up/Down stages, OS works in single threaded mode with most system calls disabled. MS recommends threaded mode with most system calls disabled. MS recommends use IOCTL functions instead XXX_PowerUp and use IOCTL functions instead XXX_PowerUp and XXX_PowerDown. XXX_PowerDown.

Avoid calling system APIs, particularly thread-blocking APIs.Avoid calling system APIs, particularly thread-blocking APIs.

Power Management for a Driver

Page 61: Device Driver in WinCE 6.0 R2

IClass Power Management Interface:IClass Power Management Interface:

IClass value is a globally unique identifier (GUID) referring to an interface, IClass value is a globally unique identifier (GUID) referring to an interface, defineddefined

under theunder the

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Power\HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Power\InterfacesInterfaces

registry key.registry key.

Interface for generic power management–enabled devices, associated with Interface for generic power management–enabled devices, associated with thethe

GUID {A32942B7-920C-486b-B0E6-92A702A99B35}.GUID {A32942B7-920C-486b-B0E6-92A702A99B35}.

Power Management for a Driver

Page 62: Device Driver in WinCE 6.0 R2

Power Management for a Driver

Page 63: Device Driver in WinCE 6.0 R2

Question TimeQuestion Time

Can you answer this..Can you answer this..

1.1. What are the 2 ways, a Stream driver can advertise that it is Power What are the 2 ways, a Stream driver can advertise that it is Power Management enabled?Management enabled?

Page 64: Device Driver in WinCE 6.0 R2

Thank You Thank You