toaster sample package eliyas yakub windows nt ® ddk support engineer developer support microsoft...

43
Toaster Sample Package Toaster Sample Package Eliyas Yakub Eliyas Yakub Windows NT Windows NT ® ® DDK Support DDK Support Engineer Engineer Developer Support Developer Support Microsoft Corporation Microsoft Corporation

Upload: clare-cunnington

Post on 29-Mar-2015

242 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Toaster Sample PackageToaster Sample Package

Eliyas YakubEliyas Yakub Windows NTWindows NT®® DDK Support Engineer DDK Support EngineerDeveloper SupportDeveloper SupportMicrosoft CorporationMicrosoft Corporation

Page 2: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation
Page 3: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

AgendaAgenda

Introduce various components of Introduce various components of Toaster Sample PackageToaster Sample Package

Describe how the sample was Describe how the sample was implemented by highlighting implemented by highlighting core componentscore components

Page 4: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Package LayoutPackage Layout

Located under NTDDK\SRC\GENERAL\TOASTERLocated under NTDDK\SRC\GENERAL\TOASTER

Page 5: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Objectives Of The SampleObjectives Of The Sample

De-mystify WindowsDe-mystify Windows®® Driver Model Driver Model Through the use of sample code Through the use of sample code

demonstrate how to develop:demonstrate how to develop: Bus DriverBus Driver Function DriverFunction Driver Filter Drivers (Class & Device)Filter Drivers (Class & Device) Coinstaller DLLCoinstaller DLL Power ManagementPower Management WMIWMI Plug and Play Notification in user-mode and Plug and Play Notification in user-mode and

kernel-modekernel-mode

Page 6: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

What Is Toaster BusWhat Is Toaster Bus

Hypothetical bus for toaster devicesHypothetical bus for toaster devices Simulates hot plug-in, unplug, and Simulates hot plug-in, unplug, and

eject toasters deviceseject toasters devices Toasters support power managementToasters support power management Defines a new “Toaster” device class Defines a new “Toaster” device class

Page 7: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Bus Driver (BusEnum.sys)Bus Driver (BusEnum.sys)

Dual personality driverDual personality driver Use bus.inf and busf.inf to install Use bus.inf and busf.inf to install

through the Add HW Wizardthrough the Add HW Wizard System root enumerates the bus driverSystem root enumerates the bus driver DriverEntry() DriverEntry()

Sets dispatch pointsSets dispatch points AddDevice()AddDevice()

Creates FDO and attaches it to the PDO Creates FDO and attaches it to the PDO provided by the Plug and Play managerprovided by the Plug and Play manager

Page 8: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Bus DriverBus Driver

Registers Registers GUID_TOASTER_BUS_ENUMERATOR_INTERFACGUID_TOASTER_BUS_ENUMERATOR_INTERFACE_CLASSE_CLASS interface interface

Driver typically handles following IRPs:Driver typically handles following IRPs: IRP_MN_FILTER_RESOURCE_REQUIREMENTSIRP_MN_FILTER_RESOURCE_REQUIREMENTS IRP_MN_START_DEVICEIRP_MN_START_DEVICE IRP_MN_QUERY_CAPABILITIESIRP_MN_QUERY_CAPABILITIES IRP_MN_QUERY_PNP_DEVICE_STATEIRP_MN_QUERY_PNP_DEVICE_STATE IRP_MN_QUERY_DEVICE_RELATIONS IRP_MN_QUERY_DEVICE_RELATIONS

(BusRelations)(BusRelations) This is not a complete list. Drivers shouldn’t This is not a complete list. Drivers shouldn’t

make any assumptions about the order of make any assumptions about the order of these IRPsthese IRPs

Page 9: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Plug In A Device Plug In A Device

Page 10: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Simulating Plug-In Of New Simulating Plug-In Of New Toaster DeviceToaster Device Application opens the interface Application opens the interface

registered by BusEnumregistered by BusEnum Application sends Application sends

