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.

386 lines
8.7 KiB

  1. #include "usbsc.h"
  2. #include "usbscpwr.h"
  3. #include "usbscnt.h"
  4. NTSTATUS
  5. UsbScDevicePower(
  6. PDEVICE_OBJECT DeviceObject,
  7. PIRP Irp
  8. )
  9. /*++
  10. Routine Description:
  11. Handles Device Power Irps
  12. Arguments:
  13. Return Value:
  14. --*/
  15. {
  16. NTSTATUS status = STATUS_SUCCESS;
  17. PDEVICE_EXTENSION pDevExt;
  18. PIO_STACK_LOCATION stack;
  19. BOOLEAN postWaitWake;
  20. POWER_STATE state;
  21. __try
  22. {
  23. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScDevicePower Enter\n",DRIVER_NAME ));
  24. pDevExt = DeviceObject->DeviceExtension;
  25. stack = IoGetCurrentIrpStackLocation(Irp);
  26. state.DeviceState = stack->Parameters.Power.State.DeviceState;
  27. switch (stack->MinorFunction) {
  28. case IRP_MN_QUERY_POWER:
  29. //
  30. // Since we can always wait for our irps to complete, so we just always succeed
  31. //
  32. IoReleaseRemoveLock(&pDevExt->RemoveLock,
  33. Irp);
  34. PoStartNextPowerIrp(Irp);
  35. IoSkipCurrentIrpStackLocation(Irp);
  36. status = PoCallDriver(pDevExt->LowerDeviceObject,
  37. Irp);
  38. break;
  39. case IRP_MN_SET_POWER:
  40. if (state.DeviceState < pDevExt->PowerState) {
  41. //
  42. // We are coming up!! We must let lower drivers power up before we do
  43. //
  44. IoMarkIrpPending(Irp);
  45. IoCopyCurrentIrpStackLocationToNext(Irp);
  46. IoSetCompletionRoutine(Irp,
  47. UsbScDevicePowerUpCompletion,
  48. pDevExt,
  49. TRUE,
  50. TRUE,
  51. TRUE);
  52. status = PoCallDriver(pDevExt->LowerDeviceObject,
  53. Irp);
  54. status = STATUS_PENDING;
  55. } else {
  56. //
  57. // We are moving to a lower power state, so we handle it before
  58. // passing it down
  59. //
  60. status = UsbScSetDevicePowerState(DeviceObject,
  61. state.DeviceState,
  62. &postWaitWake);
  63. PoSetPowerState(DeviceObject,
  64. DevicePowerState,
  65. state);
  66. IoReleaseRemoveLock(&pDevExt->RemoveLock,
  67. Irp);
  68. PoStartNextPowerIrp(Irp);
  69. IoSkipCurrentIrpStackLocation(Irp);
  70. status = PoCallDriver(pDevExt->LowerDeviceObject,
  71. Irp);
  72. }
  73. break;
  74. default:
  75. // We shouldn't be here
  76. ASSERT(FALSE);
  77. break;
  78. }
  79. }
  80. __finally
  81. {
  82. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScDevicePower Exit : 0x%x\n",DRIVER_NAME, status ));
  83. }
  84. return status;
  85. }
  86. NTSTATUS
  87. UsbScSystemPower(
  88. PDEVICE_OBJECT DeviceObject,
  89. PIRP Irp
  90. )
  91. /*++
  92. Routine Description:
  93. Handles system power irps
  94. Arguments:
  95. Return Value:
  96. --*/
  97. {
  98. NTSTATUS status = STATUS_SUCCESS;
  99. PDEVICE_EXTENSION pDevExt;
  100. __try
  101. {
  102. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScSystemPower Enter\n",DRIVER_NAME ));
  103. pDevExt = DeviceObject->DeviceExtension;
  104. IoMarkIrpPending(Irp);
  105. IoCopyCurrentIrpStackLocationToNext(Irp);
  106. IoSetCompletionRoutine(Irp,
  107. UsbScSystemPowerCompletion,
  108. pDevExt,
  109. TRUE,
  110. TRUE,
  111. TRUE);
  112. status = PoCallDriver(pDevExt->LowerDeviceObject,
  113. Irp);
  114. status = STATUS_PENDING;
  115. }
  116. __finally
  117. {
  118. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScSystemPower Exit : 0x%x\n",DRIVER_NAME, status ));
  119. }
  120. return status;
  121. }
  122. NTSTATUS
  123. UsbScSystemPowerCompletion(
  124. PDEVICE_OBJECT DeviceObject,
  125. PIRP Irp,
  126. PVOID Context
  127. )
  128. /*++
  129. Routine Description:
  130. Completion routine called after system power irp has been passed down the stack.
  131. handles mapping system state to device state and requests the device power irp.
  132. Arguments:
  133. Return Value:
  134. --*/
  135. {
  136. NTSTATUS status = STATUS_SUCCESS;
  137. PDEVICE_EXTENSION pDevExt;
  138. PIO_STACK_LOCATION irpStack;
  139. POWER_STATE state;
  140. POWER_STATE systemState;
  141. __try
  142. {
  143. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScSystemPowerCompletion Enter\n",DRIVER_NAME ));
  144. pDevExt = (PDEVICE_EXTENSION) Context;
  145. if (!NT_SUCCESS(Irp->IoStatus.Status)) {
  146. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScSystemPowerCompletion SIRP failed by lower driver\n",DRIVER_NAME ));
  147. PoStartNextPowerIrp(Irp);
  148. IoCompleteRequest(Irp,
  149. IO_NO_INCREMENT);
  150. status = Irp->IoStatus.Status;
  151. IoReleaseRemoveLock(&pDevExt->RemoveLock,
  152. Irp);
  153. __leave;
  154. }
  155. irpStack = IoGetCurrentIrpStackLocation(Irp);
  156. systemState = irpStack->Parameters.Power.State;
  157. state.DeviceState = pDevExt->DeviceCapabilities.DeviceState[systemState.SystemState];
  158. status = PoRequestPowerIrp(DeviceObject,
  159. irpStack->MinorFunction,
  160. state,
  161. UsbScDeviceRequestCompletion,
  162. (PVOID) Irp,
  163. NULL);
  164. ASSERT(NT_SUCCESS(status));
  165. status = STATUS_MORE_PROCESSING_REQUIRED;
  166. }
  167. __finally
  168. {
  169. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScSystemPowerCompletion Exit : 0x%x\n",DRIVER_NAME, status ));
  170. }
  171. return status;
  172. }
  173. VOID
  174. UsbScDeviceRequestCompletion(
  175. PDEVICE_OBJECT DeviceObject,
  176. UCHAR MinorFunction,
  177. POWER_STATE PowerState,
  178. PVOID Context,
  179. PIO_STATUS_BLOCK IoStatus
  180. )
  181. /*++
  182. Routine Description:
  183. Completion routine called after device power irp completes.
  184. Completes the system power irp.
  185. Arguments:
  186. Return Value:
  187. --*/
  188. {
  189. NTSTATUS status = STATUS_SUCCESS;
  190. PDEVICE_EXTENSION pDevExt;
  191. PIRP irp;
  192. __try
  193. {
  194. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScDeviceRequestCompletion Enter\n",DRIVER_NAME ));
  195. pDevExt = DeviceObject->DeviceExtension;
  196. irp = (PIRP) Context;
  197. PoStartNextPowerIrp(irp);
  198. irp->IoStatus.Status = IoStatus->Status;
  199. IoCompleteRequest(irp,
  200. IO_NO_INCREMENT);
  201. IoReleaseRemoveLock(&pDevExt->RemoveLock,
  202. irp);
  203. }
  204. __finally
  205. {
  206. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScDeviceRequestCompletion Exit : 0x%x\n",DRIVER_NAME, status ));
  207. }
  208. return;
  209. }
  210. NTSTATUS
  211. UsbScDevicePowerUpCompletion(
  212. PDEVICE_OBJECT DeviceObject,
  213. PIRP Irp,
  214. PVOID Context
  215. )
  216. /*++
  217. Routine Description:
  218. Completion routine called after device irp for higher power state has been
  219. passed down the stack.
  220. Arguments:
  221. Return Value:
  222. --*/
  223. {
  224. NTSTATUS status = STATUS_SUCCESS;
  225. PDEVICE_EXTENSION pDevExt;
  226. PIO_STACK_LOCATION irpStack;
  227. BOOLEAN postWaitWake; // We don't really care about this
  228. __try
  229. {
  230. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScDevicePowerUpCompletion Enter\n",DRIVER_NAME ));
  231. pDevExt = DeviceObject->DeviceExtension;
  232. irpStack = IoGetCurrentIrpStackLocation(Irp);
  233. status = UsbScSetDevicePowerState(DeviceObject,
  234. irpStack->Parameters.Power.State.DeviceState,
  235. &postWaitWake);
  236. PoSetPowerState(DeviceObject,
  237. DevicePowerState,
  238. irpStack->Parameters.Power.State);
  239. PoStartNextPowerIrp(Irp);
  240. }
  241. __finally
  242. {
  243. IoReleaseRemoveLock(&pDevExt->RemoveLock,
  244. Irp);
  245. SmartcardDebug( DEBUG_TRACE, ("%s!UsbScDevicePowerUpCompletion Exit : 0x%x\n",DRIVER_NAME, status ));
  246. }
  247. return status;
  248. }