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.

443 lines
11 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. activate.c
  5. Abstract:
  6. This module contains code to prepare an SD card in a given
  7. slot for use. This is done when the host is started, after
  8. device insertions, and after a power up transition.
  9. Authors:
  10. Neil Sandlin (neilsa) Jan 1, 2002
  11. Environment:
  12. Kernel mode only
  13. Notes:
  14. Revision History:
  15. --*/
  16. #include "pch.h"
  17. //
  18. // Internal References
  19. //
  20. VOID
  21. SdbusActivatePowerUpComplete(
  22. IN PSD_WORK_PACKET WorkPacket,
  23. IN NTSTATUS status
  24. );
  25. VOID
  26. SdbusActivateIdentifyPhase1Complete(
  27. IN PSD_WORK_PACKET WorkPacket,
  28. IN NTSTATUS status
  29. );
  30. VOID
  31. SdbusActivateIdentifyPhase2Complete(
  32. IN PSD_WORK_PACKET WorkPacket,
  33. IN NTSTATUS status
  34. );
  35. VOID
  36. SdbusActivateInitializeCardComplete(
  37. IN PSD_WORK_PACKET WorkPacket,
  38. IN NTSTATUS status
  39. );
  40. //
  41. //
  42. //
  43. VOID
  44. SdbusActivateSocket(
  45. IN PDEVICE_OBJECT Fdo,
  46. IN PSDBUS_ACTIVATE_COMPLETION_ROUTINE CompletionRoutine,
  47. IN PVOID Context
  48. )
  49. /*++
  50. Routine Description:
  51. Arguments:
  52. Fdo - Pointer to the device object for the host controller
  53. Return Value:
  54. --*/
  55. {
  56. NTSTATUS status;
  57. PFDO_EXTENSION fdoExtension = Fdo->DeviceExtension;
  58. PSD_WORK_PACKET workPacket1 = NULL, workPacket2 = NULL, workPacket3 = NULL, workPacket4 = NULL;
  59. BOOLEAN cardInSlot;
  60. BOOLEAN callCompletion;
  61. PSD_ACTIVATE_CONTEXT activateContext;
  62. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x activate socket START\n", Fdo));
  63. activateContext = ExAllocatePool(NonPagedPool, sizeof(SD_ACTIVATE_CONTEXT));
  64. if (!activateContext) {
  65. ASSERT(activateContext != NULL);
  66. return;
  67. }
  68. activateContext->CompletionRoutine = CompletionRoutine;
  69. activateContext->Context = Context;
  70. try{
  71. cardInSlot = (*(fdoExtension->FunctionBlock->DetectCardInSocket))(fdoExtension);
  72. if (!cardInSlot) {
  73. if (fdoExtension->SocketState != SOCKET_EMPTY) {
  74. IoInvalidateDeviceRelations(fdoExtension->Pdo, BusRelations);
  75. }
  76. //ISSUE: implement synchronization
  77. fdoExtension->SocketState = SOCKET_EMPTY;
  78. callCompletion = TRUE;
  79. status = STATUS_SUCCESS;
  80. leave;
  81. }
  82. fdoExtension->SocketState = CARD_DETECTED;
  83. status = SdbusBuildWorkPacket(fdoExtension,
  84. SDWP_POWER_ON,
  85. SdbusActivatePowerUpComplete,
  86. NULL,
  87. &workPacket1);
  88. if (!NT_SUCCESS(status)) {
  89. callCompletion = TRUE;
  90. leave;
  91. }
  92. status = SdbusBuildWorkPacket(fdoExtension,
  93. SDWP_IDENTIFY_IO_DEVICE,
  94. SdbusActivateIdentifyPhase1Complete,
  95. NULL,
  96. &workPacket2);
  97. if (!NT_SUCCESS(status)) {
  98. callCompletion = TRUE;
  99. leave;
  100. }
  101. status = SdbusBuildWorkPacket(fdoExtension,
  102. SDWP_IDENTIFY_MEMORY_DEVICE,
  103. SdbusActivateIdentifyPhase2Complete,
  104. NULL,
  105. &workPacket3);
  106. if (!NT_SUCCESS(status)) {
  107. callCompletion = TRUE;
  108. leave;
  109. }
  110. status = SdbusBuildWorkPacket(fdoExtension,
  111. SDWP_INITIALIZE_CARD,
  112. SdbusActivateInitializeCardComplete,
  113. activateContext,
  114. &workPacket4);
  115. if (!NT_SUCCESS(status)) {
  116. callCompletion = TRUE;
  117. leave;
  118. }
  119. workPacket1->NextWorkPacketInChain = workPacket2;
  120. workPacket2->NextWorkPacketInChain = workPacket3;
  121. workPacket3->NextWorkPacketInChain = workPacket4;
  122. SdbusQueueWorkPacket(fdoExtension, workPacket1, WP_TYPE_SYSTEM);
  123. callCompletion = FALSE;
  124. } finally {
  125. if (callCompletion) {
  126. if (activateContext->CompletionRoutine) {
  127. (*activateContext->CompletionRoutine)(Fdo, activateContext->Context, status);
  128. }
  129. if (workPacket1) {
  130. ExFreePool(workPacket1);
  131. }
  132. if (workPacket2) {
  133. ExFreePool(workPacket2);
  134. }
  135. if (workPacket3) {
  136. ExFreePool(workPacket3);
  137. }
  138. if (workPacket4) {
  139. ExFreePool(workPacket4);
  140. }
  141. ExFreePool(activateContext);
  142. }
  143. }
  144. }
  145. VOID
  146. SdbusActivatePowerUpComplete(
  147. IN PSD_WORK_PACKET WorkPacket,
  148. IN NTSTATUS status
  149. )
  150. /*++
  151. Routine Description:
  152. Arguments:
  153. Return Value:
  154. --*/
  155. {
  156. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x activate PowerUp Complete\n", WorkPacket->FdoExtension->DeviceObject));
  157. if (WorkPacket->NextWorkPacketInChain) {
  158. WorkPacket->NextWorkPacketInChain->ChainedStatus = status;
  159. }
  160. ExFreePool(WorkPacket);
  161. }
  162. VOID
  163. SdbusActivateIdentifyPhase1Complete(
  164. IN PSD_WORK_PACKET WorkPacket,
  165. IN NTSTATUS status
  166. )
  167. /*++
  168. Routine Description:
  169. Arguments:
  170. Return Value:
  171. --*/
  172. {
  173. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x activate Identify Phase1 Complete\n", WorkPacket->FdoExtension->DeviceObject));
  174. if (WorkPacket->NextWorkPacketInChain) {
  175. WorkPacket->NextWorkPacketInChain->ChainedStatus = status;
  176. }
  177. ExFreePool(WorkPacket);
  178. }
  179. VOID
  180. SdbusActivateIdentifyPhase2Complete(
  181. IN PSD_WORK_PACKET WorkPacket,
  182. IN NTSTATUS status
  183. )
  184. /*++
  185. Routine Description:
  186. Arguments:
  187. Fdo - Pointer to the device object for the host controller
  188. Return Value:
  189. --*/
  190. {
  191. PFDO_EXTENSION fdoExtension = WorkPacket->FdoExtension;
  192. PDEVICE_OBJECT Fdo = fdoExtension->DeviceObject;
  193. PSD_ACTIVATE_CONTEXT activateContext = WorkPacket->CompletionContext;
  194. BOOLEAN callCompletion;
  195. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x activate Identify Phase2 Complete\n", WorkPacket->FdoExtension->DeviceObject));
  196. ExFreePool(WorkPacket);
  197. #if 0
  198. try{
  199. if (!NT_SUCCESS(status)) {
  200. callCompletion = TRUE;
  201. leave;
  202. }
  203. //
  204. // If we get this far, we expect that there is at least one function detected
  205. //
  206. if (!fdoExtension->numFunctions && !fdoExtension->memFunction) {
  207. callCompletion = TRUE;
  208. SdbusDumpDbgLog();
  209. status = STATUS_UNSUCCESSFUL;
  210. leave;
  211. }
  212. //
  213. // The card should be in the Identification state. Send a CMD3 to get the relative address,
  214. // and to move the card to the standby state.
  215. //
  216. status = SdbusBuildWorkPacket(fdoExtension,
  217. SDWP_PASSTHRU,
  218. SdbusActivateTransitionToStandbyCompletion,
  219. activateContext,
  220. &WorkPacket);
  221. if (!NT_SUCCESS(status)) {
  222. callCompletion = TRUE;
  223. leave;
  224. }
  225. WorkPacket->ExecutingSDCommand = TRUE;
  226. WorkPacket->Cmd = SDCMD_SEND_RELATIVE_ADDR;
  227. WorkPacket->ResponseType = SDCMD_RESP_6;
  228. SdbusQueueWorkPacket(fdoExtension, WorkPacket, WP_TYPE_SYSTEM_PRIORITY);
  229. callCompletion = FALSE;
  230. } finally {
  231. if (callCompletion) {
  232. if (activateContext->CompletionRoutine) {
  233. (*activateContext->CompletionRoutine)(Fdo, activateContext->Context, status);
  234. }
  235. ExFreePool(activateContext);
  236. }
  237. }
  238. #endif
  239. }
  240. #if 0
  241. VOID
  242. SdbusActivateTransitionToStandbyCompletion(
  243. IN PSD_WORK_PACKET WorkPacket,
  244. IN NTSTATUS status
  245. )
  246. /*++
  247. Routine Description:
  248. Arguments:
  249. Fdo - Pointer to the device object for the host controller
  250. Return Value:
  251. --*/
  252. {
  253. PFDO_EXTENSION fdoExtension = WorkPacket->FdoExtension;
  254. PDEVICE_OBJECT Fdo = fdoExtension->DeviceObject;
  255. PSD_ACTIVATE_CONTEXT activateContext = WorkPacket->CompletionContext;
  256. BOOLEAN callCompletion;
  257. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x activate TransitionToStandby COMPLETE %08x\n", Fdo, status));
  258. try{
  259. if (!NT_SUCCESS(status)) {
  260. callCompletion = TRUE;
  261. leave;
  262. }
  263. fdoExtension->RelativeAddr = WorkPacket->ResponseBuffer[0] & 0xFFFF0000;
  264. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x relative addr %08x\n", Fdo, fdoExtension->RelativeAddr));
  265. status = SdbusBuildWorkPacket(fdoExtension,
  266. SDWP_INITIALIZE_CARD,
  267. SdbusActivateInitializeCardComplete,
  268. activateContext,
  269. &workPacket);
  270. if (!NT_SUCCESS(status)) {
  271. callCompletion = TRUE;
  272. leave;
  273. }
  274. } else {
  275. if (activateContext->CompletionRoutine) {
  276. (*activateContext->CompletionRoutine)(Fdo, activateContext->Context, status);
  277. }
  278. ExFreePool(activateContext);
  279. }
  280. ExFreePool(WorkPacket);
  281. }
  282. #endif
  283. VOID
  284. SdbusActivateInitializeCardComplete(
  285. IN PSD_WORK_PACKET WorkPacket,
  286. IN NTSTATUS status
  287. )
  288. /*++
  289. Routine Description:
  290. Arguments:
  291. Fdo - Pointer to the device object for the host controller
  292. Return Value:
  293. --*/
  294. {
  295. PFDO_EXTENSION fdoExtension = WorkPacket->FdoExtension;
  296. PDEVICE_OBJECT Fdo = fdoExtension->DeviceObject;
  297. PSD_ACTIVATE_CONTEXT activateContext = WorkPacket->CompletionContext;
  298. DebugPrint((SDBUS_DEBUG_ENUM, "fdo %08x activate socket COMPLETE %08x\n", Fdo, status));
  299. if (NT_SUCCESS(status)) {
  300. fdoExtension->SocketState = CARD_NEEDS_ENUMERATION;
  301. IoInvalidateDeviceRelations(fdoExtension->Pdo, BusRelations);
  302. } else {
  303. DebugPrint((SDBUS_DEBUG_FAIL, "fdo %08x activate failure %08x\n", Fdo, status));
  304. SdbusDumpDbgLog();
  305. }
  306. ExFreePool(WorkPacket);
  307. if (activateContext->CompletionRoutine) {
  308. (*activateContext->CompletionRoutine)(Fdo, activateContext->Context, status);
  309. }
  310. ExFreePool(activateContext);
  311. }