IOCTL_BUSENUM_PLUGIN_HARDWARE IOCTL_BUSENUM_PLUGIN_HARDWARE Provides Serial Number and HW ID Provides Serial Number and HW ID

of the deviceof the device

Page 11: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Plug-InPlug-In

BusEnum creates a DeviceObject BusEnum creates a DeviceObject (PDO) for the new device(PDO) for the new device

Initializes device extensionInitializes device extension Present = TRUE;Present = TRUE; ReportedMissing = FALSE;ReportedMissing = FALSE;

BusEnum notifies Plug and Play BusEnum notifies Plug and Play manager manager Calls IoInvalidateDeviceRelations() Calls IoInvalidateDeviceRelations()

with BusRelations parameterwith BusRelations parameter

Page 12: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Plug-InPlug-In

Plug and Play manager sends Plug and Play manager sends IRP_MN_QUERY_DEVICE_RELATIONS IRP_MN_QUERY_DEVICE_RELATIONS to BusEnum’s FDOto BusEnum’s FDO

The BusEnum returns a list of The BusEnum returns a list of PDO pointersPDO pointers

Plug and Play manager starts Plug and Play manager starts enumeration after discovering PDO for enumeration after discovering PDO for the new Toaster devicethe new Toaster device

Page 13: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Plug-InPlug-In

Plug and Play manager sends following IRPs to the Plug and Play manager sends following IRPs to the new PDO to learn more about the device:new PDO to learn more about the device: IRP_MN_QUERY_ID (BusQueryDeviceID)IRP_MN_QUERY_ID (BusQueryDeviceID) IRP_MN_QUERY_CAPABILITIESIRP_MN_QUERY_CAPABILITIES IRP_MN_QUERY_DEVICE_TEXT (Description)IRP_MN_QUERY_DEVICE_TEXT (Description) IRP_MN_QUERY_DEVICE_TEXT (Location)IRP_MN_QUERY_DEVICE_TEXT (Location) IRP_MN_QUERY_ID (BusInstanceID)IRP_MN_QUERY_ID (BusInstanceID) IRP_MN_QUERY_ID (BusQueryHardwareIDs)IRP_MN_QUERY_ID (BusQueryHardwareIDs) IRP_MN_QUERY_ID (BusQueryCompatibleIDs)IRP_MN_QUERY_ID (BusQueryCompatibleIDs) IRP_MN_QUERY_RESOURCE_REQUIREMENTSIRP_MN_QUERY_RESOURCE_REQUIREMENTS IRP_MN_QUERY_BUS_INFORMATIONIRP_MN_QUERY_BUS_INFORMATION IRP_MN_QUERY_RESOURCESIRP_MN_QUERY_RESOURCES

Page 14: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Plug-InPlug-In

Plug and Play loads the toaster.sys Plug and Play loads the toaster.sys function driverfunction driver Matches the Device ID in toaster.infMatches the Device ID in toaster.inf Loads the function Driver (toaster.sys)Loads the function Driver (toaster.sys) Calls AddDevice in toaster.sysCalls AddDevice in toaster.sys Function driver handles following Plug and Function driver handles following Plug and

Play IRPs:Play IRPs: IRP_MN_FILTER_RESOURCE_REQUIREMENTSIRP_MN_FILTER_RESOURCE_REQUIREMENTS IRP_MN_QUERY_INTERFACEIRP_MN_QUERY_INTERFACE IRP_MN_START_DEVICEIRP_MN_START_DEVICE IRP_MN_QUERY_CAPABILITIESIRP_MN_QUERY_CAPABILITIES IRP_MN_QUERY_PNP_DEVICE_STATEIRP_MN_QUERY_PNP_DEVICE_STATE

Page 15: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Notify.exeNotify.exe

FDOFDOPDOPDO

Toast.exeToast.exe

FDOFDO

BusEnum.sysBusEnum.sys

Toaster.sysToaster.sys

Plug and Play Plug and Play ManagerManager

PDOPDO. . . . . . . . .. . . . . . . . .PDOPDO FDOFDO

