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.

182 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. state.c
  5. Abstract:
  6. Maintains state changes for power management power states
  7. for device objects
  8. Author:
  9. Ken Reneris (kenr) 19-July-1994
  10. Revision History:
  11. --*/
  12. #include "pop.h"
  13. // sync rules - only PoSetPowerState ever writes to the
  14. // StateValue entries in the psb.
  15. //
  16. NTKERNELAPI
  17. POWER_STATE
  18. PoSetPowerState (
  19. IN PDEVICE_OBJECT DeviceObject,
  20. IN POWER_STATE_TYPE Type,
  21. IN POWER_STATE State
  22. )
  23. /*++
  24. Routine Description:
  25. This routine stores the new power state for a device object,
  26. calling notification routines, if any, first.
  27. If the new state and old state are the same, this procedure
  28. is a noop
  29. A note on synchronization:
  30. No lock is acquire just to set the values. This is because
  31. it is assumed that only this routine writes them, so locking
  32. is not necessary.
  33. If the notify list is to be run, a lock will be acquired.
  34. Arguments:
  35. DeviceObject - pointer to the device object to set the power
  36. state for and to issue any notifications for
  37. Type - indicates whether System or Device state is being set
  38. State - the System or Device state to set
  39. Return Value:
  40. The Old power state.
  41. --*/
  42. {
  43. PDEVOBJ_EXTENSION doe;
  44. PDEVICE_OBJECT_POWER_EXTENSION dope;
  45. POWER_STATE OldState;
  46. BOOLEAN change;
  47. ULONG notificationmask;
  48. KIRQL OldIrql2;
  49. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  50. ASSERT(DeviceObject);
  51. PoPowerTrace(POWERTRACE_SETSTATE,DeviceObject,(ULONG)Type,(ULONG)State.SystemState);
  52. doe = DeviceObject->DeviceObjectExtension;
  53. dope = doe->Dope;
  54. notificationmask = 0L;
  55. change = FALSE;
  56. PopLockIrpSerialList(&OldIrql2);
  57. switch (Type) {
  58. case SystemPowerState:
  59. OldState.SystemState = PopGetDoSystemPowerState(doe);
  60. if (OldState.SystemState != State.SystemState) {
  61. change = TRUE;
  62. }
  63. break;
  64. case DevicePowerState:
  65. OldState.DeviceState = PopGetDoDevicePowerState(doe);
  66. if (OldState.DeviceState != State.DeviceState) {
  67. change = TRUE;
  68. if (OldState.DeviceState == PowerDeviceD0) {
  69. notificationmask = PO_NOTIFY_TRANSITIONING_FROM_D0;
  70. } else if (State.DeviceState == PowerDeviceD0) {
  71. notificationmask = PO_NOTIFY_D0;
  72. }
  73. }
  74. break;
  75. default:
  76. OldState.SystemState = PowerSystemUnspecified; // Never executed but keeps compiler happy
  77. break;
  78. }
  79. if (! change) {
  80. PopUnlockIrpSerialList(OldIrql2);
  81. return OldState;
  82. }
  83. //
  84. // We know what is going to happen. Always store the changed
  85. // state first, so we can drop the lock and do the notification.
  86. //
  87. switch (Type) {
  88. case SystemPowerState:
  89. PopSetDoSystemPowerState(doe, State.SystemState);
  90. break;
  91. case DevicePowerState:
  92. PopSetDoDevicePowerState(doe, State.DeviceState);
  93. break;
  94. }
  95. PopUnlockIrpSerialList(OldIrql2);
  96. //
  97. // If anything to notify...
  98. //
  99. if (notificationmask && dope) {
  100. PopStateChangeNotify(DeviceObject, notificationmask);
  101. }
  102. return OldState;
  103. }
  104. DEVICE_POWER_STATE
  105. PopLockGetDoDevicePowerState(
  106. IN PDEVOBJ_EXTENSION Doe
  107. )
  108. /*++
  109. Routine Description:
  110. Function which returns the power state of the specified device.
  111. Unlike PopGetDoDevicePowerState, this routine also acquires and
  112. releases the appropriate spinlock.
  113. Arguments:
  114. Doe - Supplies the devobj_extension of the device.
  115. Return Value:
  116. DEVICE_POWER_STATE
  117. --*/
  118. {
  119. KIRQL OldIrql;
  120. DEVICE_POWER_STATE State;
  121. PopLockIrpSerialList(&OldIrql);
  122. State = PopGetDoDevicePowerState(Doe);
  123. PopUnlockIrpSerialList(OldIrql);
  124. return(State);
  125. }