Source code of Windows XP (NT5)
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.

374 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1990-1998 Microsoft Corporation, All Rights Reserved.
  3. Module Name:
  4. atmsmdrv.c
  5. Abstract:
  6. A sample ATM Client driver.
  7. This sample demonstrates establishment and tear down of P-P or PMP
  8. connections over ATM.
  9. This module contains the driver entry routine.
  10. Author:
  11. Anil Francis Thomas (10/98)
  12. Environment:
  13. Kernel
  14. Revision History:
  15. --*/
  16. #include "precomp.h"
  17. #pragma hdrstop
  18. #define MODULE_ID MODULE_INIT
  19. #pragma NDIS_INIT_FUNCTION(DriverEntry)
  20. VOID AtmSmUnload(
  21. IN PDRIVER_OBJECT pDriverObject
  22. );
  23. VOID AtmSmInitializeGlobal(
  24. IN PDRIVER_OBJECT pDriverObject
  25. );
  26. VOID AtmSmCleanupGlobal();
  27. NTSTATUS DriverEntry(
  28. IN PDRIVER_OBJECT pDriverObject,
  29. IN PUNICODE_STRING pRegistryPath
  30. )
  31. /*++
  32. Routine Description:
  33. This is the "init" routine, called by the system when the AtmSmDrv
  34. module is loaded. We initialize all our global objects, fill in our
  35. Dispatch and Unload routine addresses in the driver object, and create
  36. a device object for receiving I/O requests.
  37. Arguments:
  38. pDriverObject - Pointer to the driver object created by the system.
  39. pRegistryPath - Pointer to our global registry path. This is ignored.
  40. Return Value:
  41. NT Status code: STATUS_SUCCESS if successful, error code otherwise.
  42. --*/
  43. {
  44. NDIS_STATUS Status;
  45. NDIS_PROTOCOL_CHARACTERISTICS PChars;
  46. PNDIS_CONFIGURATION_PARAMETER Param;
  47. PDEVICE_OBJECT pDeviceObject;
  48. UNICODE_STRING DeviceName;
  49. UNICODE_STRING SymbolicName;
  50. HANDLE ThreadHandle;
  51. int i;
  52. TraceIn(DriverEntry);
  53. #if DBG
  54. DbgPrint("AtmSmDebugFlag: Address = %p Value = 0x%x\n",
  55. &AtmSmDebugFlag, AtmSmDebugFlag);
  56. #endif
  57. DbgLoud(("Sizeof TCHAR = %hu\n",sizeof(TCHAR)));
  58. AtmSmInitializeGlobal(pDriverObject);
  59. //
  60. // Initialize the debug memory allocator
  61. //
  62. ATMSM_INITIALIZE_AUDIT_MEM();
  63. //
  64. // Initialize the Driver Object.
  65. //
  66. pDriverObject->DriverUnload = AtmSmUnload;
  67. pDriverObject->FastIoDispatch = NULL;
  68. for(i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
  69. {
  70. pDriverObject->MajorFunction[i] = AtmSmDispatch;
  71. }
  72. do
  73. { // break off loop
  74. //
  75. // Create a device object for atm sample module. The device object
  76. // is used by the user mode app for I/O
  77. //
  78. RtlInitUnicodeString(&DeviceName, ATM_SAMPLE_CLIENT_DEVICE_NAME);
  79. Status = IoCreateDevice(
  80. pDriverObject,
  81. 0,
  82. &DeviceName,
  83. FILE_DEVICE_NETWORK,
  84. FILE_DEVICE_SECURE_OPEN,
  85. FALSE,
  86. &pDeviceObject
  87. );
  88. if(NDIS_STATUS_SUCCESS != Status)
  89. {
  90. DbgErr(("Failed to create device object - Error - 0x%X\n",
  91. Status));
  92. break;
  93. }
  94. // we are doing direct IO for sends and recvs
  95. pDeviceObject->Flags |= DO_DIRECT_IO;
  96. // Save the device object
  97. AtmSmGlobal.pDeviceObject = pDeviceObject;
  98. AtmSmGlobal.ulInitSeqFlag |= CREATED_IO_DEVICE;
  99. //
  100. // Set up a symbolic name for interaction with the user-mode
  101. // application.
  102. //
  103. RtlInitUnicodeString(&SymbolicName, ATM_SAMPLE_CLIENT_SYMBOLIC_NAME);
  104. IoCreateSymbolicLink(&SymbolicName, &DeviceName);
  105. AtmSmGlobal.ulInitSeqFlag |= REGISTERED_SYM_NAME;
  106. //
  107. // We are doing direct I/O.
  108. //
  109. pDeviceObject->Flags |= DO_DIRECT_IO;
  110. //
  111. // Now register the protocol.
  112. //
  113. Status = AtmSmInitializeNdis();
  114. if(NDIS_STATUS_SUCCESS != Status)
  115. {
  116. DbgErr(("Failed to Register protocol - Error - 0x%X\n",
  117. Status));
  118. break;
  119. }
  120. AtmSmGlobal.ulInitSeqFlag |= REGISTERED_WITH_NDIS;
  121. }while(FALSE);
  122. if(NDIS_STATUS_SUCCESS != Status)
  123. {
  124. //
  125. // Clean up will happen in Unload routine
  126. //
  127. }
  128. TraceOut(DriverEntry);
  129. return(Status);
  130. }
  131. VOID AtmSmUnload(
  132. IN PDRIVER_OBJECT pDriverObject
  133. )
  134. /*++
  135. Routine Description:
  136. This routine is called by the system prior to unloading us.
  137. Currently, we just undo everything we did in DriverEntry,
  138. that is, de-register ourselves as an NDIS protocol, and delete
  139. the device object we had created.
  140. Arguments:
  141. pDriverObject - Pointer to the driver object created by the system.
  142. Return Value:
  143. None
  144. --*/
  145. {
  146. UNICODE_STRING SymbolicName;
  147. NDIS_STATUS Status;
  148. TraceIn(Unload);
  149. // call the shutdown handler
  150. AtmSmShutDown();
  151. // Remove the Symbolic Name created by us
  152. if(0 != (AtmSmGlobal.ulInitSeqFlag & REGISTERED_SYM_NAME))
  153. {
  154. RtlInitUnicodeString(&SymbolicName, ATM_SAMPLE_CLIENT_SYMBOLIC_NAME);
  155. IoDeleteSymbolicLink(&SymbolicName);
  156. }
  157. // Remove the Device created by us
  158. if(0 != (AtmSmGlobal.ulInitSeqFlag & CREATED_IO_DEVICE))
  159. {
  160. IoDeleteDevice(AtmSmGlobal.pDeviceObject);
  161. }
  162. ATMSM_SHUTDOWN_AUDIT_MEM();
  163. AtmSmCleanupGlobal();
  164. TraceOut(Unload);
  165. return;
  166. }
  167. VOID AtmSmShutDown()
  168. /*++
  169. Routine Description:
  170. Called when the system is being shutdown.
  171. Here we unbind all the adapters that we bound to previously.
  172. Arguments:
  173. None
  174. Return Value:
  175. None
  176. --*/
  177. {
  178. PATMSM_ADAPTER pAdapt, pNextAdapt;
  179. NDIS_STATUS Status;
  180. #if DBG
  181. KIRQL EntryIrql, ExitIrql;
  182. #endif
  183. TraceIn(AtmSmShutDown);
  184. ATMSM_GET_ENTRY_IRQL(EntryIrql);
  185. //
  186. // grab the global lock and Unbind each of the adapters.
  187. //
  188. ACQUIRE_GLOBAL_LOCK();
  189. for(pAdapt = AtmSmGlobal.pAdapterList; pAdapt; pAdapt = pNextAdapt)
  190. {
  191. pNextAdapt = pAdapt->pAdapterNext;
  192. RELEASE_GLOBAL_LOCK();
  193. AtmSmUnbindAdapter(&Status,
  194. (NDIS_HANDLE)pAdapt,
  195. (NDIS_HANDLE)NULL);
  196. ACQUIRE_GLOBAL_LOCK();
  197. }
  198. RELEASE_GLOBAL_LOCK();
  199. // Deregister from NDIS
  200. if(0 != (AtmSmGlobal.ulInitSeqFlag & REGISTERED_WITH_NDIS))
  201. AtmSmDeinitializeNdis();
  202. ATMSM_CHECK_EXIT_IRQL(EntryIrql, ExitIrql);
  203. TraceOut(AtmSmShutDown);
  204. }
  205. VOID AtmSmInitializeGlobal(
  206. IN PDRIVER_OBJECT pDriverObject
  207. )
  208. {
  209. NdisZeroMemory(&AtmSmGlobal, sizeof(ATMSM_GLOBAL));
  210. AtmSmGlobal.ulSignature = atmsm_global_signature;
  211. AtmSmGlobal.pDriverObject = pDriverObject;
  212. NdisAllocateSpinLock(&AtmSmGlobal.Lock);
  213. }
  214. VOID AtmSmCleanupGlobal()
  215. {
  216. NdisFreeSpinLock(&AtmSmGlobal.Lock);
  217. }
  218. NDIS_STATUS AtmSmInitializeNdis()
  219. /*++
  220. Routine Description:
  221. Arguments:
  222. Return Value:
  223. --*/
  224. {
  225. NDIS_STATUS Status;
  226. NDIS_PROTOCOL_CHARACTERISTICS Chars;
  227. //
  228. // Register with NDIS as a protocol.
  229. //
  230. RtlZeroMemory(&Chars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  231. Chars.MajorNdisVersion = 5;
  232. Chars.MinorNdisVersion = 0;
  233. Chars.OpenAdapterCompleteHandler = AtmSmOpenAdapterComplete;
  234. Chars.CloseAdapterCompleteHandler = AtmSmCloseAdapterComplete;
  235. Chars.StatusHandler = AtmSmStatus;
  236. Chars.RequestCompleteHandler = AtmSmRequestComplete;
  237. Chars.ReceiveCompleteHandler = AtmSmReceiveComplete;
  238. Chars.StatusCompleteHandler = AtmSmStatusComplete;
  239. Chars.BindAdapterHandler = AtmSmBindAdapter;
  240. Chars.UnbindAdapterHandler = AtmSmUnbindAdapter;
  241. Chars.PnPEventHandler = AtmSmPnPEvent;
  242. Chars.CoSendCompleteHandler = AtmSmCoSendComplete;
  243. Chars.CoStatusHandler = AtmSmCoStatus;
  244. Chars.CoReceivePacketHandler = AtmSmCoReceivePacket;
  245. Chars.CoAfRegisterNotifyHandler = AtmSmCoAfRegisterNotify;
  246. RtlInitUnicodeString(&Chars.Name, ATMSM_SERVICE_NAME_L);
  247. NdisRegisterProtocol(&Status,
  248. &AtmSmGlobal.ProtHandle,
  249. &Chars,
  250. sizeof(Chars));
  251. return Status;
  252. }
  253. NDIS_STATUS AtmSmDeinitializeNdis()
  254. {
  255. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  256. TraceIn(AtmSmDeinitializeNdis);
  257. if(NULL != AtmSmGlobal.ProtHandle)
  258. {
  259. NdisDeregisterProtocol(&Status, AtmSmGlobal.ProtHandle);
  260. AtmSmGlobal.ProtHandle = NULL;
  261. }
  262. TraceOut(AtmSmDeinitializeNdis);
  263. return Status;
  264. }