Diagram Of Single Toaster DeviceDiagram Of Single Toaster Device

Page 16: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Notify.exeNotify.exe

FDOFDOPDOPDO PDOPDO

FDOFDO

Toast.exeToast.exe

PDOPDO

FDOFDO

BusEnum.sysBusEnum.sys

Toaster.sysToaster.sys

Plug and Play ManagerPlug and Play Manager PDOPDO. . . . . . . . .. . . . . . . . . PDOPDO

Diagram Of Two Toaster DevicesDiagram Of Two Toaster Devices

Page 17: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Unplug A DeviceUnplug A Device

Application sends an Application sends an IOCTL_BUSENUM_UNPLUG_IOCTL_BUSENUM_UNPLUG_HARDWARE with the Serial NumberHARDWARE with the Serial Number

BusEnum marks the device as BusEnum marks the device as not presentnot present Present = FALSE;Present = FALSE;

BusEnum informs Plug and Play manager BusEnum informs Plug and Play manager by calling IoInvalidateDeviceRelations()by calling IoInvalidateDeviceRelations()

Plug and Play manager sends Plug and Play manager sends IRP_MN_QUERY_DEVICE_RELATIONSIRP_MN_QUERY_DEVICE_RELATIONS

BusEnum does not include the unplugged BusEnum does not include the unplugged PDO in list of PDOsPDO in list of PDOs

Page 18: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

UnPlugUnPlug

Set the ReportedMissing to TRUESet the ReportedMissing to TRUE ReportedMissing = TRUE;ReportedMissing = TRUE;

The Plug and Play manager sends:The Plug and Play manager sends: IRP_MN_SURPRISE_REMOVALIRP_MN_SURPRISE_REMOVAL IRP_MN_REMOVE_DEVICEIRP_MN_REMOVE_DEVICE

Function driver detaches and deletes Function driver detaches and deletes its device objectits device object

Busenum deletes the PDO after Busenum deletes the PDO after receiving IRP_MN_REMOVE_DEVICEreceiving IRP_MN_REMOVE_DEVICE

Page 19: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Eject A DeviceEject A Device

User Application sends User Application sends IOCTL_BUSENUM_EJECT_HARDWIOCTL_BUSENUM_EJECT_HARDWARE with the Serial NumberARE with the Serial Number

BusEnum calls BusEnum calls IoRequestDeviceEject IoRequestDeviceEject

The Plug and Play manager sends: The Plug and Play manager sends: IRP_MN_QUERY_REMOVALIRP_MN_QUERY_REMOVAL IRP_MN_REMOVE_DEVICEIRP_MN_REMOVE_DEVICE

Page 20: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

EjectEject

Plug and Play manager sends Plug and Play manager sends IRP_MN_EJECT IRP_MN_EJECT

set the Present = FALSE;set the Present = FALSE; IRP_MN_QUERY_DEVICE_RELATIONIRP_MN_QUERY_DEVICE_RELATION

S (BusRelations)S (BusRelations)

set the ReportedMissing = TRUE;set the ReportedMissing = TRUE; IRP_MN_REMOVE_DEVICEIRP_MN_REMOVE_DEVICE

Page 21: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Device Plug And Play StateDevice Plug And Play Statetypedef enum _DEVICE_PNP_STATE {typedef enum _DEVICE_PNP_STATE {

NotStarted = 0,NotStarted = 0, // Not started yet// Not started yet

Started, Started, // Device has received the START_DEVICE IRP// Device has received the START_DEVICE IRP

StopPending,StopPending, // Device has received the QUERY_STOP IRP// Device has received the QUERY_STOP IRP

Stopped, Stopped, // Device has received the STOP_DEVICE IRP// Device has received the STOP_DEVICE IRP

RemovePending, RemovePending, // Device has received the QUERY_REMOVE IRP// Device has received the QUERY_REMOVE IRP

SurpriseRemovePending, // Device has received the SURPRISE_REMOVE IRPSurpriseRemovePending, // Device has received the SURPRISE_REMOVE IRP

Deleted Deleted // Device has received the REMOVE_DEVICE IRP// Device has received the REMOVE_DEVICE IRP

} DEVICE_PNP_STATE;} DEVICE_PNP_STATE;

