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.

432 lines
12 KiB

  1. /*++
  2. Copyright (c) 1992-2000 Microsoft Corporation
  3. Module Name:
  4. mux.c
  5. Abstract:
  6. DriverEntry and NT dispatch functions for the NDIS MUX Intermediate
  7. Miniport driver sample.
  8. Environment:
  9. Kernel mode
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #define MODULE_NUMBER MODULE_MUX
  15. #pragma NDIS_INIT_FUNCTION(DriverEntry)
  16. #if DBG
  17. //
  18. // Debug level for mux driver
  19. //
  20. INT muxDebugLevel = MUX_WARN;
  21. #endif //DBG
  22. //
  23. // G L O B A L V A R I A B L E S
  24. // ----------- -----------------
  25. //
  26. NDIS_MEDIUM MediumArray[1] =
  27. {
  28. NdisMedium802_3, // Ethernet
  29. };
  30. //
  31. // Global Mutex protects the AdapterList;
  32. // see macros MUX_ACQUIRE/RELEASE_MUTEX
  33. //
  34. MUX_MUTEX GlobalMutex = {0};
  35. //
  36. // List of all bound adapters.
  37. //
  38. LIST_ENTRY AdapterList;
  39. //
  40. // Total number of VELAN miniports in existance:
  41. //
  42. LONG MiniportCount = 0;
  43. //
  44. // Used to assign VELAN numbers (which are used to generate MAC
  45. // addresses).
  46. //
  47. ULONG NextVElanNumber = 0; // monotonically increasing count
  48. //
  49. // Some global NDIS handles:
  50. //
  51. NDIS_HANDLE NdisWrapperHandle = NULL;// From NdisMInitializeWrapper
  52. NDIS_HANDLE ProtHandle = NULL; // From NdisRegisterProtocol
  53. NDIS_HANDLE DriverHandle = NULL; // From NdisIMRegisterLayeredMiniport
  54. NDIS_HANDLE NdisDeviceHandle = NULL; // From NdisMRegisterDevice
  55. PDEVICE_OBJECT ControlDeviceObject = NULL; // Device for IOCTLs
  56. MUX_MUTEX ControlDeviceMutex;
  57. NTSTATUS
  58. DriverEntry(
  59. IN PDRIVER_OBJECT DriverObject,
  60. IN PUNICODE_STRING RegistryPath
  61. )
  62. /*++
  63. Routine Description:
  64. First entry point to be called, when this driver is loaded.
  65. Register with NDIS as an intermediate driver.
  66. Arguments:
  67. DriverObject - pointer to the system's driver object structure
  68. for this driver
  69. RegistryPath - system's registry path for this driver
  70. Return Value:
  71. STATUS_SUCCESS if all initialization is successful, STATUS_XXX
  72. error code if not.
  73. --*/
  74. {
  75. NDIS_STATUS Status;
  76. NDIS_PROTOCOL_CHARACTERISTICS PChars;
  77. NDIS_MINIPORT_CHARACTERISTICS MChars;
  78. NDIS_STRING Name;
  79. NdisInitializeListHead(&AdapterList);
  80. MUX_INIT_MUTEX(&GlobalMutex);
  81. MUX_INIT_MUTEX(&ControlDeviceMutex);
  82. NdisMInitializeWrapper(&NdisWrapperHandle, DriverObject, RegistryPath, NULL);
  83. do
  84. {
  85. //
  86. // Register the miniport with NDIS. Note that it is the
  87. // miniport which was started as a driver and not the protocol.
  88. // Also the miniport must be registered prior to the protocol
  89. // since the protocol's BindAdapter handler can be initiated
  90. // anytime and when it is, it must be ready to
  91. // start driver instances.
  92. //
  93. NdisZeroMemory(&MChars, sizeof(NDIS_MINIPORT_CHARACTERISTICS));
  94. MChars.MajorNdisVersion = MUX_MAJOR_NDIS_VERSION;
  95. MChars.MinorNdisVersion = MUX_MINOR_NDIS_VERSION;
  96. MChars.InitializeHandler = MPInitialize;
  97. MChars.QueryInformationHandler = MPQueryInformation;
  98. MChars.SetInformationHandler = MPSetInformation;
  99. MChars.TransferDataHandler = MPTransferData;
  100. MChars.HaltHandler = MPHalt;
  101. #ifdef NDIS51_MINIPORT
  102. MChars.CancelSendPacketsHandler = MPCancelSendPackets;
  103. MChars.PnPEventNotifyHandler = MPDevicePnPEvent;
  104. MChars.AdapterShutdownHandler = MPAdapterShutdown;
  105. #endif // NDIS51_MINIPORT
  106. //
  107. // We will disable the check for hang timeout so we do not
  108. // need a check for hang handler!
  109. //
  110. MChars.CheckForHangHandler = NULL;
  111. MChars.ReturnPacketHandler = MPReturnPacket;
  112. //
  113. // Either the Send or the SendPackets handler should be specified.
  114. // If SendPackets handler is specified, SendHandler is ignored
  115. //
  116. MChars.SendHandler = NULL;
  117. MChars.SendPacketsHandler = MPSendPackets;
  118. Status = NdisIMRegisterLayeredMiniport(NdisWrapperHandle,
  119. &MChars,
  120. sizeof(MChars),
  121. &DriverHandle);
  122. if (Status != NDIS_STATUS_SUCCESS)
  123. {
  124. break;
  125. }
  126. NdisMRegisterUnloadHandler(NdisWrapperHandle, MPUnload);
  127. //
  128. // Now register the protocol.
  129. //
  130. NdisZeroMemory(&PChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  131. PChars.MajorNdisVersion = MUX_PROT_MAJOR_NDIS_VERSION;
  132. PChars.MinorNdisVersion = MUX_PROT_MINOR_NDIS_VERSION;
  133. //
  134. // Make sure the protocol-name matches the service-name
  135. // (from the INF) under which this protocol is installed.
  136. // This is needed to ensure that NDIS can correctly determine
  137. // the binding and call us to bind to miniports below.
  138. //
  139. NdisInitUnicodeString(&Name, L"MUXP"); // Protocol name
  140. PChars.Name = Name;
  141. PChars.OpenAdapterCompleteHandler = PtOpenAdapterComplete;
  142. PChars.CloseAdapterCompleteHandler = PtCloseAdapterComplete;
  143. PChars.SendCompleteHandler = PtSendComplete;
  144. PChars.TransferDataCompleteHandler = PtTransferDataComplete;
  145. PChars.ResetCompleteHandler = PtResetComplete;
  146. PChars.RequestCompleteHandler = PtRequestComplete;
  147. PChars.ReceiveHandler = PtReceive;
  148. PChars.ReceiveCompleteHandler = PtReceiveComplete;
  149. PChars.StatusHandler = PtStatus;
  150. PChars.StatusCompleteHandler = PtStatusComplete;
  151. PChars.BindAdapterHandler = PtBindAdapter;
  152. PChars.UnbindAdapterHandler = PtUnbindAdapter;
  153. PChars.UnloadHandler = NULL;
  154. PChars.ReceivePacketHandler = PtReceivePacket;
  155. PChars.PnPEventHandler= PtPNPHandler;
  156. NdisRegisterProtocol(&Status,
  157. &ProtHandle,
  158. &PChars,
  159. sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  160. if (Status != NDIS_STATUS_SUCCESS)
  161. {
  162. NdisIMDeregisterLayeredMiniport(DriverHandle);
  163. break;
  164. }
  165. //
  166. // Let NDIS know of the association between our protocol
  167. // and miniport entities.
  168. //
  169. NdisIMAssociateMiniport(DriverHandle, ProtHandle);
  170. }
  171. while (FALSE);
  172. if (Status != NDIS_STATUS_SUCCESS)
  173. {
  174. NdisTerminateWrapper(NdisWrapperHandle, NULL);
  175. }
  176. return(Status);
  177. }
  178. NDIS_STATUS
  179. PtRegisterDevice(
  180. VOID
  181. )
  182. /*++
  183. Routine Description:
  184. Register an ioctl interface - a device object to be used for this
  185. purpose is created by NDIS when we call NdisMRegisterDevice.
  186. This routine is called whenever a new miniport instance is
  187. initialized. However, we only create one global device object,
  188. when the first miniport instance is initialized. This routine
  189. handles potential race conditions with PtDeregisterDevice via
  190. the ControlDeviceMutex.
  191. NOTE: do not call this from DriverEntry; it will prevent the driver
  192. from being unloaded (e.g. on uninstall).
  193. Arguments:
  194. None
  195. Return Value:
  196. NDIS_STATUS_SUCCESS if we successfully register a device object.
  197. --*/
  198. {
  199. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  200. UNICODE_STRING DeviceName;
  201. UNICODE_STRING DeviceLinkUnicodeString;
  202. PDRIVER_DISPATCH DispatchTable[IRP_MJ_MAXIMUM_FUNCTION+1];
  203. DBGPRINT(MUX_LOUD, ("==>PtRegisterDevice\n"));
  204. MUX_ACQUIRE_MUTEX(&ControlDeviceMutex);
  205. ++MiniportCount;
  206. if (1 == MiniportCount)
  207. {
  208. NdisZeroMemory(DispatchTable, (IRP_MJ_MAXIMUM_FUNCTION+1) * sizeof(PDRIVER_DISPATCH));
  209. DispatchTable[IRP_MJ_CREATE] = PtDispatch;
  210. DispatchTable[IRP_MJ_CLEANUP] = PtDispatch;
  211. DispatchTable[IRP_MJ_CLOSE] = PtDispatch;
  212. DispatchTable[IRP_MJ_DEVICE_CONTROL] = PtDispatch;
  213. NdisInitUnicodeString(&DeviceName, NTDEVICE_STRING);
  214. NdisInitUnicodeString(&DeviceLinkUnicodeString, LINKNAME_STRING);
  215. //
  216. // Create a device object and register our dispatch handlers
  217. //
  218. Status = NdisMRegisterDevice(
  219. NdisWrapperHandle,
  220. &DeviceName,
  221. &DeviceLinkUnicodeString,
  222. &DispatchTable[0],
  223. &ControlDeviceObject,
  224. &NdisDeviceHandle
  225. );
  226. }
  227. MUX_RELEASE_MUTEX(&ControlDeviceMutex);
  228. DBGPRINT(MUX_INFO, ("<==PtRegisterDevice: %x\n", Status));
  229. return (Status);
  230. }
  231. NTSTATUS
  232. PtDispatch(
  233. IN PDEVICE_OBJECT DeviceObject,
  234. IN PIRP Irp
  235. )
  236. /*++
  237. Routine Description:
  238. Process IRPs sent to this device.
  239. Arguments:
  240. DeviceObject - pointer to a device object
  241. Irp - pointer to an I/O Request Packet
  242. Return Value:
  243. NTSTATUS - STATUS_SUCCESS always - change this when adding
  244. real code to handle ioctls.
  245. --*/
  246. {
  247. PIO_STACK_LOCATION irpStack;
  248. NTSTATUS status = STATUS_SUCCESS;
  249. ULONG inlen;
  250. PVOID buffer;
  251. DeviceObject;
  252. irpStack = IoGetCurrentIrpStackLocation(Irp);
  253. DBGPRINT(MUX_LOUD, ("==>PtDispatch %d\n", irpStack->MajorFunction));
  254. switch (irpStack->MajorFunction)
  255. {
  256. case IRP_MJ_CREATE:
  257. break;
  258. case IRP_MJ_CLEANUP:
  259. break;
  260. case IRP_MJ_CLOSE:
  261. break;
  262. case IRP_MJ_DEVICE_CONTROL:
  263. {
  264. buffer = Irp->AssociatedIrp.SystemBuffer;
  265. inlen = irpStack->Parameters.DeviceIoControl.InputBufferLength;
  266. switch (irpStack->Parameters.DeviceIoControl.IoControlCode)
  267. {
  268. //
  269. // Add code here to handle ioctl commands.
  270. //
  271. }
  272. break;
  273. }
  274. default:
  275. break;
  276. }
  277. Irp->IoStatus.Information = 0;
  278. Irp->IoStatus.Status = status;
  279. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  280. DBGPRINT(MUX_LOUD, ("<== Pt Dispatch\n"));
  281. return status;
  282. }
  283. NDIS_STATUS
  284. PtDeregisterDevice(
  285. VOID
  286. )
  287. /*++
  288. Routine Description:
  289. Deregister the ioctl interface. This is called whenever a miniport
  290. instance is halted. When the last miniport instance is halted, we
  291. request NDIS to delete the device object
  292. Arguments:
  293. NdisDeviceHandle - Handle returned by NdisMRegisterDevice
  294. Return Value:
  295. NDIS_STATUS_SUCCESS if everything worked ok
  296. --*/
  297. {
  298. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  299. DBGPRINT(MUX_LOUD, ("==>PassthruDeregisterDevice\n"));
  300. MUX_ACQUIRE_MUTEX(&ControlDeviceMutex);
  301. ASSERT(MiniportCount > 0);
  302. --MiniportCount;
  303. if (0 == MiniportCount)
  304. {
  305. //
  306. // All VELAN miniport instances have been halted.
  307. // Deregister the control device.
  308. //
  309. if (NdisDeviceHandle != NULL)
  310. {
  311. Status = NdisMDeregisterDevice(NdisDeviceHandle);
  312. NdisDeviceHandle = NULL;
  313. }
  314. }
  315. MUX_RELEASE_MUTEX(&ControlDeviceMutex);
  316. DBGPRINT(MUX_INFO, ("<== PassthruDeregisterDevice: %x\n", Status));
  317. return Status;
  318. }