2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/device.c
5 * PURPOSE: Device Object Management, including Notifications and Queues.
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Filip Navara (navaraf@reactos.org)
8 * Hervé Poussineau (hpoussin@reactos.org)
11 /* INCLUDES *******************************************************************/
17 /* GLOBALS ********************************************************************/
19 ULONG IopDeviceObjectNumber
= 0;
20 LIST_ENTRY ShutdownListHead
, LastChanceShutdownListHead
;
21 KSPIN_LOCK ShutdownListLock
;
22 extern LIST_ENTRY IopDiskFileSystemQueueHead
;
23 extern LIST_ENTRY IopCdRomFileSystemQueueHead
;
24 extern LIST_ENTRY IopTapeFileSystemQueueHead
;
25 extern ERESOURCE IopDatabaseResource
;
27 /* PRIVATE FUNCTIONS **********************************************************/
31 IopReadyDeviceObjects(IN PDRIVER_OBJECT Driver
)
33 PDEVICE_OBJECT DeviceObject
;
36 /* Set the driver as initialized */
37 Driver
->Flags
|= DRVO_INITIALIZED
;
38 DeviceObject
= Driver
->DeviceObject
;
41 /* Set every device as initialized too */
42 DeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
43 DeviceObject
= DeviceObject
->NextDevice
;
49 IopDeleteDevice(IN PVOID ObjectBody
)
51 PDEVICE_OBJECT DeviceObject
= ObjectBody
;
52 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
55 /* Cleanup and free the device node */
57 IopFreeDeviceNode(DeviceNode
);
59 /* Dereference the driver object, referenced in IoCreateDevice */
60 if (DeviceObject
->DriverObject
)
61 ObDereferenceObject(DeviceObject
->DriverObject
);
67 IopAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
68 IN PDEVICE_OBJECT TargetDevice
,
69 OUT PDEVICE_OBJECT
*AttachedToDeviceObject OPTIONAL
)
71 PDEVICE_OBJECT AttachedDevice
;
72 PEXTENDED_DEVOBJ_EXTENSION SourceDeviceExtension
;
74 /* Get the Attached Device and source extension */
75 AttachedDevice
= IoGetAttachedDevice(TargetDevice
);
76 SourceDeviceExtension
= IoGetDevObjExtension(SourceDevice
);
77 ASSERT(SourceDeviceExtension
->AttachedTo
== NULL
);
79 /* Make sure that it's in a correct state */
80 if ((AttachedDevice
->Flags
& DO_DEVICE_INITIALIZING
) ||
81 (IoGetDevObjExtension(AttachedDevice
)->ExtensionFlags
&
85 DOE_REMOVE_PROCESSED
)))
87 /* Device was unloading or being removed */
88 AttachedDevice
= NULL
;
92 /* Update atached device fields */
93 AttachedDevice
->AttachedDevice
= SourceDevice
;
94 AttachedDevice
->Spare1
++;
96 /* Update the source with the attached data */
97 SourceDevice
->StackSize
= AttachedDevice
->StackSize
+ 1;
98 SourceDevice
->AlignmentRequirement
= AttachedDevice
->
100 SourceDevice
->SectorSize
= AttachedDevice
->SectorSize
;
102 /* Check for pending start flag */
103 if (IoGetDevObjExtension(AttachedDevice
)->ExtensionFlags
&
107 IoGetDevObjExtension(SourceDevice
)->ExtensionFlags
|=
111 /* Set the attachment in the device extension */
112 SourceDeviceExtension
->AttachedTo
= AttachedDevice
;
115 /* Return the attached device */
116 if (AttachedToDeviceObject
) *AttachedToDeviceObject
= AttachedDevice
;
117 return AttachedDevice
;
122 IoShutdownPnpDevices(VOID
)
124 /* This routine is only used by Driver Verifier to validate shutdown */
130 IoShutdownSystem(IN ULONG Phase
)
132 PLIST_ENTRY ListEntry
;
133 PDEVICE_OBJECT DeviceObject
;
134 PSHUTDOWN_ENTRY ShutdownEntry
;
135 IO_STATUS_BLOCK StatusBlock
;
140 /* Initialize an event to wait on */
141 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
147 IoShutdownPnpDevices();
149 /* Loop first-chance shutdown notifications */
150 ListEntry
= ExInterlockedRemoveHeadList(&ShutdownListHead
,
154 /* Get the shutdown entry */
155 ShutdownEntry
= CONTAINING_RECORD(ListEntry
,
159 /* Get the attached device */
160 DeviceObject
= IoGetAttachedDevice(ShutdownEntry
->DeviceObject
);
162 /* Build the shutdown IRP and call the driver */
163 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN
,
172 Status
= IoCallDriver(DeviceObject
, Irp
);
173 if (Status
== STATUS_PENDING
)
175 /* Wait on the driver */
176 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
180 /* Remove the flag */
181 ShutdownEntry
->DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
183 /* Get rid of our reference to it */
184 ObDereferenceObject(ShutdownEntry
->DeviceObject
);
186 /* Free the shutdown entry and reset the event */
187 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
188 KeClearEvent(&Event
);
190 /* Go to the next entry */
191 ListEntry
= ExInterlockedRemoveHeadList(&ShutdownListHead
,
197 /* Acquire resource forever */
198 ExAcquireResourceExclusiveLite(&IopDatabaseResource
, TRUE
);
200 /* Shutdown disk file systems */
201 IopShutdownBaseFileSystems(&IopDiskFileSystemQueueHead
);
203 /* Shutdown cdrom file systems */
204 IopShutdownBaseFileSystems(&IopCdRomFileSystemQueueHead
);
206 /* Shutdown tape filesystems */
207 IopShutdownBaseFileSystems(&IopTapeFileSystemQueueHead
);
209 /* Loop last-chance shutdown notifications */
210 ListEntry
= ExInterlockedRemoveHeadList(&LastChanceShutdownListHead
,
214 /* Get the shutdown entry */
215 ShutdownEntry
= CONTAINING_RECORD(ListEntry
,
219 /* Get the attached device */
220 DeviceObject
= IoGetAttachedDevice(ShutdownEntry
->DeviceObject
);
222 /* Build the shutdown IRP and call the driver */
223 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN
,
232 Status
= IoCallDriver(DeviceObject
, Irp
);
233 if (Status
== STATUS_PENDING
)
235 /* Wait on the driver */
236 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
240 /* Remove the flag */
241 ShutdownEntry
->DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
243 /* Get rid of our reference to it */
244 ObDereferenceObject(ShutdownEntry
->DeviceObject
);
246 /* Free the shutdown entry and reset the event */
247 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
248 KeClearEvent(&Event
);
250 /* Go to the next entry */
251 ListEntry
= ExInterlockedRemoveHeadList(&LastChanceShutdownListHead
,
260 IopGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
261 IN ACCESS_MASK DesiredAccess
,
262 OUT PFILE_OBJECT
*FileObject
,
263 OUT PDEVICE_OBJECT
*DeviceObject
,
266 OBJECT_ATTRIBUTES ObjectAttributes
;
267 IO_STATUS_BLOCK StatusBlock
;
268 PFILE_OBJECT LocalFileObject
;
272 /* Open the Device */
273 InitializeObjectAttributes(&ObjectAttributes
,
278 Status
= ZwOpenFile(&FileHandle
,
283 FILE_NON_DIRECTORY_FILE
| AttachFlag
);
284 if (!NT_SUCCESS(Status
)) return Status
;
286 /* Get File Object */
287 Status
= ObReferenceObjectByHandle(FileHandle
,
291 (PVOID
*)&LocalFileObject
,
293 if (NT_SUCCESS(Status
))
295 /* Return the requested data */
296 *DeviceObject
= IoGetRelatedDeviceObject(LocalFileObject
);
297 *FileObject
= LocalFileObject
;
300 /* Close the handle */
308 IopGetLowestDevice(IN PDEVICE_OBJECT DeviceObject
)
310 PDEVICE_OBJECT LowestDevice
;
311 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
313 /* Get the current device and its extension */
314 LowestDevice
= DeviceObject
;
315 DeviceExtension
= IoGetDevObjExtension(LowestDevice
);
317 /* Keep looping as long as we're attached */
318 while (DeviceExtension
->AttachedTo
)
320 /* Get the lowest device and its extension */
321 LowestDevice
= DeviceExtension
->AttachedTo
;
322 DeviceExtension
= IoGetDevObjExtension(LowestDevice
);
325 /* Return the lowest device */
331 IopEditDeviceList(IN PDRIVER_OBJECT DriverObject
,
332 IN PDEVICE_OBJECT DeviceObject
,
333 IN IOP_DEVICE_LIST_OPERATION Type
)
335 PDEVICE_OBJECT Previous
;
337 /* Check the type of operation */
338 if (Type
== IopRemove
)
340 /* Get the current device and check if it's the current one */
341 Previous
= DeviceObject
->DriverObject
->DeviceObject
;
342 if (Previous
== DeviceObject
)
344 /* It is, simply unlink this one directly */
345 DeviceObject
->DriverObject
->DeviceObject
=
346 DeviceObject
->NextDevice
;
350 /* It's not, so loop until we find the device */
351 while (Previous
->NextDevice
!= DeviceObject
)
353 /* Not this one, keep moving */
354 if (!Previous
->NextDevice
)
356 DPRINT1("Failed to remove PDO %p on driver %wZ (not found)\n", DeviceObject
, &DeviceObject
->DriverObject
->DriverName
);
359 Previous
= Previous
->NextDevice
;
362 /* We found it, now unlink us */
363 Previous
->NextDevice
= DeviceObject
->NextDevice
;
368 /* Link the device object and the driver object */
369 DeviceObject
->NextDevice
= DriverObject
->DeviceObject
;
370 DriverObject
->DeviceObject
= DeviceObject
;
376 IopUnloadDevice(IN PDEVICE_OBJECT DeviceObject
)
378 PDRIVER_OBJECT DriverObject
= DeviceObject
->DriverObject
;
379 PEXTENDED_DEVOBJ_EXTENSION ThisExtension
= IoGetDevObjExtension(DeviceObject
);
381 /* Check if deletion is pending */
382 if (ThisExtension
->ExtensionFlags
& DOE_DELETE_PENDING
)
384 if (DeviceObject
->AttachedDevice
)
386 DPRINT("Device object is in the middle of a device stack\n");
390 if (DeviceObject
->ReferenceCount
)
392 DPRINT("Device object still has %d references\n", DeviceObject
->ReferenceCount
);
396 /* Check if we have a Security Descriptor */
397 if (DeviceObject
->SecurityDescriptor
)
400 ExFreePoolWithTag(DeviceObject
->SecurityDescriptor
, TAG_SD
);
403 /* Remove the device from the list */
404 IopEditDeviceList(DeviceObject
->DriverObject
, DeviceObject
, IopRemove
);
406 /* Dereference the keep-alive */
407 ObDereferenceObject(DeviceObject
);
410 /* We can't unload a non-PnP driver here */
411 if (DriverObject
->Flags
& DRVO_LEGACY_DRIVER
)
413 DPRINT("Not a PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
417 /* Return if we've already called unload (maybe we're in it?) */
418 if (DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) return;
420 /* We can't unload unless there's an unload handler */
421 if (!DriverObject
->DriverUnload
)
423 DPRINT1("No DriverUnload function on PnP driver! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
427 /* Bail if there are still devices present */
428 if (DriverObject
->DeviceObject
)
430 DPRINT("Devices still present! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
434 DPRINT1("Unloading driver '%wZ' (automatic)\n", &DriverObject
->DriverName
);
436 /* Set the unload invoked flag */
437 DriverObject
->Flags
|= DRVO_UNLOAD_INVOKED
;
440 DriverObject
->DriverUnload(DriverObject
);
442 /* Make object temporary so it can be deleted */
443 ObMakeTemporaryObject(DriverObject
);
448 IopDereferenceDeviceObject(IN PDEVICE_OBJECT DeviceObject
,
449 IN BOOLEAN ForceUnload
)
452 ASSERT(DeviceObject
->ReferenceCount
);
454 /* Dereference the device */
455 InterlockedDecrement(&DeviceObject
->ReferenceCount
);
458 * Check if we can unload it and it's safe to unload (or if we're forcing
459 * an unload, which is OK too).
461 ASSERT(!ForceUnload
);
462 if (!(DeviceObject
->ReferenceCount
) &&
463 (IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
& DOE_DELETE_PENDING
))
466 IopUnloadDevice(DeviceObject
);
472 IopStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
473 IN BOOLEAN Cancelable
,
476 PKDEVICE_QUEUE_ENTRY Entry
;
480 /* Acquire the cancel lock if this is cancelable */
481 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
483 /* Clear the current IRP */
484 DeviceObject
->CurrentIrp
= NULL
;
486 /* Remove an entry from the queue */
487 Entry
= KeRemoveByKeyDeviceQueue(&DeviceObject
->DeviceQueue
, Key
);
490 /* Get the IRP and set it */
491 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
492 DeviceObject
->CurrentIrp
= Irp
;
494 /* Check if this is a cancelable packet */
497 /* Check if the caller requested no cancellation */
498 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
501 /* He did, so remove the cancel routine */
502 Irp
->CancelRoutine
= NULL
;
505 /* Release the cancel lock */
506 IoReleaseCancelSpinLock(OldIrql
);
509 /* Call the Start I/O Routine */
510 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
514 /* Otherwise, release the cancel lock if we had acquired it */
515 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
521 IopStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
522 IN BOOLEAN Cancelable
)
524 PKDEVICE_QUEUE_ENTRY Entry
;
528 /* Acquire the cancel lock if this is cancelable */
529 if (Cancelable
) IoAcquireCancelSpinLock(&OldIrql
);
531 /* Clear the current IRP */
532 DeviceObject
->CurrentIrp
= NULL
;
534 /* Remove an entry from the queue */
535 Entry
= KeRemoveDeviceQueue(&DeviceObject
->DeviceQueue
);
538 /* Get the IRP and set it */
539 Irp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
540 DeviceObject
->CurrentIrp
= Irp
;
542 /* Check if this is a cancelable packet */
545 /* Check if the caller requested no cancellation */
546 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
549 /* He did, so remove the cancel routine */
550 Irp
->CancelRoutine
= NULL
;
553 /* Release the cancel lock */
554 IoReleaseCancelSpinLock(OldIrql
);
557 /* Call the Start I/O Routine */
558 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
562 /* Otherwise, release the cancel lock if we had acquired it */
563 if (Cancelable
) IoReleaseCancelSpinLock(OldIrql
);
569 IopStartNextPacketByKeyEx(IN PDEVICE_OBJECT DeviceObject
,
573 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
574 ULONG CurrentKey
= Key
;
575 ULONG CurrentFlags
= Flags
;
577 /* Get the device extension and start the packet loop */
578 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
581 /* Increase the count */
582 if (InterlockedIncrement(&DeviceExtension
->StartIoCount
) > 1)
585 * We've already called the routine once...
586 * All we have to do is save the key and add the new flags
588 DeviceExtension
->StartIoFlags
|= CurrentFlags
;
589 DeviceExtension
->StartIoKey
= CurrentKey
;
593 /* Mask out the current packet flags and key */
594 DeviceExtension
->StartIoFlags
&= ~(DOE_SIO_WITH_KEY
|
597 DeviceExtension
->StartIoKey
= 0;
599 /* Check if this is a packet start with key */
600 if (Flags
& DOE_SIO_WITH_KEY
)
602 /* Start the packet with a key */
603 IopStartNextPacketByKey(DeviceObject
,
604 (Flags
& DOE_SIO_CANCELABLE
) ?
608 else if (Flags
& DOE_SIO_NO_KEY
)
610 /* Start the packet */
611 IopStartNextPacket(DeviceObject
,
612 (Flags
& DOE_SIO_CANCELABLE
) ?
617 /* Decrease the Start I/O count and check if it's 0 now */
618 if (!InterlockedDecrement(&DeviceExtension
->StartIoCount
))
620 /* Get the current active key and flags */
621 CurrentKey
= DeviceExtension
->StartIoKey
;
622 CurrentFlags
= DeviceExtension
->StartIoFlags
& (DOE_SIO_WITH_KEY
|
626 /* Check if we should still loop */
627 if (!(CurrentFlags
& (DOE_SIO_WITH_KEY
| DOE_SIO_NO_KEY
))) break;
631 /* There are still Start I/Os active, so quit this loop */
639 IopGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
640 OUT PDEVICE_NODE
*DeviceNode
)
643 IO_STACK_LOCATION Stack
= {0};
644 PDEVICE_RELATIONS DeviceRelations
;
645 PDEVICE_OBJECT DeviceObject
= NULL
;
649 /* Get DeviceObject related to given FileObject */
650 DeviceObject
= IoGetRelatedDeviceObject(FileObject
);
651 if (!DeviceObject
) return STATUS_NO_SUCH_DEVICE
;
653 /* Define input parameters */
654 Stack
.MajorFunction
= IRP_MJ_PNP
;
655 Stack
.MinorFunction
= IRP_MN_QUERY_DEVICE_RELATIONS
;
656 Stack
.Parameters
.QueryDeviceRelations
.Type
= TargetDeviceRelation
;
657 Stack
.FileObject
= FileObject
;
659 /* Call the driver to query all relations (IRP_MJ_PNP) */
660 Status
= IopSynchronousCall(DeviceObject
,
662 (PVOID
)&DeviceRelations
);
663 if (!NT_SUCCESS(Status
)) return Status
;
665 /* Make sure it's not NULL and contains only one object */
666 ASSERT(DeviceRelations
);
667 ASSERT(DeviceRelations
->Count
== 1);
669 /* Finally get the device node */
670 *DeviceNode
= IopGetDeviceNode(DeviceRelations
->Objects
[0]);
671 if (!*DeviceNode
) Status
= STATUS_NO_SUCH_DEVICE
;
673 /* Free the DEVICE_RELATIONS structure, it's not needed anymore */
674 ExFreePool(DeviceRelations
);
679 /* PUBLIC FUNCTIONS ***********************************************************/
684 * Layers a device over the highest device in a device stack.
688 * Device to be attached.
691 * Name of the target device.
694 * Caller storage for the device attached to.
701 IoAttachDevice(PDEVICE_OBJECT SourceDevice
,
702 PUNICODE_STRING TargetDeviceName
,
703 PDEVICE_OBJECT
*AttachedDevice
)
706 PFILE_OBJECT FileObject
= NULL
;
707 PDEVICE_OBJECT TargetDevice
= NULL
;
709 /* Call the helper routine for an attach operation */
710 Status
= IopGetDeviceObjectPointer(TargetDeviceName
,
711 FILE_READ_ATTRIBUTES
,
714 IO_ATTACH_DEVICE_API
);
715 if (!NT_SUCCESS(Status
)) return Status
;
717 /* Attach the device */
718 Status
= IoAttachDeviceToDeviceStackSafe(SourceDevice
,
723 ObDereferenceObject(FileObject
);
728 * IoAttachDeviceByPointer
735 IoAttachDeviceByPointer(IN PDEVICE_OBJECT SourceDevice
,
736 IN PDEVICE_OBJECT TargetDevice
)
738 PDEVICE_OBJECT AttachedDevice
;
739 NTSTATUS Status
= STATUS_SUCCESS
;
742 AttachedDevice
= IoAttachDeviceToDeviceStack(SourceDevice
, TargetDevice
);
743 if (!AttachedDevice
) Status
= STATUS_NO_SUCH_DEVICE
;
745 /* Return the status */
754 IoAttachDeviceToDeviceStack(IN PDEVICE_OBJECT SourceDevice
,
755 IN PDEVICE_OBJECT TargetDevice
)
757 /* Attach it safely */
758 return IopAttachDeviceToDeviceStackSafe(SourceDevice
,
768 IoAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice
,
769 IN PDEVICE_OBJECT TargetDevice
,
770 IN OUT PDEVICE_OBJECT
*AttachedToDeviceObject
)
772 /* Call the internal function */
773 if (!IopAttachDeviceToDeviceStackSafe(SourceDevice
,
775 AttachedToDeviceObject
))
778 return STATUS_NO_SUCH_DEVICE
;
782 return STATUS_SUCCESS
;
788 * Allocates memory for and intializes a device object for use for
793 * Driver object passed by IO Manager when the driver was loaded.
795 * DeviceExtensionSize
796 * Number of bytes for the device extension.
799 * Unicode name of device.
802 * Device type of the new device.
804 * DeviceCharacteristics
805 * Bit mask of device characteristics.
808 * TRUE if only one thread can access the device at a time.
811 * On successful return this parameter is filled by pointer to
812 * allocated device object.
819 IoCreateDevice(IN PDRIVER_OBJECT DriverObject
,
820 IN ULONG DeviceExtensionSize
,
821 IN PUNICODE_STRING DeviceName
,
822 IN DEVICE_TYPE DeviceType
,
823 IN ULONG DeviceCharacteristics
,
824 IN BOOLEAN Exclusive
,
825 OUT PDEVICE_OBJECT
*DeviceObject
)
827 WCHAR AutoNameBuffer
[20];
828 UNICODE_STRING AutoName
;
829 PDEVICE_OBJECT CreatedDeviceObject
;
830 PDEVOBJ_EXTENSION DeviceObjectExtension
;
831 OBJECT_ATTRIBUTES ObjectAttributes
;
833 ULONG AlignedDeviceExtensionSize
;
838 /* Check if we have to generate a name */
839 if (DeviceCharacteristics
& FILE_AUTOGENERATED_DEVICE_NAME
)
842 swprintf(AutoNameBuffer
,
844 InterlockedIncrementUL(&IopDeviceObjectNumber
));
846 /* Initialize the name */
847 RtlInitUnicodeString(&AutoName
, AutoNameBuffer
);
848 DeviceName
= &AutoName
;
851 /* Initialize the Object Attributes */
852 InitializeObjectAttributes(&ObjectAttributes
,
858 /* Honor exclusive flag */
859 if (Exclusive
) ObjectAttributes
.Attributes
|= OBJ_EXCLUSIVE
;
861 /* Create a permanent object for named devices */
862 if (DeviceName
) ObjectAttributes
.Attributes
|= OBJ_PERMANENT
;
864 /* Align the Extension Size to 8-bytes */
865 AlignedDeviceExtensionSize
= (DeviceExtensionSize
+ 7) &~ 7;
868 TotalSize
= AlignedDeviceExtensionSize
+
869 sizeof(DEVICE_OBJECT
) +
870 sizeof(EXTENDED_DEVOBJ_EXTENSION
);
872 /* Create the Device Object */
873 *DeviceObject
= NULL
;
874 Status
= ObCreateObject(KernelMode
,
882 (PVOID
*)&CreatedDeviceObject
);
883 if (!NT_SUCCESS(Status
)) return Status
;
885 /* Clear the whole Object and extension so we don't null stuff manually */
886 RtlZeroMemory(CreatedDeviceObject
, TotalSize
);
889 * Setup the Type and Size. Note that we don't use the aligned size,
890 * because that's only padding for the DevObjExt and not part of the Object.
892 CreatedDeviceObject
->Type
= IO_TYPE_DEVICE
;
893 CreatedDeviceObject
->Size
= sizeof(DEVICE_OBJECT
) + (USHORT
)DeviceExtensionSize
;
895 /* The kernel extension is after the driver internal extension */
896 DeviceObjectExtension
= (PDEVOBJ_EXTENSION
)
897 ((ULONG_PTR
)(CreatedDeviceObject
+ 1) +
898 AlignedDeviceExtensionSize
);
900 /* Set the Type and Size. Question: why is Size 0 on Windows? */
901 DeviceObjectExtension
->Type
= IO_TYPE_DEVICE_OBJECT_EXTENSION
;
902 DeviceObjectExtension
->Size
= 0;
904 /* Initialize with Power Manager */
905 PoInitializeDeviceObject(DeviceObjectExtension
);
907 /* Link the Object and Extension */
908 DeviceObjectExtension
->DeviceObject
= CreatedDeviceObject
;
909 CreatedDeviceObject
->DeviceObjectExtension
= DeviceObjectExtension
;
911 /* Set Device Object Data */
912 CreatedDeviceObject
->DeviceType
= DeviceType
;
913 CreatedDeviceObject
->Characteristics
= DeviceCharacteristics
;
914 CreatedDeviceObject
->DeviceExtension
= DeviceExtensionSize
?
915 CreatedDeviceObject
+ 1 :
917 CreatedDeviceObject
->StackSize
= 1;
918 CreatedDeviceObject
->AlignmentRequirement
= 0;
921 CreatedDeviceObject
->Flags
= DO_DEVICE_INITIALIZING
;
922 if (Exclusive
) CreatedDeviceObject
->Flags
|= DO_EXCLUSIVE
;
923 if (DeviceName
) CreatedDeviceObject
->Flags
|= DO_DEVICE_HAS_NAME
;
925 /* Attach a Vpb for Disks and Tapes, and create the Device Lock */
926 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK
) ||
927 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_VIRTUAL_DISK
) ||
928 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM
) ||
929 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE
))
932 Status
= IopCreateVpb(CreatedDeviceObject
);
933 if (!NT_SUCCESS(Status
))
935 /* Dereference the device object and fail */
936 ObDereferenceObject(CreatedDeviceObject
);
940 /* Initialize Lock Event */
941 KeInitializeEvent(&CreatedDeviceObject
->DeviceLock
,
942 SynchronizationEvent
,
946 /* Set the right Sector Size */
949 /* All disk systems */
950 case FILE_DEVICE_DISK_FILE_SYSTEM
:
951 case FILE_DEVICE_DISK
:
952 case FILE_DEVICE_VIRTUAL_DISK
:
954 /* The default is 512 bytes */
955 CreatedDeviceObject
->SectorSize
= 512;
958 /* CD-ROM file systems */
959 case FILE_DEVICE_CD_ROM_FILE_SYSTEM
:
961 /* The default is 2048 bytes */
962 CreatedDeviceObject
->SectorSize
= 2048;
965 /* Create the Device Queue */
966 if ((CreatedDeviceObject
->DeviceType
== FILE_DEVICE_DISK_FILE_SYSTEM
) ||
967 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_FILE_SYSTEM
) ||
968 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_CD_ROM_FILE_SYSTEM
) ||
969 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_NETWORK_FILE_SYSTEM
) ||
970 (CreatedDeviceObject
->DeviceType
== FILE_DEVICE_TAPE_FILE_SYSTEM
))
972 /* Simple FS Devices, they don't need a real Device Queue */
973 InitializeListHead(&CreatedDeviceObject
->Queue
.ListEntry
);
977 /* An actual Device, initialize its DQ */
978 KeInitializeDeviceQueue(&CreatedDeviceObject
->DeviceQueue
);
981 /* Insert the Object */
982 Status
= ObInsertObject(CreatedDeviceObject
,
984 FILE_READ_DATA
| FILE_WRITE_DATA
,
986 (PVOID
*)&CreatedDeviceObject
,
988 if (!NT_SUCCESS(Status
)) return Status
;
990 /* Now do the final linking */
991 ObReferenceObject(DriverObject
);
992 ASSERT((DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
) == 0);
993 CreatedDeviceObject
->DriverObject
= DriverObject
;
994 IopEditDeviceList(DriverObject
, CreatedDeviceObject
, IopAdd
);
996 /* Link with the power manager */
997 if (CreatedDeviceObject
->Vpb
) PoVolumeDevice(CreatedDeviceObject
);
999 /* Close the temporary handle and return to caller */
1000 ObCloseHandle(TempHandle
, KernelMode
);
1001 *DeviceObject
= CreatedDeviceObject
;
1002 return STATUS_SUCCESS
;
1013 IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject
)
1017 /* Check if the device is registered for shutdown notifications */
1018 if (DeviceObject
->Flags
& DO_SHUTDOWN_REGISTERED
)
1020 /* Call the shutdown notifications */
1021 IoUnregisterShutdownNotification(DeviceObject
);
1024 /* Check if it has a timer */
1025 Timer
= DeviceObject
->Timer
;
1028 /* Remove it and free it */
1029 IopRemoveTimerFromTimerList(Timer
);
1030 ExFreePoolWithTag(Timer
, TAG_IO_TIMER
);
1033 /* Check if the device has a name */
1034 if (DeviceObject
->Flags
& DO_DEVICE_HAS_NAME
)
1036 /* It does, make it temporary so we can remove it */
1037 ObMakeTemporaryObject(DeviceObject
);
1040 /* Set the pending delete flag */
1041 IoGetDevObjExtension(DeviceObject
)->ExtensionFlags
|= DOE_DELETE_PENDING
;
1043 /* Unlink with the power manager */
1044 if (DeviceObject
->Vpb
) PoRemoveVolumeDevice(DeviceObject
);
1046 /* Check if the device object can be unloaded */
1047 if (!DeviceObject
->ReferenceCount
) IopUnloadDevice(DeviceObject
);
1058 IoDetachDevice(IN PDEVICE_OBJECT TargetDevice
)
1060 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1063 DeviceExtension
= IoGetDevObjExtension(TargetDevice
->AttachedDevice
);
1064 ASSERT(DeviceExtension
->AttachedTo
== TargetDevice
);
1066 /* Remove the attachment */
1067 DeviceExtension
->AttachedTo
= NULL
;
1068 TargetDevice
->AttachedDevice
= NULL
;
1070 /* Check if it's ok to delete this device */
1071 if ((IoGetDevObjExtension(TargetDevice
)->ExtensionFlags
& DOE_DELETE_PENDING
) &&
1072 !(TargetDevice
->ReferenceCount
))
1075 IopUnloadDevice(TargetDevice
);
1084 IoEnumerateDeviceObjectList(IN PDRIVER_OBJECT DriverObject
,
1085 IN PDEVICE_OBJECT
*DeviceObjectList
,
1086 IN ULONG DeviceObjectListSize
,
1087 OUT PULONG ActualNumberDeviceObjects
)
1089 ULONG ActualDevices
= 1;
1090 PDEVICE_OBJECT CurrentDevice
= DriverObject
->DeviceObject
;
1092 /* Find out how many devices we'll enumerate */
1093 while ((CurrentDevice
= CurrentDevice
->NextDevice
)) ActualDevices
++;
1095 /* Go back to the first */
1096 CurrentDevice
= DriverObject
->DeviceObject
;
1098 /* Start by at least returning this */
1099 *ActualNumberDeviceObjects
= ActualDevices
;
1101 /* Check if we can support so many */
1102 if ((ActualDevices
* 4) > DeviceObjectListSize
)
1104 /* Fail because the buffer was too small */
1105 return STATUS_BUFFER_TOO_SMALL
;
1108 /* Check if the caller only wanted the size */
1109 if (DeviceObjectList
)
1111 /* Loop through all the devices */
1112 while (ActualDevices
)
1114 /* Reference each Device */
1115 ObReferenceObject(CurrentDevice
);
1117 /* Add it to the list */
1118 *DeviceObjectList
= CurrentDevice
;
1120 /* Go to the next one */
1121 CurrentDevice
= CurrentDevice
->NextDevice
;
1127 /* Return the status */
1128 return STATUS_SUCCESS
;
1132 * IoGetAttachedDevice
1139 IoGetAttachedDevice(PDEVICE_OBJECT DeviceObject
)
1141 /* Get the last attached device */
1142 while (DeviceObject
->AttachedDevice
)
1144 /* Move to the next one */
1145 DeviceObject
= DeviceObject
->AttachedDevice
;
1149 return DeviceObject
;
1153 * IoGetAttachedDeviceReference
1160 IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject
)
1162 /* Reference the Attached Device */
1163 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1164 ObReferenceObject(DeviceObject
);
1165 return DeviceObject
;
1173 IoGetDeviceAttachmentBaseRef(IN PDEVICE_OBJECT DeviceObject
)
1175 /* Reference the lowest attached device */
1176 DeviceObject
= IopGetLowestDevice(DeviceObject
);
1177 ObReferenceObject(DeviceObject
);
1178 return DeviceObject
;
1182 * IoGetDeviceObjectPointer
1189 IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName
,
1190 IN ACCESS_MASK DesiredAccess
,
1191 OUT PFILE_OBJECT
*FileObject
,
1192 OUT PDEVICE_OBJECT
*DeviceObject
)
1194 /* Call the helper routine for a normal operation */
1195 return IopGetDeviceObjectPointer(ObjectName
,
1207 IoGetDiskDeviceObject(IN PDEVICE_OBJECT FileSystemDeviceObject
,
1208 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
1210 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1215 /* Make sure there's a VPB */
1216 if (!FileSystemDeviceObject
->Vpb
) return STATUS_INVALID_PARAMETER
;
1219 IoAcquireVpbSpinLock(&OldIrql
);
1221 /* Get the Device Extension */
1222 DeviceExtension
= IoGetDevObjExtension(FileSystemDeviceObject
);
1224 /* Make sure this one has a VPB too */
1225 Vpb
= DeviceExtension
->Vpb
;
1228 /* Make sure that it's mounted */
1229 if ((Vpb
->ReferenceCount
) &&
1230 (Vpb
->Flags
& VPB_MOUNTED
))
1232 /* Return the Disk Device Object */
1233 *DiskDeviceObject
= Vpb
->RealDevice
;
1235 /* Reference it and return success */
1236 ObReferenceObject(Vpb
->RealDevice
);
1237 Status
= STATUS_SUCCESS
;
1241 /* It's not, so return failure */
1242 Status
= STATUS_VOLUME_DISMOUNTED
;
1248 Status
= STATUS_INVALID_PARAMETER
;
1251 /* Release the lock */
1252 IoReleaseVpbSpinLock(OldIrql
);
1261 IoGetLowerDeviceObject(IN PDEVICE_OBJECT DeviceObject
)
1263 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1264 PDEVICE_OBJECT LowerDeviceObject
= NULL
;
1266 /* Make sure it's not getting deleted */
1267 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1268 if (!(DeviceExtension
->ExtensionFlags
& (DOE_UNLOAD_PENDING
|
1269 DOE_DELETE_PENDING
|
1270 DOE_REMOVE_PENDING
|
1271 DOE_REMOVE_PROCESSED
)))
1273 /* Get the Lower Device Object */
1274 LowerDeviceObject
= DeviceExtension
->AttachedTo
;
1276 /* Check that we got a valid device object */
1277 if (LowerDeviceObject
)
1279 /* We did so let's reference it */
1280 ObReferenceObject(LowerDeviceObject
);
1285 return LowerDeviceObject
;
1293 IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject
)
1295 PDEVICE_OBJECT DeviceObject
= FileObject
->DeviceObject
;
1297 /* Check if we have a VPB with a device object */
1298 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1300 /* Then use the DO from the VPB */
1301 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1302 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1304 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1305 (FileObject
->DeviceObject
->Vpb
) &&
1306 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1308 /* The disk device actually has a VPB, so get the DO from there */
1309 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1313 /* Otherwise, this was a direct open */
1314 DeviceObject
= FileObject
->DeviceObject
;
1318 ASSERT(DeviceObject
!= NULL
);
1320 /* Check if we were attached */
1321 if (DeviceObject
->AttachedDevice
)
1323 /* Check if the file object has an extension present */
1324 if (FileObject
->Flags
& FO_FILE_OBJECT_HAS_EXTENSION
)
1326 /* Sanity check, direct open files can't have this */
1327 ASSERT(!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
));
1329 /* Check if the extension is really present */
1330 if (FileObject
->FileObjectExtension
)
1332 /* FIXME: Unhandled yet */
1333 DPRINT1("FOEs not supported\n");
1338 /* Return the highest attached device */
1339 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
1342 /* Return the DO we found */
1343 return DeviceObject
;
1351 IoGetRelatedTargetDevice(IN PFILE_OBJECT FileObject
,
1352 OUT PDEVICE_OBJECT
*DeviceObject
)
1355 PDEVICE_NODE DeviceNode
= NULL
;
1357 /* Call the internal helper function */
1358 Status
= IopGetRelatedTargetDevice(FileObject
, &DeviceNode
);
1359 if (NT_SUCCESS(Status
) && DeviceNode
)
1361 *DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
1371 IoGetBaseFileSystemDeviceObject(IN PFILE_OBJECT FileObject
)
1373 PDEVICE_OBJECT DeviceObject
;
1376 * If the FILE_OBJECT's VPB is defined,
1377 * get the device from it.
1379 if ((FileObject
->Vpb
) && (FileObject
->Vpb
->DeviceObject
))
1381 /* Use the VPB's Device Object's */
1382 DeviceObject
= FileObject
->Vpb
->DeviceObject
;
1384 else if (!(FileObject
->Flags
& FO_DIRECT_DEVICE_OPEN
) &&
1385 (FileObject
->DeviceObject
->Vpb
) &&
1386 (FileObject
->DeviceObject
->Vpb
->DeviceObject
))
1388 /* Use the VPB's File System Object */
1389 DeviceObject
= FileObject
->DeviceObject
->Vpb
->DeviceObject
;
1393 /* Use the FO's Device Object */
1394 DeviceObject
= FileObject
->DeviceObject
;
1397 /* Return the device object we found */
1398 ASSERT(DeviceObject
!= NULL
);
1399 return DeviceObject
;
1407 IoRegisterLastChanceShutdownNotification(IN PDEVICE_OBJECT DeviceObject
)
1409 PSHUTDOWN_ENTRY Entry
;
1411 /* Allocate the shutdown entry */
1412 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1413 sizeof(SHUTDOWN_ENTRY
),
1414 TAG_SHUTDOWN_ENTRY
);
1415 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1418 Entry
->DeviceObject
= DeviceObject
;
1420 /* Reference it so it doesn't go away */
1421 ObReferenceObject(DeviceObject
);
1423 /* Insert it into the list */
1424 ExInterlockedInsertHeadList(&LastChanceShutdownListHead
,
1425 &Entry
->ShutdownList
,
1428 /* Set the shutdown registered flag */
1429 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1430 return STATUS_SUCCESS
;
1438 IoRegisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1440 PSHUTDOWN_ENTRY Entry
;
1442 /* Allocate the shutdown entry */
1443 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
1444 sizeof(SHUTDOWN_ENTRY
),
1445 TAG_SHUTDOWN_ENTRY
);
1446 if (!Entry
) return STATUS_INSUFFICIENT_RESOURCES
;
1449 Entry
->DeviceObject
= DeviceObject
;
1451 /* Reference it so it doesn't go away */
1452 ObReferenceObject(DeviceObject
);
1454 /* Insert it into the list */
1455 ExInterlockedInsertHeadList(&ShutdownListHead
,
1456 &Entry
->ShutdownList
,
1459 /* Set the shutdown registered flag */
1460 DeviceObject
->Flags
|= DO_SHUTDOWN_REGISTERED
;
1461 return STATUS_SUCCESS
;
1469 IoUnregisterShutdownNotification(PDEVICE_OBJECT DeviceObject
)
1471 PSHUTDOWN_ENTRY ShutdownEntry
;
1472 PLIST_ENTRY NextEntry
;
1475 /* Remove the flag */
1476 DeviceObject
->Flags
&= ~DO_SHUTDOWN_REGISTERED
;
1478 /* Acquire the shutdown lock and loop the shutdown list */
1479 KeAcquireSpinLock(&ShutdownListLock
, &OldIrql
);
1480 NextEntry
= ShutdownListHead
.Flink
;
1481 while (NextEntry
!= &ShutdownListHead
)
1484 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1488 /* Get if the DO matches */
1489 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1491 /* Remove it from the list */
1492 RemoveEntryList(NextEntry
);
1493 NextEntry
= NextEntry
->Blink
;
1495 /* Free the entry */
1496 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1498 /* Get rid of our reference to it */
1499 ObDereferenceObject(DeviceObject
);
1502 /* Go to the next entry */
1503 NextEntry
= NextEntry
->Flink
;
1506 /* Now loop the last chance list */
1507 NextEntry
= LastChanceShutdownListHead
.Flink
;
1508 while (NextEntry
!= &LastChanceShutdownListHead
)
1511 ShutdownEntry
= CONTAINING_RECORD(NextEntry
,
1515 /* Get if the DO matches */
1516 if (ShutdownEntry
->DeviceObject
== DeviceObject
)
1518 /* Remove it from the list */
1519 RemoveEntryList(NextEntry
);
1520 NextEntry
= NextEntry
->Blink
;
1522 /* Free the entry */
1523 ExFreePoolWithTag(ShutdownEntry
, TAG_SHUTDOWN_ENTRY
);
1525 /* Get rid of our reference to it */
1526 ObDereferenceObject(DeviceObject
);
1529 /* Go to the next entry */
1530 NextEntry
= NextEntry
->Flink
;
1533 /* Release the shutdown lock */
1534 KeReleaseSpinLock(&ShutdownListLock
, OldIrql
);
1542 IoSetStartIoAttributes(IN PDEVICE_OBJECT DeviceObject
,
1543 IN BOOLEAN DeferredStartIo
,
1544 IN BOOLEAN NonCancelable
)
1546 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1548 /* Get the Device Extension */
1549 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1551 /* Set the flags the caller requested */
1552 DeviceExtension
->StartIoFlags
|= (DeferredStartIo
) ? DOE_SIO_DEFERRED
: 0;
1553 DeviceExtension
->StartIoFlags
|= (NonCancelable
) ? DOE_SIO_NO_CANCEL
: 0;
1561 IoStartNextPacketByKey(IN PDEVICE_OBJECT DeviceObject
,
1562 IN BOOLEAN Cancelable
,
1565 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1567 /* Get the Device Extension */
1568 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1570 /* Check if deferred start was requested */
1571 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1573 /* Call our internal function to handle the defered case */
1574 IopStartNextPacketByKeyEx(DeviceObject
,
1577 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1581 /* Call the normal routine */
1582 IopStartNextPacketByKey(DeviceObject
, Cancelable
, Key
);
1591 IoStartNextPacket(IN PDEVICE_OBJECT DeviceObject
,
1592 IN BOOLEAN Cancelable
)
1594 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1596 /* Get the Device Extension */
1597 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1599 /* Check if deferred start was requested */
1600 if (DeviceExtension
->StartIoFlags
& DOE_SIO_DEFERRED
)
1602 /* Call our internal function to handle the defered case */
1603 IopStartNextPacketByKeyEx(DeviceObject
,
1606 (Cancelable
? DOE_SIO_CANCELABLE
: 0));
1610 /* Call the normal routine */
1611 IopStartNextPacket(DeviceObject
, Cancelable
);
1620 IoStartPacket(IN PDEVICE_OBJECT DeviceObject
,
1623 IN PDRIVER_CANCEL CancelFunction
)
1626 KIRQL OldIrql
, CancelIrql
;
1628 /* Raise to dispatch level */
1629 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1631 /* Check if we should acquire the cancel lock */
1634 /* Acquire and set it */
1635 IoAcquireCancelSpinLock(&CancelIrql
);
1636 Irp
->CancelRoutine
= CancelFunction
;
1639 /* Check if we have a key */
1643 Stat
= KeInsertByKeyDeviceQueue(&DeviceObject
->DeviceQueue
,
1644 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
1649 /* Insert without a key */
1650 Stat
= KeInsertDeviceQueue(&DeviceObject
->DeviceQueue
,
1651 &Irp
->Tail
.Overlay
.DeviceQueueEntry
);
1654 /* Check if this was a first insert */
1658 DeviceObject
->CurrentIrp
= Irp
;
1660 /* Check if this is a cancelable packet */
1663 /* Check if the caller requested no cancellation */
1664 if (IoGetDevObjExtension(DeviceObject
)->StartIoFlags
&
1667 /* He did, so remove the cancel routine */
1668 Irp
->CancelRoutine
= NULL
;
1671 /* Release the cancel lock */
1672 IoReleaseCancelSpinLock(CancelIrql
);
1675 /* Call the Start I/O function */
1676 DeviceObject
->DriverObject
->DriverStartIo(DeviceObject
, Irp
);
1680 /* The packet was inserted... check if we have a cancel function */
1683 /* Check if the IRP got cancelled */
1687 * Set the cancel IRQL, clear the currnet cancel routine and
1690 Irp
->CancelIrql
= CancelIrql
;
1691 Irp
->CancelRoutine
= NULL
;
1692 CancelFunction(DeviceObject
, Irp
);
1696 /* Otherwise, release the lock */
1697 IoReleaseCancelSpinLock(CancelIrql
);
1702 /* Return back to previous IRQL */
1703 KeLowerIrql(OldIrql
);
1706 #if defined (_WIN64)
1709 IoWMIDeviceObjectToProviderId(
1710 IN PDEVICE_OBJECT DeviceObject
)