Notice the use of following macros:Notice the use of following macros: INITIALIZE_PNP_STATEINITIALIZE_PNP_STATE SET_NEW_PNP_STATESET_NEW_PNP_STATE RESTORE_PREVIOUS_PNP_STATERESTORE_PREVIOUS_PNP_STATE

Page 22: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

PDO DeletionPDO Deletion

Bus drivers must delete the PDO only after Bus drivers must delete the PDO only after device is physically removeddevice is physically removed Surprise removalSurprise removal

After receiving IRP_MN_REMOVE_DEVICEAfter receiving IRP_MN_REMOVE_DEVICE

EjectingEjecting After receiving the second IRP_MN_REMOVE_DEVICE After receiving the second IRP_MN_REMOVE_DEVICE During ejection Plug and Play manager sends During ejection Plug and Play manager sends

QUERY_REMOVE, REMOVE, EJECT QUERY_REMOVE, REMOVE, EJECT QUERY_DEVICE_RELATIONS, REMOVEQUERY_DEVICE_RELATIONS, REMOVE

Second IRP_MN_REMOVE_DEVICE is sent in Second IRP_MN_REMOVE_DEVICE is sent in response to missing PDO returned with response to missing PDO returned with QUERY_DEVICE_RELATIONS IRP QUERY_DEVICE_RELATIONS IRP

Page 23: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

PDO DeletionPDO Deletion

Delete all children PDOs when bus is Delete all children PDOs when bus is removed or disabledremoved or disabled EXCEPTION: Do NOT delete PDOs in the EXCEPTION: Do NOT delete PDOs in the

process of being surprised removedprocess of being surprised removed Do NOT delete PDO when Do NOT delete PDO when

device is disableddevice is disabled BUS Driver will receive QUERY_REMOVE / BUS Driver will receive QUERY_REMOVE /

REMOVE IRPs when device is disabledREMOVE IRPs when device is disabled Do not delete the PDO because the device Do not delete the PDO because the device

is still presentis still present

Page 24: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Function Driver (toaster.sys)Function Driver (toaster.sys)

Demonstrates:Demonstrates: Plug and Play handlingPlug and Play handling Power management by acting as a Power management by acting as a

power policy owner of the devicepower policy owner of the device WMIWMI IRP Queuing, Cancel and IRP Queuing, Cancel and

Cleanup techniquesCleanup techniques Demonstrates the use of Demonstrates the use of

IoInValidateDeviceState() to hide IoInValidateDeviceState() to hide your deviceyour device

Page 25: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

INF FilesINF Files

Bus.inf: For the bus driverBus.inf: For the bus driver Toaster.inf: For the function driverToaster.inf: For the function driver Busf.inf: Bus driver with upper and Busf.inf: Bus driver with upper and

lower device filterslower device filters Toasterf.inf: Function driver with Toasterf.inf: Function driver with

lower and upper device and lower and upper device and class filtersclass filters

Toastco.inf: Installs function driver Toastco.inf: Installs function driver and a coinstaller dlland a coinstaller dll

Filter.inf: Installs a device upper Filter.inf: Installs a device upper filter for the toaster device using filter for the toaster device using Update DriverUpdate Driver

Page 26: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Filter DriversFilter Drivers

A single source file is used to generate A single source file is used to generate six different filter drivers:six different filter drivers: Devupper.sysDevupper.sys Devlower.sysDevlower.sys Clasupper.sysClasupper.sys Claslower.sysClaslower.sys BFdoLwr.sysBFdoLwr.sys BFdoUpr.sysBFdoUpr.sys

To load all the filters you should use busf.inf To load all the filters you should use busf.inf to install the enumerator and use toasterf.inf to install the enumerator and use toasterf.inf to install device and class filtersto install device and class filters

