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
8.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: power.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. VOID
  12. PowerStateCallback(
  13. IN PVOID CallbackContext,
  14. IN PVOID Argument1,
  15. IN PVOID Argument2
  16. )
  17. {
  18. ULONG_PTR action = (ULONG_PTR)Argument1;
  19. ULONG_PTR state = (ULONG_PTR)Argument2;
  20. UNREFERENCED_PARAMETER(CallbackContext);
  21. if( PO_CB_AC_STATUS == action ) {
  22. //
  23. // AC <-> DC Transition has occurred
  24. // state == TRUE if on AC, else FALSE.
  25. //
  26. PowerStateIsAC = (BOOLEAN)state;
  27. // DbgPrint("PowerState is now %s\n",PowerStateIsAC?"AC":"Battery");
  28. }
  29. return;
  30. }
  31. NTSTATUS
  32. PptPowerComplete (
  33. IN PDEVICE_OBJECT pDeviceObject,
  34. IN PIRP pIrp,
  35. IN PFDO_EXTENSION Fdx
  36. )
  37. /*++
  38. Routine Description:
  39. This routine handles all IRP_MJ_POWER IRPs.
  40. Arguments:
  41. pDeviceObject - represents the port device
  42. pIrp - PNP irp
  43. Fdx - Device Extension
  44. Return Value:
  45. Status
  46. --*/
  47. {
  48. POWER_STATE_TYPE powerType;
  49. POWER_STATE powerState;
  50. PIO_STACK_LOCATION pIrpStack;
  51. UNREFERENCED_PARAMETER( pDeviceObject );
  52. if( pIrp->PendingReturned ) {
  53. IoMarkIrpPending( pIrp );
  54. }
  55. pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
  56. powerType = pIrpStack->Parameters.Power.Type;
  57. powerState = pIrpStack->Parameters.Power.State;
  58. switch (pIrpStack->MinorFunction) {
  59. case IRP_MN_QUERY_POWER:
  60. ASSERTMSG ("Invalid power completion minor code: Query Power\n", FALSE);
  61. break;
  62. case IRP_MN_SET_POWER:
  63. DD((PCE)Fdx,DDT,"Power - Setting %s state to %d\n",
  64. ( (powerType == SystemPowerState) ? "System" : "Device" ), powerState.SystemState);
  65. switch (powerType) {
  66. case DevicePowerState:
  67. if (Fdx->DeviceState < powerState.DeviceState) {
  68. //
  69. // Powering down
  70. //
  71. ASSERTMSG ("Invalid power completion Device Down\n", FALSE);
  72. } else if (powerState.DeviceState < Fdx->DeviceState) {
  73. //
  74. // Powering Up
  75. //
  76. PoSetPowerState (Fdx->DeviceObject, powerType, powerState);
  77. if (PowerDeviceD0 == Fdx->DeviceState) {
  78. //
  79. // Do the power on stuff here.
  80. //
  81. }
  82. Fdx->DeviceState = powerState.DeviceState;
  83. }
  84. break;
  85. case SystemPowerState:
  86. if (Fdx->SystemState < powerState.SystemState) {
  87. //
  88. // Powering down
  89. //
  90. ASSERTMSG ("Invalid power completion System Down\n", FALSE);
  91. } else if (powerState.SystemState < Fdx->SystemState) {
  92. //
  93. // Powering Up
  94. //
  95. if (PowerSystemWorking == powerState.SystemState) {
  96. //
  97. // Do the system start up stuff here.
  98. //
  99. powerState.DeviceState = PowerDeviceD0;
  100. PoRequestPowerIrp (Fdx->DeviceObject,
  101. IRP_MN_SET_POWER,
  102. powerState,
  103. NULL, // no completion function
  104. NULL, // and no context
  105. NULL);
  106. }
  107. Fdx->SystemState = powerState.SystemState;
  108. }
  109. break;
  110. }
  111. break;
  112. default:
  113. ASSERTMSG ("Power Complete: Bad Power State", FALSE);
  114. }
  115. PoStartNextPowerIrp (pIrp);
  116. return STATUS_SUCCESS;
  117. }
  118. NTSTATUS
  119. PptFdoPower (
  120. IN PDEVICE_OBJECT pDeviceObject,
  121. IN PIRP pIrp
  122. )
  123. /*++
  124. Routine Description:
  125. This routine handles all IRP_MJ_POWER IRPs.
  126. Arguments:
  127. pDeviceObject - represents the port device
  128. pIrp - PNP irp
  129. Return Value:
  130. Status
  131. --*/
  132. {
  133. POWER_STATE_TYPE powerType;
  134. POWER_STATE powerState;
  135. PIO_STACK_LOCATION pIrpStack;
  136. NTSTATUS status;
  137. PFDO_EXTENSION fdx;
  138. BOOLEAN hookit = FALSE;
  139. BOOLEAN bogusIrp = FALSE;
  140. //
  141. // WORKWORK. THIS CODE DOESN'T DO MUCH...NEED TO CHECK OUT FULL POWER FUNCTIONALITY.
  142. //
  143. fdx = pDeviceObject->DeviceExtension;
  144. pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
  145. status = PptAcquireRemoveLock(&fdx->RemoveLock, pIrp);
  146. if( !NT_SUCCESS(status) ) {
  147. PoStartNextPowerIrp(pIrp);
  148. return P4CompleteRequest( pIrp, status, pIrp->IoStatus.Information );
  149. }
  150. powerType = pIrpStack->Parameters.Power.Type;
  151. powerState = pIrpStack->Parameters.Power.State;
  152. switch (pIrpStack->MinorFunction) {
  153. case IRP_MN_QUERY_POWER:
  154. status = STATUS_SUCCESS;
  155. break;
  156. case IRP_MN_SET_POWER:
  157. DD((PCE)fdx,DDT,"Power - Setting %s state to %d\n",
  158. ( (powerType == SystemPowerState) ? "System" : "Device" ), powerState.SystemState);
  159. status = STATUS_SUCCESS;
  160. switch (powerType) {
  161. case DevicePowerState:
  162. if (fdx->DeviceState < powerState.DeviceState) {
  163. //
  164. // Powering down
  165. //
  166. PoSetPowerState (fdx->DeviceObject, powerType, powerState);
  167. if (PowerDeviceD0 == fdx->DeviceState) {
  168. //
  169. // Do the power on stuff here.
  170. //
  171. }
  172. fdx->DeviceState = powerState.DeviceState;
  173. } else if (powerState.DeviceState < fdx->DeviceState) {
  174. //
  175. // Powering Up
  176. //
  177. hookit = TRUE;
  178. }
  179. break;
  180. case SystemPowerState:
  181. if (fdx->SystemState < powerState.SystemState) {
  182. //
  183. // Powering down
  184. //
  185. if (PowerSystemWorking == fdx->SystemState) {
  186. //
  187. // Do the system shut down stuff here.
  188. //
  189. }
  190. powerState.DeviceState = PowerDeviceD3;
  191. PoRequestPowerIrp (fdx->DeviceObject,
  192. IRP_MN_SET_POWER,
  193. powerState,
  194. NULL, // no completion function
  195. NULL, // and no context
  196. NULL);
  197. fdx->SystemState = powerState.SystemState;
  198. } else if (powerState.SystemState < fdx->SystemState) {
  199. //
  200. // Powering Up
  201. //
  202. hookit = TRUE;
  203. }
  204. break;
  205. }
  206. break;
  207. default:
  208. bogusIrp = TRUE;
  209. status = STATUS_NOT_SUPPORTED;
  210. }
  211. IoCopyCurrentIrpStackLocationToNext (pIrp);
  212. if (!NT_SUCCESS (status)) {
  213. PoStartNextPowerIrp (pIrp);
  214. if( bogusIrp ) {
  215. status = PoCallDriver( fdx->ParentDeviceObject, pIrp );
  216. } else {
  217. P4CompleteRequest( pIrp, status, pIrp->IoStatus.Information );
  218. }
  219. } else if (hookit) {
  220. IoSetCompletionRoutine( pIrp, PptPowerComplete, fdx, TRUE, TRUE, TRUE );
  221. status = PoCallDriver (fdx->ParentDeviceObject, pIrp);
  222. } else {
  223. PoStartNextPowerIrp (pIrp);
  224. status = PoCallDriver (fdx->ParentDeviceObject, pIrp);
  225. }
  226. PptReleaseRemoveLock(&fdx->RemoveLock, pIrp);
  227. return status;
  228. }