Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. init.c
  5. Abstract:
  6. This module contains the initialization code for softpci.sys
  7. Author:
  8. Nicholas Owens (nichow) 11-Mar-1999
  9. Revision History:
  10. Brandon Allsop (BrandonA) Feb, 2000 - added support to load devices from the registry during boot
  11. --*/
  12. #include "pch.h"
  13. UNICODE_STRING driverRegistryPath;
  14. SOFTPCI_TREE SoftPciTree;
  15. BOOLEAN SoftPciFailSafe = FALSE; // Setting this to true will cause adddevice to fail
  16. BOOLEAN SoftPciInterfaceRegistered = FALSE;
  17. NTSTATUS
  18. SoftPCIDriverAddDevice(
  19. IN PDRIVER_OBJECT DriverObject,
  20. IN PDEVICE_OBJECT PhysicalDeviceObject
  21. );
  22. VOID
  23. SoftPCIDriverUnload(
  24. IN PDRIVER_OBJECT DriverObject
  25. );
  26. NTSTATUS
  27. DriverEntry(
  28. IN PDRIVER_OBJECT DriverObject,
  29. IN PUNICODE_STRING RegistryPath
  30. )
  31. /*++
  32. Routine Description:
  33. This routine is called when the driver is loaded to initalize the driver.
  34. Arguments:
  35. DriverObject - Pointer to the driver object.
  36. RegistryPath - Registry path of the device object.
  37. Return Value:
  38. NTSTATUS.
  39. --*/
  40. {
  41. //
  42. // Fill in Entry points for Dispatch Routines
  43. //
  44. DriverObject->DriverExtension->AddDevice = SoftPCIDriverAddDevice;
  45. DriverObject->DriverUnload = SoftPCIDriverUnload;
  46. DriverObject->MajorFunction[IRP_MJ_PNP] = SoftPCIDispatchPnP;
  47. DriverObject->MajorFunction[IRP_MJ_POWER] = SoftPCIDispatchPower;
  48. DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = SoftPCIPassIrpDown; //Currenly no WMI
  49. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SoftPCIDispatchDeviceControl;
  50. DriverObject->MajorFunction[IRP_MJ_CREATE] = SoftPCIOpenDeviceControl;
  51. DriverObject->MajorFunction[IRP_MJ_CLOSE] = SoftPCICloseDeviceControl;
  52. //
  53. // Save the registry path to the driver.
  54. //
  55. RtlInitUnicodeString(&driverRegistryPath,
  56. RegistryPath->Buffer
  57. );
  58. return STATUS_SUCCESS;
  59. }
  60. NTSTATUS
  61. SoftPCIDriverAddDevice(
  62. IN PDRIVER_OBJECT DriverObject,
  63. IN PDEVICE_OBJECT PhysicalDeviceObject
  64. )
  65. /*++
  66. Routine Description:
  67. This routine adds the DeviceObjects for the FDO's and Filter DO's.
  68. Arguments:
  69. DriverObject - Pointer to the driver object.
  70. PhysicalDeviceObject - Pointer to the PDO.
  71. Return Value:
  72. NTSTATUS.
  73. --*/
  74. {
  75. NTSTATUS status;
  76. PDEVICE_OBJECT deviceObject;
  77. PSOFTPCI_DEVICE_EXTENSION deviceExtension;
  78. BOOLEAN isFDO;
  79. #if 0
  80. DbgBreakPoint();
  81. #endif
  82. if (SoftPciFailSafe) {
  83. return STATUS_UNSUCCESSFUL;
  84. }
  85. deviceExtension = NULL;
  86. status = IoCreateDevice(DriverObject,
  87. sizeof(SOFTPCI_DEVICE_EXTENSION),
  88. NULL,
  89. FILE_DEVICE_NULL,
  90. 0,
  91. FALSE,
  92. &deviceObject
  93. );
  94. if (!NT_SUCCESS(status)) {
  95. SoftPCIDbgPrint(
  96. SOFTPCI_ERROR,
  97. "SOFTPCI: DriverAddDevice - IoCreateDevice failed! status = 0x%x\n",
  98. status
  99. );
  100. goto Cleanup;
  101. }
  102. deviceExtension = deviceObject->DeviceExtension;
  103. //
  104. // Attach our Filter/FDO to the device stack.
  105. //
  106. deviceExtension->LowerDevObj = IoAttachDeviceToDeviceStack(deviceObject,
  107. PhysicalDeviceObject
  108. );
  109. if (deviceExtension->LowerDevObj==NULL) {
  110. SoftPCIDbgPrint(
  111. SOFTPCI_ERROR,
  112. "SOFTPCI: DriverAddDevice - IoAttachDeviceToDeviceStack failed!\n"
  113. );
  114. goto Cleanup;
  115. }
  116. //
  117. // Mark it as ours
  118. //
  119. deviceExtension->Signature = SPCI_SIG;
  120. //
  121. // Save the PDO in the device extension.
  122. //
  123. deviceExtension->PDO = PhysicalDeviceObject;
  124. //
  125. // Now lets see if we are an FDO or a FilterDO
  126. //
  127. isFDO = TRUE;
  128. status = SoftPCIQueryDeviceObjectType(deviceExtension->LowerDevObj, &isFDO);
  129. if (!NT_SUCCESS(status)) {
  130. SoftPCIDbgPrint(
  131. SOFTPCI_ERROR,
  132. "SOFTPCI: DriverAddDevice - QueryDeviceObjectType() failed! status = 0x%x\n",
  133. status
  134. );
  135. goto Cleanup;
  136. }
  137. if (isFDO) {
  138. //
  139. // This is a FDO so mark it in the device extension.
  140. //
  141. deviceExtension->FilterDevObj = FALSE;
  142. }else{
  143. //
  144. // This is a Filter DO so mark it in the device extension.
  145. //
  146. deviceExtension->FilterDevObj = TRUE;
  147. if (SoftPciTree.BusInterface == NULL) {
  148. SoftPciTree.BusInterface = ExAllocatePool(NonPagedPool,
  149. sizeof(SOFTPCI_PCIBUS_INTERFACE)
  150. );
  151. if (SoftPciTree.BusInterface == NULL) {
  152. status = STATUS_INSUFFICIENT_RESOURCES;
  153. goto Cleanup;
  154. }
  155. RtlZeroMemory(SoftPciTree.BusInterface, sizeof(SOFTPCI_PCIBUS_INTERFACE));
  156. }
  157. //
  158. // We save our filter device extensions in a global list for later use.
  159. //
  160. SoftPCIInsertEntryAtTail(&deviceExtension->ListEntry);
  161. //
  162. // Register a DeviceInterface. Since we can possibly be filtering more than one root bus
  163. // and we only need to access the first one, only bother with the first one.
  164. //
  165. if (!SoftPciInterfaceRegistered){
  166. deviceExtension->InterfaceRegistered = TRUE;
  167. status = IoRegisterDeviceInterface(PhysicalDeviceObject,
  168. (LPGUID)&GUID_SOFTPCI_INTERFACE,
  169. NULL,
  170. &(deviceExtension->SymbolicLinkName)
  171. );
  172. if (!NT_SUCCESS(status)) {
  173. SoftPCIDbgPrint(
  174. SOFTPCI_ERROR,
  175. "SOFTPCI: DriverAddDevice - Failed to register a device interface!\n"
  176. );
  177. }
  178. SoftPciInterfaceRegistered = TRUE;
  179. }
  180. //
  181. // Initialize our tree spinlock
  182. //
  183. KeInitializeSpinLock(&SoftPciTree.TreeLock);
  184. }
  185. if (NT_SUCCESS(status)) {
  186. deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
  187. return status;
  188. }
  189. Cleanup:
  190. //
  191. // Undo whatever was done
  192. //
  193. if (NT_SUCCESS(status)) {
  194. //
  195. // If we got here with STATUS_SUCCESS then we must have failed to attach to the stack.
  196. //
  197. status = STATUS_UNSUCCESSFUL;
  198. }
  199. if (deviceExtension && deviceExtension->LowerDevObj) {
  200. IoDetachDevice(deviceExtension->LowerDevObj);
  201. }
  202. if (deviceObject) {
  203. IoDeleteDevice(deviceObject);
  204. }
  205. return status;
  206. }
  207. VOID
  208. SoftPCIDriverUnload(
  209. IN PDRIVER_OBJECT DriverObject
  210. )
  211. /*++
  212. Routine Description:
  213. This routine does all clean-up work neccesary to remove the driver from memory.
  214. Arguments:
  215. DriverObject - Pointer to the driver object.
  216. Return Value:
  217. NTSTATUS.
  218. --*/
  219. {
  220. //TODO
  221. UNREFERENCED_PARAMETER(DriverObject);
  222. }