Page 27: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Device Filters InstallationDevice Filters Installation

Use DDInstall.HW sections in the INF file to Use DDInstall.HW sections in the INF file to install filtersinstall filters [Toaster_Device.NT.HW.AddReg][Toaster_Device.NT.HW.AddReg] HKR,,"UpperFilters",0x00010000,"devupper"HKR,,"UpperFilters",0x00010000,"devupper" HKR,,"LowerFilters",0x00010000,"devlower"HKR,,"LowerFilters",0x00010000,"devlower"

These sections adds above registry values These sections adds above registry values under the device hardware keyunder the device hardware key HKLM\System\CurrentControlSet\Enum\MsToaster\HKLM\System\CurrentControlSet\Enum\MsToaster\

1&1a590e2c&1&011&1a590e2c&1&01 UpperFilters:REG_MULTI_SZ:DevupperUpperFilters:REG_MULTI_SZ:Devupper LowerFilters:REG_MULTI_SZ:DevlowerLowerFilters:REG_MULTI_SZ:Devlower

Page 28: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Enum.exeEnum.exe

FDOFDOPDOPDO

Toast.exeToast.exe

FiDOFiDO

BusEnum.sysBusEnum.sys

Toaster.sysToaster.sys

Plug and Play ManagerPlug and Play Manager

FDOFDO

FiDOFiDO

DevUpper.sysDevUpper.sys

DevLower.sysDevLower.sys

PDOPDOPDOPDOFDOFDO

Toaster Stack Toaster Stack With Device With Device FiltersFilters

Page 29: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Class FiltersClass Filters

Class filters are specified adding Class filters are specified adding UpperFilters or LowerFilters name UpperFilters or LowerFilters name value under the class keyvalue under the class keyHKLM\System\CurrentControlSet\HKLM\System\CurrentControlSet\

Control\Class\{B85B7C50-6A01-11d2-Control\Class\{B85B7C50-6A01-11d2-B841-00C04FAD5171}B841-00C04FAD5171}UpperFilters:REG_MULTI_SZ:ClasupperUpperFilters:REG_MULTI_SZ:Clasupper

LowerFilters:REG_MULTI_SZ:ClaslowerLowerFilters:REG_MULTI_SZ:Claslower

Page 30: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Enum.exeEnum.exe

FDOFDOPDOPDO PDOPDO

Toast.exeToast.exe

FiDOFiDO FiDOFiDO

BusEnum.sysBusEnum.sys

Toaster.sysToaster.sys

Plug and Play ManagerPlug and Play Manager

FDOFDO FDOFDO

FiDOFiDO FiDOFiDO

DevUpper.sysDevUpper.sys

DevLower.sysDevLower.sys FiDOFiDO FiDOFiDO

FiDOFiDO FiDOFiDO

ClasLower.sysClasLower.sys

ClasUpper.sysClasUpper.sys

PDOPDOPDOPDOFDOFDO

Page 31: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Co-InstallerCo-Installer

Loaded whenever a toaster device Loaded whenever a toaster device is installed or removedis installed or removed

Shows how to process custom Shows how to process custom sections in an INF filesections in an INF file

Shows how to create a friendly nameShows how to create a friendly name

Page 32: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Notify ApplicationNotify Application

Simulates plug in, unplug, and Simulates plug in, unplug, and ejection of devicesejection of devices

Demonstrates how applications Demonstrates how applications monitor and process user-mode monitor and process user-mode Plug and Play notificationsPlug and Play notifications

Combines the functionality of Combines the functionality of Enum.exe and toast.exe Enum.exe and toast.exe

Page 33: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Notify ApplicationNotify Application

Register for Interface Register for Interface Change NotificationChange Notification

DEV_BROADCAST_DEVICEINTERFACE filter;DEV_BROADCAST_DEVICEINTERFACE filter;

filter.dbcc_size = sizeof(filter);filter.dbcc_size = sizeof(filter);filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

