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.

424 lines
11 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. PNDIS_CONFIGURATION_PARAMETER Param;
  79. NDIS_STRING Name;
  80. NdisInitializeListHead(&AdapterList);
  81. MUX_INIT_MUTEX(&GlobalMutex);
  82. MUX_INIT_MUTEX(&ControlDeviceMutex);
  83. NdisMInitializeWrapper(&NdisWrapperHandle, DriverObject, RegistryPath, NULL);
  84. do
  85. {
  86. //
  87. // Register the miniport with NDIS. Note that it is the
  88. // miniport which was started as a driver and not the protocol.
  89. // Also the miniport must be registered prior to the protocol
  90. // since the protocol's BindAdapter handler can be initiated
  91. // anytime and when it is, it must be ready to
  92. // start driver instances.
  93. //
  94. NdisZeroMemory(&MChars, sizeof(NDIS_MINIPORT_CHARACTERISTICS));
  95. MChars.MajorNdisVersion = MUX_MAJOR_NDIS_VERSION;
  96. MChars.MinorNdisVersion = MUX_MINOR_NDIS_VERSION;
  97. MChars.InitializeHandler = MPInitialize;
  98. MChars.QueryInformationHandler = MPQueryInformation;
  99. MChars.SetInformationHandler = MPSetInformation;
  100. MChars.TransferDataHandler = MPTransferData;
  101. MChars.HaltHandler = MPHalt;
  102. #ifdef NDIS51_MINIPORT
  103. MChars.CancelSendPacketsHandler = MPCancelSendPackets;
  104. MChars.PnPEventNotifyHandler = MPDevicePnPEvent;
  105. MChars.AdapterShutdownHandler = MPAdapterShutdown;
  106. #endif // NDIS51_MINIPORT
  107. //
  108. // We will disable the check for hang timeout so we do not
  109. // need a check for hang handler!
  110. //
  111. MChars.CheckForHangHandler = NULL;
  112. MChars.ReturnPacketHandler = MPReturnPacket;
  113. //
  114. // Either the Send or the SendPackets handler should be specified.
  115. // If SendPackets handler is specified, SendHandler is ignored
  116. //
  117. MChars.SendHandler = NULL;
  118. MChars.SendPacketsHandler = MPSendPackets;
  119. Status = NdisIMRegisterLayeredMiniport(NdisWrapperHandle,
  120. &MChars,
  121. sizeof(MChars),
  122. &DriverHandle);
  123. if (Status != NDIS_STATUS_SUCCESS)
  124. {
  125. break;
  126. }
  127. NdisMRegisterUnloadHandler(NdisWrapperHandle, MPUnload);
  128. //
  129. // Now register the protocol.
  130. //
  131. NdisZeroMemory(&PChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  132. PChars.MajorNdisVersion = MUX_PROT_MAJOR_NDIS_VERSION;
  133. PChars.MinorNdisVersion = MUX_PROT_MINOR_NDIS_VERSION;
  134. //
  135. // Make sure the protocol-name matches the service-name
  136. // (from the INF) under which this protocol is installed.
  137. // This is needed to ensure that NDIS can correctly determine
  138. // the binding and call us to bind to miniports below.
  139. //
  140. NdisInitUnicodeString(&Name, L"MUXP"); // Protocol name
  141. PChars.Name = Name;
  142. PChars.OpenAdapterCompleteHandler = PtOpenAdapterComplete;
  143. PChars.CloseAdapterCompleteHandler = PtCloseAdapterComplete;
  144. PChars.SendCompleteHandler = PtSendComplete;
  145. PChars.TransferDataCompleteHandler = PtTransferDataComplete;
  146. PChars.ResetCompleteHandler = PtResetComplete;
  147. PChars.RequestCompleteHandler = PtRequestComplete;
  148. PChars.ReceiveHandler = PtReceive;
  149. PChars.ReceiveCompleteHandler = PtReceiveComplete;
  150. PChars.StatusHandler = PtStatus;
  151. PChars.StatusCompleteHandler = PtStatusComplete;
  152. PChars.BindAdapterHandler = PtBindAdapter;
  153. PChars.UnbindAdapterHandler = PtUnbindAdapter;
  154. PChars.UnloadHandler = NULL;
  155. PChars.ReceivePacketHandler = PtReceivePacket;
  156. PChars.PnPEventHandler= PtPNPHandler;
  157. NdisRegisterProtocol(&Status,
  158. &ProtHandle,
  159. &PChars,
  160. sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  161. if (Status != NDIS_STATUS_SUCCESS)
  162. {
  163. NdisIMDeregisterLayeredMiniport(DriverHandle);
  164. break;
  165. }
  166. //
  167. // Let NDIS know of the association between our protocol
  168. // and miniport entities.
  169. //
  170. NdisIMAssociateMiniport(DriverHandle, ProtHandle);
  171. }
  172. while (FALSE);
  173. if (Status != NDIS_STATUS_SUCCESS)
  174. {
  175. NdisTerminateWrapper(NdisWrapperHandle, NULL);
  176. }
  177. return(Status);
  178. }
  179. NDIS_STATUS
  180. PtRegisterDevice(
  181. VOID
  182. )
  183. /*++
  184. Routine Description:
  185. Register an ioctl interface - a device object to be used for this
  186. purpose is created by NDIS when we call NdisMRegisterDevice.
  187. This routine is called whenever a new miniport instance is
  188. initialized. However, we only create one global device object,
  189. when the first miniport instance is initialized. This routine
  190. handles potential race conditions with PtDeregisterDevice via
  191. the ControlDeviceMutex.
  192. NOTE: do not call this from DriverEntry; it will prevent the driver
  193. from being unloaded (e.g. on uninstall).
  194. Arguments:
  195. None
  196. Return Value:
  197. NDIS_STATUS_SUCCESS if we successfully register a device object.
  198. --*/
  199. {
  200. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  201. UNICODE_STRING DeviceName;
  202. UNICODE_STRING DeviceLinkUnicodeString;
  203. PDRIVER_DISPATCH DispatchTable[IRP_MJ_MAXIMUM_FUNCTION];
  204. UINT i;
  205. DBGPRINT(MUX_LOUD, ("==>PtRegisterDevice\n"));
  206. MUX_ACQUIRE_MUTEX(&ControlDeviceMutex);
  207. ++MiniportCount;
  208. if (1 == MiniportCount)
  209. {
  210. for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
  211. {
  212. DispatchTable[i] = PtDispatch;
  213. }
  214. NdisInitUnicodeString(&DeviceName, NTDEVICE_STRING);
  215. NdisInitUnicodeString(&DeviceLinkUnicodeString, LINKNAME_STRING);
  216. //
  217. // Create a device object and register our dispatch handlers
  218. //
  219. Status = NdisMRegisterDevice(
  220. NdisWrapperHandle,
  221. &DeviceName,
  222. &DeviceLinkUnicodeString,
  223. &DispatchTable[0],
  224. &ControlDeviceObject,
  225. &NdisDeviceHandle
  226. );
  227. }
  228. MUX_RELEASE_MUTEX(&ControlDeviceMutex);
  229. DBGPRINT(MUX_INFO, ("<==PtRegisterDevice: %x\n", Status));
  230. return (Status);
  231. }
  232. NTSTATUS
  233. PtDispatch(
  234. IN PDEVICE_OBJECT DeviceObject,
  235. IN PIRP Irp
  236. )
  237. /*++
  238. Routine Description:
  239. Process IRPs sent to this device.
  240. Arguments:
  241. DeviceObject - pointer to a device object
  242. Irp - pointer to an I/O Request Packet
  243. Return Value:
  244. NTSTATUS - STATUS_SUCCESS always - change this when adding
  245. real code to handle ioctls.
  246. --*/
  247. {
  248. PIO_STACK_LOCATION irpStack;
  249. NTSTATUS status = STATUS_SUCCESS;
  250. ULONG inlen, outlen;
  251. PVOID buffer;
  252. NDIS_STRING KeyName;
  253. WCHAR Device[100];
  254. irpStack = IoGetCurrentIrpStackLocation(Irp);
  255. DBGPRINT(MUX_LOUD, ("==>PtDispatch %d\n", irpStack->MajorFunction));
  256. switch (irpStack->MajorFunction)
  257. {
  258. case IRP_MJ_CREATE:
  259. break;
  260. case IRP_MJ_CLOSE:
  261. break;
  262. case IRP_MJ_DEVICE_CONTROL: {
  263. buffer = Irp->AssociatedIrp.SystemBuffer;
  264. inlen = irpStack->Parameters.DeviceIoControl.InputBufferLength;
  265. switch (irpStack->Parameters.DeviceIoControl.IoControlCode) {
  266. //
  267. // Add code here to handle ioctl commands.
  268. //
  269. }
  270. break;
  271. }
  272. default:
  273. break;
  274. }
  275. Irp->IoStatus.Information = 0;
  276. Irp->IoStatus.Status = status;
  277. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  278. DBGPRINT(MUX_LOUD, ("<== Pt Dispatch\n"));
  279. return status;
  280. }
  281. NDIS_STATUS
  282. PtDeregisterDevice(
  283. VOID
  284. )
  285. /*++
  286. Routine Description:
  287. Deregister the ioctl interface. This is called whenever a miniport
  288. instance is halted. When the last miniport instance is halted, we
  289. request NDIS to delete the device object
  290. Arguments:
  291. NdisDeviceHandle - Handle returned by NdisMRegisterDevice
  292. Return Value:
  293. NDIS_STATUS_SUCCESS if everything worked ok
  294. --*/
  295. {
  296. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  297. DBGPRINT(MUX_LOUD, ("==>PassthruDeregisterDevice\n"));
  298. MUX_ACQUIRE_MUTEX(&ControlDeviceMutex);
  299. ASSERT(MiniportCount > 0);
  300. --MiniportCount;
  301. if (0 == MiniportCount)
  302. {
  303. //
  304. // All VELAN miniport instances have been halted.
  305. // Deregister the control device.
  306. //
  307. if (NdisDeviceHandle != NULL)
  308. {
  309. Status = NdisMDeregisterDevice(NdisDeviceHandle);
  310. NdisDeviceHandle = NULL;
  311. }
  312. }
  313. MUX_RELEASE_MUTEX(&ControlDeviceMutex);
  314. DBGPRINT(MUX_INFO, ("<== PassthruDeregisterDevice: %x\n", Status));
  315. return Status;
  316. }