filter.dbcc_classguid = InterfaceGuid;filter.dbcc_classguid = InterfaceGuid;

hInterfaceNotification = RegisterDeviceNotification(hWnd, hInterfaceNotification = RegisterDeviceNotification(hWnd, &filter, 0);&filter, 0);

Handles Plug and Play notificationsHandles Plug and Play notifications DBT_DEVICEARRIVALDBT_DEVICEARRIVAL DBT_DEVICEREMOVECOMPLETEDBT_DEVICEREMOVECOMPLETE

Page 34: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Notify ApplicationNotify Application

Open a toster device and register Open a toster device and register another notification on the file handleanother notification on the file handle filter.dbch_size = sizeof(filter);filter.dbch_size = sizeof(filter);

filter.dbch_devicetype = DBT_DEVTYP_HANDLE;filter.dbch_devicetype = DBT_DEVTYP_HANDLE; filter.dbch_handle = DeviceInfo[NumDevices].hDevice;filter.dbch_handle = DeviceInfo[NumDevices].hDevice; DeviceInfo[NumDevices].hHandleNotification = DeviceInfo[NumDevices].hHandleNotification =

RegisterDeviceNotification(hWnd, RegisterDeviceNotification(hWnd, &filter, 0);&filter, 0);

Handle Device Change NotificationHandle Device Change Notification DBT_DEVICEQUERYREMOVEDBT_DEVICEQUERYREMOVE DBT_DEVICEREMOVECOMPLETEDBT_DEVICEREMOVECOMPLETE DBT_DEVICEREMOVEPENDINGDBT_DEVICEREMOVEPENDING DBT_DEVICEQUERYREMOVEFAILEDDBT_DEVICEQUERYREMOVEFAILED

Page 35: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

ToastMonToastMon

Demonstrates how drivers monitor Demonstrates how drivers monitor Plug and Play events of arbitrary Plug and Play events of arbitrary devicesdevices Example: monitors a non-Plug and Example: monitors a non-Plug and

Play serial devicePlay serial device Uses kernel-mode Plug and Play Uses kernel-mode Plug and Play

notificationnotification Use toatmon.inf to install the driver Use toatmon.inf to install the driver

through root enumerationthrough root enumeration

Page 36: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

ToastMonToastMon

Register for interface arrival Register for interface arrival notification in AddDevicenotification in AddDevice

status = IoRegisterPlugPlayNotification ( status = IoRegisterPlugPlayNotification ( EventCategoryDeviceInterfaceChange, EventCategoryDeviceInterfaceChange,

PNPNOTIFY_DEVICE_INTERFACE_INCLUDE_EXISTING_PNPNOTIFY_DEVICE_INTERFACE_INCLUDE_EXISTING_ INTERFACES, INTERFACES,

(PVOID)&GUID_TOASTER_DEVICE_INTERFACE_CLASS(PVOID)&GUID_TOASTER_DEVICE_INTERFACE_CLASSDriverObject, DriverObject,

(PDRIVER_NOTIFICATION_CALLBACK_ROUTINE) (PDRIVER_NOTIFICATION_CALLBACK_ROUTINE) ToastMon_PnpNotifyInterfaceChange, ToastMon_PnpNotifyInterfaceChange,(PVOID)deviceExtension,(PVOID)deviceExtension,&deviceExtension->NotificationHandle);&deviceExtension->NotificationHandle);

Open the device in response to Open the device in response to GUID_TOASTER_DEVICE_INTERFACE_CLASSGUID_TOASTER_DEVICE_INTERFACE_CLASS notification callbacknotification callback

Page 37: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

ToastMon ToastMon

Careful coding required in Careful coding required in notification callbacknotification callback Do NOT open interface that can cause Do NOT open interface that can cause

Plug and Play eventPlug and Play event Example: SWENUMExample: SWENUM Doing so can cause deadlockDoing so can cause deadlock

Page 38: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

ToastMonToastMon

Register for Device change notification on Register for Device change notification on the FileObjectthe FileObject

IoRegisterPlugPlayNotification ( IoRegisterPlugPlayNotification ( EventCategoryTargetDeviceChange,EventCategoryTargetDeviceChange,

0,0,(PVOID)List->FileObject,(PVOID)List->FileObject,List->DeviceExtension->List->DeviceExtension-> DeviceObject->DriverObject, DeviceObject->DriverObject,

(PDRIVER_NOTIFICATION_CALLBACK_ROUTINE)(PDRIVER_NOTIFICATION_CALLBACK_ROUTINE)ToastMon_PnpNotifyDeviceChange, ToastMon_PnpNotifyDeviceChange, (PVOID)List, (PVOID)List,

&List->NotificationHandle);&List->NotificationHandle);

Handle Notification Callback Events For:Handle Notification Callback Events For:GUID_TARGET_DEVICE_QUERY_REMOVEGUID_TARGET_DEVICE_QUERY_REMOVEGUID_TARGET_DEVICE_REMOVE_COMPLETEGUID_TARGET_DEVICE_REMOVE_COMPLETEGUID_TARGET_DEVICE_REMOVE_CANCELLEDGUID_TARGET_DEVICE_REMOVE_CANCELLED

Page 39: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Setup Class GUIDSetup Class GUID

Setup class GUID is specified in Setup class GUID is specified in the INF filethe INF file

[Version][Version]Signature="$WINDOWS NT$“Signature="$WINDOWS NT$“Class=TOASTERClass=TOASTERClassGuid={B85B7C50-6A01-11d2-ClassGuid={B85B7C50-6A01-11d2-B841-00C04FAD5171}B841-00C04FAD5171}

All the system defined GUIDs are listed All the system defined GUIDs are listed in in DEVGUID.HDEVGUID.H

Page 40: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Interface Class GUIDInterface Class GUID

Interface GUIDs are used to interact Interface GUIDs are used to interact with the driverwith the driver Registered in AddDeviceRegistered in AddDevice Enabled during StartEnabled during Start Disabled during RemoveDisabled during Remove

Are defined in device specific header files Are defined in device specific header files under the NTDDK\INC directoryunder the NTDDK\INC directory Examples:Examples:

ntdd8042.h, ntddbeep.h, ntddcdrm.h, ntdddisk.h, ntdd8042.h, ntddbeep.h, ntddcdrm.h, ntdddisk.h, ntddft.h, ntddft2.h, ntddkbd.h, ntddmou.h, ntddft.h, ntddft2.h, ntddkbd.h, ntddmou.h, ntddndis.h, ntddpar.h, ntddpcm.h, ntddscsi.h, ntddndis.h, ntddpar.h, ntddpcm.h, ntddscsi.h, ntddser.h, ntddstor.h, ntddtdi.h, ntddvol.hntddser.h, ntddstor.h, ntddtdi.h, ntddvol.h

Page 41: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

Call To ActionCall To Action

Study the toaster sample to learn how Study the toaster sample to learn how all the components of a device all the components of a device stack fit togetherstack fit together Sample complements the Sample complements the

DDK documentation DDK documentation Contains extensive internal documentation Contains extensive internal documentation

E-mail me E-mail me (([email protected]@microsoft.com)) with with questions regarding the samplequestions regarding the sample

Page 42: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation

ErrataErrata

In Line 63, the driver name should be In Line 63, the driver name should be toastmon.sys instead of genport.systoastmon.sys instead of genport.sys

In the toaster.c of function driver In the toaster.c of function driver the line the line

IoSetCancelRoutine(nextIrp, Null) IoSetCancelRoutine(nextIrp, Null) should be replace by should be replace by

If(IoSetCancelRoutine(nextIrp, Null) )If(IoSetCancelRoutine(nextIrp, Null) ) {{

KeReleaseSpinlock();KeReleaseSpinlock(); Continue;Continue;

} }

Page 43: Toaster Sample Package Eliyas Yakub Windows NT ® DDK Support Engineer Developer Support Microsoft Corporation